Last active
August 29, 2015 14:20
-
-
Save gologo13/87038dd95c974484520f to your computer and use it in GitHub Desktop.
Swift初心者を卒業するためのTips(enum編) ref: http://qiita.com/gologo13/items/0d484da7d4c9beb9e0b1
This file contains 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
typedef NS_ENUM (NSUInteger, kIconSize) { | |
kIconSizeSmall, | |
kIconSizeMedium, | |
kIconSizeLarge, | |
kIconSizeOriginal | |
}; |
This file contains 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
enum kIconSize : String { | |
case Small = "Small" | |
case Medium = "Medium" | |
case Large = "Large" | |
case Original = "Original" | |
} | |
// .rawValue で値を取り出す | |
println(kIconSize.Small.rawValue) | |
// -> "Small" |
This file contains 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
enum Result<T> { | |
// 成功の場合 | |
case Success(T) | |
// 失敗の場合 | |
case Error(NSError) | |
} |
This file contains 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
- (void)doAsyncRequestWithCompletion:(void (^)(NSDictionary *response, NSError *error))completionHandler; | |
This file contains 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
// T にはレスポンスのモデルクラスを指定する | |
class HTTPClient<T> { | |
// 通信が失敗した時のエラー情報を返す | |
func error() -> NSError? { ... } | |
// 通信が成功した場合のレスポンスを返す | |
func getResult() -> T { ... } | |
// 非同期で結果が返ってくるメソッド | |
func getResultAsync(completion:(Result<T> -> Void)) { | |
// 処理に失敗した場合 | |
if let error = self.error() { | |
completion(Result<T>.Error(error)) | |
return | |
} | |
// 成功の場合 | |
let result = self.getResult() | |
completion(Result<T>.Success(result)) | |
} | |
} | |
// 使用例 | |
HTTPClient<UserModel>().getResultAsync { result in | |
switch result { | |
case .Error(let error): | |
println(error) | |
case .Success(let resp): | |
println(resp) | |
} | |
} |
This file contains 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
let size = kIconSize.Small | |
switch size { | |
case .Small: | |
// ここにマッチ | |
println("Small Size") // -> "Small Size" | |
case .Medium: | |
println("Medium Size") | |
case .Large: | |
println("Large Size") | |
case .Original: | |
println("Original Size") | |
} |
This file contains 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
- (NSString *)iconSizeToString:(kIconSize)iconSize | |
{ | |
switch (iconSize) { | |
case kIconSizeSmall: | |
return @"Small"; | |
case kIconSizeMedium: | |
return @"Medium"; | |
case kIconSizeLarge: | |
return @"Large"; | |
case kIconSizeOriginal: | |
return @"Original" | |
} | |
} |
This file contains 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
enum kIconSize { | |
case Small | |
case Medium | |
case Large | |
case Original | |
func sizeName() -> String { | |
switch self { | |
case .Small: | |
return "Small" | |
case .Medium: | |
return "Medium" | |
case .Large: | |
return "Large" | |
case .Original: | |
return "Original" | |
} | |
} | |
} | |
println(kIconSize.Small.sizeName()) | |
// "Small" |
This file contains 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
// 定義 | |
enum Barcode { | |
case UPCA(Int, Int, Int, Int) | |
case QRCode(String) | |
} |
This file contains 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
// Barcode型の初期化 | |
let barcode = Barcode.QRCode("ABCDEFGHIJKLMNOP") | |
// ↓はコンパイルエラーになる | |
Barcode.QRCode() |
This file contains 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
switch barcode { | |
case .UPCA(let numberSystem, let manufacturer, let product, let check): | |
println("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).") | |
case .QRCode(let productCode): | |
println("QR code: \(productCode).") | |
} | |
// prints "QR code: ABCDEFGHIJKLMNOP." |
This file contains 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
enum Barcode { | |
case UPCA(Int, Int, Int, Int) | |
case QRCode(String) | |
func qrcode() -> String? { | |
switch self { | |
case QRCode(let qrcode): | |
return qrcode | |
default: | |
return nil | |
} | |
} | |
} |
This file contains 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
enum Optional<T> { | |
case Some(T) | |
case None | |
} | |
var x = Optional<Int>.None // nil と同じ意味 | |
x = Optional<Int>.Some(10) // Optional(10) と同じ意味 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment