1.問題:遇到Expression of type 'UIViewController?' is unused的警告
let nv = self.presentingViewController as? UINavigationController
nv?.popViewController(animated: false)
解決:
let nv = self.presentingViewController as? UINavigationController
_ = nv?.popViewController(animated: false)
因為在swfit3中規(guī)定,所有有返回值的方法,都需要捕獲返回值.
除非在方法定義時,聲明"@discardableResult"
例如:
@discardableResult func getValue()->Int{
return 3
}
getValue()
這樣在被調(diào)用時,即使不捕獲返回值一樣不會出現(xiàn)警告
2.問題:String.Index.distance
代碼如下,在swift2.3中運行沒有問題,但在swift3.0中,distance的方法就不能這么調(diào)用了
....
var text = "this is a text!"
let start = text.startIndex.advancedBy(item.range.location - dIndex)
let end = text.startIndex.advancedBy(item.range.location + item.range.length - dIndex)
text.removeRange(start..<end) indexes.append(text.startIndex.distanceTo(start))
let tmpDIndex = start.distanceTo(end)
......
將distance的調(diào)用改為下面的調(diào)用方法,同時注意text.removeRange的位置,因為text.characters.distance方法需要保證start,end表示的索引必須是text.characters.count以內(nèi)的值,否則會報錯.所以如果先運行的是text.removeSubrange將可能導(dǎo)致start或end越界.
var text = "this is a text!"
let start = text.characters.index(text.startIndex, offsetBy: item.range.location - dIndex)
let end = text.characters.index(text.startIndex, offsetBy: item.range.location + item.range.length - dIndex)
indexes.append(text.characters.distance(from: text.startIndex, to: start))
let tmpDIndex = text.characters.distance(from: start, to: end)
text.removeSubrange(start..<end)
......