因为协议给Swift带来了更多抽象性 可以让设计和实现完全分隔开
OOP的三大特性 封装 继承 多态
POP注意点
1.优先考虑创建协议 而不是父类
2.优先考虑值类型 而不是引用类型
3.巧用协议的扩展功能
4.不要为了面向协议而使用协议
利用协议实现前缀
struct MJ<Base> {
var base: Base
init(_ base: Base) {
self.base = base
}
}
extension String {
var mj: MJ<String> {
return MJ(self)
}
}
extension MJ where Base == String {
var numberCount: Int {
var count = 0
for c in base where ("0"..."9").contains(c) {
count += 1
}
return count
}
}
print("1212".mj.numberCount)
4
struct MJ<Base> {
var base: Base
init(_ base: Base) {
self.base = base
}
}
class Person {}
extension Person {
var mj: MJ<Person> {
return MJ(self)
}
}
extension MJ where Base: Person {
func run(){
}
}
var p = Person()
p.mj.run()
访问类型方法
struct MJ<Base> {
var base: Base
init(_ base: Base) {
self.base = base
}
}
extension String {
var mj: MJ<String> {
return MJ(self)
}
static var mj: MJ<String>.Type {
return MJ<String>.self
}
}
extension MJ where Base == String {
var numberCount: Int {
var count = 0
for c in base where ("0"..."9").contains(c) {
count += 1
}
return count
}
static func test() {
}
}
print("1212".mj.numberCount)
String.mj.test()
协议来抽取相同部分
struct MJ<Base> {
var base: Base
init(_ base: Base) {
self.base = base
}
}
protocol MJCompatible {}
extension MJCompatible {
var mj: MJ<Self> {
return MJ(self)
}
static var mj: MJ<Self>.Type {
return MJ<Self>.self
}
}
//让String拥有前缀属性
extension String: MJCompatible{
}
//给String().mj前缀扩展功能
extension MJ where Base == String {
}
给某个类扩展功能步骤
1. 写一个前缀类型
2. 定义一个协议 给这个协议扩展前缀属性
3. 以后想扩展功能的类型 遵守协议 并且给前缀类型扩展一些功能
struct MJ<Base> {
var base: Base
init(_ base: Base) {
self.base = base
}
}
protocol MJCompatible {}
extension MJCompatible {
var mj: MJ<Self> {
set {}
get {
return MJ(self)
}
}
static var mj: MJ<Self>.Type {
set {}
get {
return MJ<Self>.self
}
}
}
extension String: MJCompatible{
}
extension MJ where Base == String {
var numberCount: Int {
var count = 0
for c in base where ("0"..."9").contains(c) {
count += 1
}
return count
}
static func test() {
}
mutating func run() {
}
}
var str = "2313"
str.mj.run()
Base可以遵守一个协议 实现String NString NSMutableString都可以调用的方法
struct MJ<Base> {
var base: Base
init(_ base: Base) {
self.base = base
}
}
protocol MJCompatible {}
extension MJCompatible {
var mj: MJ<Self> {
set {}
get {
return MJ(self)
}
}
static var mj: MJ<Self>.Type {
set {}
get {
return MJ<Self>.self
}
}
}
extension NSString: MJCompatible{}
extension String: MJCompatible{}
extension MJ where Base: ExpressibleByStringLiteral {
var numberCount: Int {
var count = 0
for c in (base as! String) where ("0"..."9").contains(c) {
count += 1
}
return count
}
}
var str1 = "3213"
var str2: NSString = "31232"
var str3: NSMutableString = "3213"
print(str1.mj.numberCount)
print(str2.mj.numberCount)
print(str3.mj.numberCount)
4
5
4
利用协议实现类型判断
protocol ArrayType {}
extension Array: ArrayType {
}
extension NSArray: ArrayType {
}
func isArrayType(_ type: Any.Type) -> Bool {
return type is ArrayType.Type
}
print(isArrayType([Int].self))
print(isArrayType([Double].self))
print(isArrayType([Any].self))
print(isArrayType(NSArray.self))
网友评论