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: Any? = nil | |
| a = 2 | |
| //a = 2.0 | |
| let b = a as? Int //可能ならOptionalへ、失敗ならnil | |
| let c = a as! Int? | |
| //let d = a as Int? compile error!! | |
| let d = a as? Int? | |
| //let e = a! // forced unwrap | |
| print(b) | |
| print(c) |
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
| asの用途 | |
| 1. 乗り継ぎタイプへのキャスト、即値から参照値へ変換、Swiftで導入された構造体からFoundationで定義されたクラスへ変換。 | |
| string -> nsstring | |
| array -> NSArray | |
| dictionary -> nsdictionary | |
| // Brigded type casting | |
| let string = "3" | |
| let nsString = string as NSString | |
| let value = nsString.floatValue // 3 | |
| 逆は、クラスのコンストラクタで可能。例:NSString(string: "Convertion") |
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" |
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
| 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
| 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
| 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
| 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
| 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
| 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) |