Created
March 8, 2016 16:01
-
-
Save niwatako/a5c6c3e398ad69e726bf to your computer and use it in GitHub Desktop.
あるProtocolに定義されているComputed Propertyの値をそのProtocolに準拠したクラスのextension毎に違う値にする...?
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
protocol PhoneType{ | |
var number:String { get } | |
func callTo(number:String) | |
} | |
extension PhoneType { | |
func callTo(number:String) { | |
print("Call to \(number) from \(self.number)") | |
} | |
} | |
// MARK: - Phone | |
class Phone { | |
func emergency() { | |
CallManager.callToEmergency(self) | |
} | |
} | |
/* "Protocolに準拠したクラスのextension" */ | |
extension Phone: PhoneType { | |
var number:String { return "111-xxx-xxx" } | |
} | |
// MARK: - Phone | |
/* Protocolに準拠したクラス */ | |
class iPhone: PhoneType { | |
func emergency() { | |
CallManager.callToEmergency(self) | |
} | |
func heySiri() { | |
CallManager.requestToSiriWith(self) | |
} | |
} | |
/* のextension */ | |
extension iPhone { | |
var number:String { return "222-xxx-xxx" } | |
} | |
// MARK: - Manager | |
class CallManager { | |
static func callToEmergency(phone:PhoneType) { | |
phone.callTo("119") | |
} | |
static func requestToSiriWith<T:PhoneType where T:iPhone>(phone:T) { | |
print("Accept your request :)") | |
} | |
} | |
// MARK: - Try | |
CallManager.callToEmergency(Phone()) // Call to 119 from 111-xxx-xxx | |
CallManager.callToEmergency(iPhone()) // Call to 119 from 222-xxx-xxx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment