在Swift1.x支持String split方法,但是在Swift2.0不支持
let ipAddr = "192.168.1.110"
// Split dot-decimal string into its (presumably) four components
let components = split(ipAddr.characters) { $0 == "."}
仔细看String中split方法,split方法不再属于String,而是在CollectionType的扩展中。
如何使用split方法
方法一:使用componentsSeparatedByString
let ipAddr = "192.168.1.110"
let arr = ipAddr.componentsSeparatedByString(".")
方法二:使用characters.split
let ipAddr = "192.168.1.110"
let components = ipAddr.characters.split{$0 == "."}
方法三:String遵循CollectionType协议
extension String : CollectionType {}
let ipAddr = "192.168.1.110"
let components = ipAddr.split{$0 == "."}
happy coding happy life.
welcom to my blog.
网友评论