< 145 } // 145extension Collection where Index: StrIDeable { /// Finds such index N that predicate is true for all elements up to /// but not including the index N,and is false for all elements /// starting with index N. /// Behavior is undefined if there is no such N. func binarySearch(predicate: (Generator.Element) -> Bool) -> Index { var low = startIndex var high = endindex while low != high { let mID = low.advanced(by: low.distance(to: high) / 2) if predicate(self[mID]) { low = mID.advanced(to: 1) } else { high = mID } } return low }}概述我试图转换以下 Swift 2.3代码: //Example usage://(0 ..< 778).binarySearch { $0 < 145 } // 145extension CollectionType where Index: RandomAccessIndexType { /// Finds such index N that predicate is true fo 我试图转换以下 Swift 2.3代码:extension Collection { func binarySearch(predicate: (Iterator.Element) -> Bool) -> Index { var low = startIndex var high = endindex while low != high { let mID = index(low,offsetBy: distance(from: low,to: high)/2) if predicate(self[mID]) { low = index(after: mID) } else { high = mID } } return low }}//Example usage://(0 ..< 778).binarySearch {//Example usage://(0 ..< 778).binarySearch {
< 145 } // 145extension CollectionType where Index: RandomAccessIndexType { /// Finds such index N that predicate is true for all elements up to /// but not including the index N,and is false for all elements /// starting with index N. /// Behavior is undefined if there is no such N. func binarySearch(predicate: Generator.Element -> Bool) -> Index { var low = startIndex var high = endindex while low != high { let mID = low.advancedBy(low.distanceto(high) / 2) if predicate(self[mID]) { low = mID.advancedBy(1) } else { high = mID } } return low }}
进入Swift 3如下:
错误Binary operator ‘/’ cannot be applIEd to operands of type ‘self.Index.StrIDe’ and ‘Int’
抛出让mID = low.advanced(by:low.distance(to:high)/ 2)
有关如何修复它的任何帮助?
在Swift 3中,“集合移动他们的索引”,比较
关于Swift进化的 A New Model for Collections and Indices.特别是,您不要在索引上调用advancedBy(),
但是在集合上使用index()方法来推进索引.所以你的方法将在Swift 3中实现
不需要对索引类型进行任何限制.
总结以上是内存溢出为你收集整理的swift – Stridable Protocol全部内容,希望文章能够帮你解决swift – Stridable Protocol所遇到的程序开发问题。
评论列表(0条)