extension扩展Int,必包作为参数,好巧妙

26. January 2016 Swift 0

extension Int{

func times(closure:(() -> ())?){ //必包作为参数,巧用self

if self >= 0 {

for _ in 0 ..< self {

closure?()

}

}

}

}

10.times{

print(“something”)

}// 打印十次

extension CustomStringConvertible{ //扩展协议

var upperDescription:String{

return self.description.uppercaseString

}

}

[“key”:”value”].upperDescription


Leave a Reply

Your email address will not be published. Required fields are marked *