Skip to content

Instantly share code, notes, and snippets.

@externvoid
Last active May 25, 2016 01:10
Show Gist options
  • Select an option

  • Save externvoid/af76b59f0140fc36146f1e752f779026 to your computer and use it in GitHub Desktop.

Select an option

Save externvoid/af76b59f0140fc36146f1e752f779026 to your computer and use it in GitHub Desktop.
コレクションオブジェクト(編集中)
⭕️forEach, for-inに応答できるオブジェクトを生成できるクラスを作る。
2つの文はループ枚にnext()を呼び出す。似たような関数anyGeneratorがあってその関数はGenerator型(closureを返すので、next()を呼び出すのと似ている)を返す。
1️⃣SequenceTypeに準拠させる
2️⃣generateメソッドを定義(SequenceTypeが要求)
3️⃣戻り値はGeneratorTypeに準拠させる
4️⃣戻り値のクラスにはnext()メソッドを用意する。
let sine = SineClass() //ここでgenerate()は呼ばれないfor or forEachで一回だけ呼ばれる
💠使い方1
for x in sine {
print(x)
}
💠使い方2
for (i, x) in sine.enumerate() {
print(i, x, separator: " :", terminator:"+ ?\n")
}
💠使い方3
sine.forEach {e in
print(e)
}
import Foundation
class SineClass : SequenceType {
/// SequenceType に準拠するための、ジェネレーターを返すメソッドです。
func generate() -> SineGenerator {
// print("OK")
return SineGenerator(self)
}
/// 引数で受け取ったラジアンを使って sin を計算します。
func value(radian:Double) -> Double {
return sin(radian)
}
}
class SineGenerator : GeneratorType {
private unowned var owner:SineClass
private var angle:Double = 0
/// 扱うデータを持つインスタンスを受け取って初期化しています。
init(_ owner:SineClass) {
self.owner = owner
}
/// 次の値を取得しつつ、その次を取得できるように準備します。
func next() -> Double? {
let pai: Double = 3.141592653589793
let getRadianFromAngle = { (angle:Double) in
angle * pai / 180.0
}
let getValue = { (radian:Double) in
self.owner.value(radian)
}
if self.angle < 360 {
return getValue(getRadianFromAngle(self.angle++))
} else {
return nil
}
}
}
// instanciation
let sine = SineClass()
for x in sine {
print(x)
}
⭕️インデックスアクセスできるオブジェクトを生成できるクラスを作る。(ex. print(cosine[3]))
1️⃣編集途中
class CosineClass : CollectionType {
/// 範囲の開始位置と終了位置を決定しています。
let startIndex:Int = 0
let endIndex:Int = 360
/// 位置から cos の値を取り出すサブスクリプトです。
subscript (angle: Int) -> Double {
let pai: Double = 3.141592653589793
let getRadian = { Double(angle) * pai / 180.0 }
if angle >= startIndex && angle < endIndex {
return cos(getRadian())
}
else {
print("out of range")
abort()
}
}
/// 範囲内の値を順番に取り出すためのジェネレーターを取得します。
func generate() -> IndexingGenerator<CosineClass> {
return IndexingGenerator(self)
}
}
let cosine = CosineClass()
//for (i, x) in cosine.enumerate() {
// print(i, x)
//}
// http://nshipster.com/swift-collection-protocols/
print(cosine.count)
print(cosine.first)
print(cosine[4])
print(cosine.indices)
print(cosine.isEmpty)
5️⃣
6️⃣7️⃣8️⃣9️⃣🔟
⭕️日付から曜日を求める
let date: NSDate = NSDate()
let cal: NSCalendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
let comp: NSDateComponents = cal.components([NSCalendarUnit.Weekday], fromDate: date)
let weekday: Int = comp.weekday
⭕️日付リテラルから日付オブジェクトを作る。
1️⃣NSDateFormatterオブジェクトのdateFromStringメソッドを使う。
dateStr String "Fri, 20 May 2016 03:19:18 -0700”文字列から日付オブジェクトを作るには、
let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en-US")
formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzzz"
let dateStr = entry["publishedDate"] as! String
let date = formatter.dateFromString(dateStr)
⭕️日付オブジェクトの作り方その2
1️⃣NSCalendarとNSDateCompnentsから作る(直感的)
import Foundation
let dateComponents = NSDateComponents()
dateComponents.day = 4
dateComponents.month = 5
dateComponents.year = 2006
let gregorianCalendar = NSCalendar(calendarIdentifier:
NSCalendarIdentifierGregorian)
let date = gregorianCalendar!.dateFromComponents(dateComponents)
print(date)
//Optional(2006-05-03 15:00:00 +0000)
注意事項:print(date)はシステムで使われる協定世界時UTCの値が表示される。ではローカルタイムで表示するには?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment