[英]Xcode 7 Extension Errors
as you know there are few changes in xcode 7 and swift 2. I got 2 errors shown below, how can I fix them? 如您所知,xcode 7和swift 2几乎没有变化。下面显示了2个错误,该如何解决? Thanks 谢谢
extension String {
var wordList:[String] {
return "".join(componentsSeparatedByCharactersInSet(NSCharacterSet.punctuationCharacterSet())).componentsSeparatedByString(" ")
}
var first: String {
return String(self[startIndex])
}
var last: String {
return String(self[endIndex.predecessor()])
}
var scrambleMiddle: String {
if count(self) < 4 { //'(String) -> _' is not identical to 'Int'
return self
}
return first + String(Array(dropLast(dropFirst(self))).shuffled) + last //Type 'String' does not conform to protocol 'Sliceable'
}
}
'(String) -> _' is not identical to 'Int'
is because String does not conform to SequenceType anymore, instead you have to get the characters
property then call the count
method on it: '(String) -> _' is not identical to 'Int'
是因为String不再符合SequenceType,而是必须获取characters
属性,然后在其上调用count
方法:
if self.characters.count < 4 {
return self
}
As of Swift 2 String
is no longer a CollectionType
and Sliceable
so you should perform these actions on the CharacterView
of it with .characters
: 从Swift 2开始, String
不再是CollectionType
和Sliceable
因此您应该使用.characters
在其CharacterView
上执行以下操作:
var scrambleMiddle: String {
if self.characters.count < 4 {
return self
}
return first + String(Array(dropLast(dropFirst(self.characters))).shuffled) + last
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.