ter*_*dyl 3 xcode deprecated ios swift
我在一个支持Xcode 8和Xcode 9的开发团队中。
我正在开发String.UTF16Index(range.location)Xcode 8 中使用的功能。当我升级到Xcode 9时,会导致错误'init' is deprecated。
因此,在Xcode 9中,我将其更改为UTF16Index.init(encodedOffset: range.lowerBound)。但是,现在在error的Xcode 8中不起作用Argument labels '(encodedOffset:)' do not match any available overloads。
即使我可以检查Xcode版本并编写不同的代码,其中的一行也会在编译时失败。我该如何处理?还是我一直等到我们完全迁移到Xcode 9之前?
从Swift文档中的Version Compatibility:
注意
当Swift 4编译器使用Swift 3代码时,它将其语言版本标识为3.2。因此,您可以使用#if swift(> = 3.2)之类的条件编译块来编写与多个Swift编译器版本兼容的代码。
在你的情况下
#if swift(>=3.2)
let idx = String.UTF16Index(encodedOffset: range.lowerBound)
#else
let idx = String.UTF16Index(range.location)
#endif
Run Code Online (Sandbox Code Playgroud)
在Xcode 9(Swift 4或Swift 3.2模式)中,仅第一行被编译,而在Xcode 8(Swift 3.1)中,仅第二行被编译。
更新:使用Swift encodedOffset被认为是有害的,在Swift 5 中将被弃用。从Swift 4开始,将NSRangea 转换为a 的正确方法Range<String.Index>是
let str = "abc"
let nsRange = NSRange(location: 2, length: 1)
let sRange = Range(nsRange, in: str)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1370 次 |
| 最近记录: |