Share
Embed
冴えない彼女(userInfo)の育て方
Elvis Shi
April 13, 2017
Transcript
20 slides
-
override init() { super.init() emplyedBy = "MAGES. 5pb. Game div"
job = "iOS Developer" twitter = "@lovee" github = "el-hoshino" additionalInfo = "Euro Truck Simulator 2 ROCKS!!!" class Speaker: Producer { } } -
userInfo: Dictionary ▸ UIKit Ͱଟ͘ͷॴͰ׆༻͞ΕΔ ▸ Notification ▸ NSError ▸
ͦͷଞଟ͘ͷΦϒδΣΫτΛލ͙σʔλͷΓऔΓ ▸ ͦΕނͷສೳ͕͋͞Δ ▸ userInfo ୯ͳΔश׳্ͷม໊ ▸ جຊ [String: Any] ͔ [Hashable: Any] ͷࣙॻܕ ▸ ͢σʔλܕʹറΒΕͳ͍ -
ͦΜͳ userInfo ࡓ͑ͯͳ͍ʂ ▸ Key ͷܕ͕ Stringʢ͔ AnyHashableʣ ▸ ଐੑ
औΕΔ ͕ϒϨΔ ఆ · Β ͳ ͍ ΜͩΑʂ ▸ Value ͷܕ͕ Any ▸ ʢຊདྷͷʣΩϟϥ ܕ ใ ͕ࢮΜͰΔΜͩΑʂ -
ϢʔβϩάΠϯ௨ʢൃ৴ଆʣ let name = Notification .Name(rawValue: "PlayerLoggedin") let userInfo: [String:
Any] = [ "id": 3891, "nickname": "Ճ౻ܙ" ] let notification = Notification (name: name, object: nil, userInfo: userInfo) NotificationCenter.default.post(notification) -
ϢʔβϩάΠϯ௨ʢൃ৴ଆʣ let name = Notification .Name(rawValue: "PlayerLoggedin") let userInfo: [String:
Any] = [ "id": 3891, "nickname": "Ճ౻ܙ" ] let notification = Notification (name: name, object: nil, userInfo: userInfo) NotificationCenter.default.post(notification) औΓಘΔΩʔ͕ར༻͢Δଆ͕Θ͔Βͳ͍ ࣙॻͷܕใ͕ͳ͘ͳΔ -
ϢʔβϩάΠϯ௨ʢड৴ଆʣ func didReceiveNotification( _ notification: Notification) { guard let userInfo
= notification.userInfo else { return } if let id = userInfo["id"] as? Int, let name = userInfo["nickname"] as? String { print(id) print(name) } else if let token = userInfo["token"] as? String { print(token) } } -
ϢʔβϩάΠϯ௨ʢड৴ଆʣ func didReceiveNotification( _ notification: Notification) { guard let userInfo
= notification.userInfo else { return } if let id = userInfo["id"] as? Int, let name = userInfo["nickname"] as? String { print(id) print(name) } else if let token = userInfo["token"] as? String { print(token) } } ΩʔΛࣗྗͰ֮͑Δඞཁ͕͋Δ ಛఆͳܕʹΩϟετμϯ͢Δඞཁ͕͋Δ if จཏͰ͖Δอূ͕ͳ͍ -
ϢʔβϩάΠϯ௨ʢڞ௨ʣ extension Notification { enum Player { case normal(id: Int,
nickname: String) case anonymous(token: String) } } extension Notification.Player { static let key = "Player" } -
ϢʔβϩάΠϯ௨ʢൃ৴ଆʣ extension Notification { enum Player { case normal(id: Int,
nickname: String) case anonymous(token: String) } } extension Notification.Player { static let key = "Player" } let player = Notification.Player .normal(id: 3891, nickname: "Ճ౻ܙ") let userInfo = [Notification.Player.key: player] ͲΜͳΩʔ͕͋Δͷ͔͙͢Θ͔Δ มͷܕใ͕Δ -
ϢʔβϩάΠϯ௨ʢड৴ଆʣ extension Notification { enum Player { case normal(id: Int,
nickname: String) case anonymous(token: String) } } extension Notification.Player { static let key = "Player" } guard let player = notification .userInfo?[Notification.Player.key] as? Notification.Player else { return } switch player { case .normal(id: let id, nickname: let name): print(id) print(name) case .anonymous(token: let token): print(token) } ΩʔΛ֮͑Δඞཁ͕ͳ͍ ܕΛΩϟετμϯ͢Δඞཁ͕ͳ͍ switch case จʹΑΔཏੑͷอূ