This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ⭕️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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Vehicle | |
| { | |
| var wheels: Int? { | |
| get { | |
| return nil | |
| } | |
| } | |
| class func vehicleFactory(wheels:Int) -> Vehicle | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| var dateFmt: NSDateFormatter = NSDateFormatter() | |
| dateFmt.dateFormat = "yyyy/MM/dd HH:mm:ss" | |
| let date1 = dateFmt.dateFromString("2014/03/18 00:00:00")! | |
| let date2: NSDate = dateFmt.dateFromString("2014/03/19 00:00:00")! | |
| var order = date1.compare(date2) | |
| print(order) | |
| print(order == NSComparisonResult.OrderedDescending) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| func iterateEnum<T: Hashable>(_: T.Type) -> AnyGenerator<T> { | |
| var cast: (Int -> T)! // reference to clousure | |
| //print(($0).dynamicType) | |
| // what is $0 ?, Int, 0, 1, 2, 3, 4 for nil | |
| switch sizeof(T) { | |
| case 0: return anyGenerator(GeneratorOfOne(unsafeBitCast((), T.self))) | |
| case 1: cast = { unsafeBitCast(UInt8(truncatingBitPattern: $0), T.self) } | |
| // print($0.dynamicType);print($0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var ar2:Array<NSString> = ["aaa", "bbb", "abc",] | |
| var strings:NSMutableArray = NSMutableArray(array: ar2) | |
| print(strings) | |
| strings.sortUsingComparator( { | |
| return $1.compare($0 as String) | |
| } ) | |
| //error: 'AnyObject' is not convertible to 'String'; did you mean to use 'as!' to force downcast? | |
| // return $1.compare($0 as String) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var a = 0 | |
| var b = 0 | |
| let closure0 = { [a] (b:Int)->Void in | |
| // capture list, argument listの同時記述が出来る。 | |
| print(a, b) | |
| } | |
| a = -1 | |
| b = 99 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyClass { | |
| var x: Int = 10 | |
| func method() { | |
| print("x = \(x)") | |
| } | |
| var g:()->() = { | |
| let y = self.x | |
| print(__LINE__,"行", String(self.dynamicType), __FUNCTION__) | |
| print("y = \(y)") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| nilを返すかもしれないメソッドから値を受け取る。 | |
| 例:objectForKeyはAnyObject?を返す!! | |
| [String: Int]?を[String: Int]へキャストする。 | |
| import Foundation | |
| var obj: [String: Int]? = ["hp": 0] | |
| print(obj) | |
| var obj3: [String: Int] = obj! as [String: Int] ?? [String: Int]() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // nilが返るかもしれないメソッドの取り扱い | |
| //let obj6 = NSUserDefaults.standardUserDefaults().objectForKey("user data") | |
| var obj6: AnyObject? = nil | |
| print(obj6) | |
| var obj5 = obj6 as? [String: Int] ?? [String: Int]() | |
| //var obj5: [String: Int] = obj! as [String: Int] ?? [String: Int]() | |
| obj5["hp"] = 3 | |
| print(obj5) // ["hp": 3] | |
| print(obj5.dynamicType) // Dictionary<String, Int> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Bar { | |
| let someVal = 5 | |
| lazy var g:() -> String = { [unowned self] in | |
| // lazy: 次のエラー対策:error: use of unresolved identifier 'self' | |
| return String(self.someVal) | |
| } | |
| } | |
| var bar = Bar() | |
| print(bar.g()) // string "5" |