import Foundation

/// 前缀类型
//前缀类型
struct Swifter<Base> {
    let base: Base
    init(_ base: Base) {
        self.base = base
    }
}
//利用协议扩展属性
protocol SwifterProtocol {}

extension SwifterProtocol {
    var swifter: Swifter<Self> {
        set { }
        get {
            return Swifter(self)
        }
    }
    static var swifter: Swifter<Self>.Type {
        set { }
        get { Swifter<Self>.self }
    }
}

//给字符串扩展功能
extension String: SwifterProtocol {}
extension NSString: SwifterProtocol {}
//extension NSMutableString: SwifterProtocol {}
//给Person扩展功能
class Person{}
extension Person: SwifterProtocol {}

// 扩展功能
extension Swifter where Base: ExpressibleByStringLiteral {
    var numberCount: Int {
        var count = 0
        for c in (base as! String) where ("0"..."9").contains(c) {
            count += 1
        }
        return count
    }
    
    static func test() {
        print("test")
    }
    
    mutating func run() {
//        self.base = "run"
    }
}

extension Swifter where Base: Person {
    func run() {
        print("跑")
    }
}

print("123ooo123".swifter.numberCount)
Person().swifter.run()

String.swifter.test()

var abc = "123"
var def: NSMutableString = "456"
abc.swifter.run()
print(abc)
def.swifter.run()

/////////////////////////

protocol ArrayType {
    
}
extension Array: ArrayType{}
extension NSArray: ArrayType{}
extension String: ArrayType{}
func isArrayType(_ type: Any.Type) -> Bool {
    type is ArrayType.Type
}

print(isArrayType([Int].self))
print(isArrayType([Any].self))
print(isArrayType(NSArray.self))
print(isArrayType(NSMutableArray.self))
print(isArrayType(String.self))

--EOF--