swift范围初始化函数错误

时间:2016-01-05 02:19:15

标签: swift

我的代码

self.p_formatterTime = self.pubTime?.substringWithRange(Range<Int>(start: 5, end: 11))

编译错误消息:

Cannot invoke initializer for type 'Range<Int>' with an argument list of type '(start: Int, end: Int)'

如何解决?

1 个答案:

答案 0 :(得分:2)

if let a = self.pubTime {
    self.p_formatterTime = a.substringWithRange(Range<String.Index>(start: a.startIndex.advancedBy(5), end: a.startIndex.advancedBy(11)))
    // ...
}

一个有效的例子,因为我真的不知道你的字符串self.p_formatterTimeself.pubTime?是否是有效的字符串:

let myString = "0123456789ABCDEFG"
let mySubString = myString.substringWithRange(Range<String.Index>(start: myString.startIndex.advancedBy(5), end: myString.startIndex.advancedBy(11)))
print(mySubString) // prints "56789A"