Swift4 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upt
来源:互联网 发布:windows rt 升级 编辑:程序博客网 时间:2024/06/07 00:48
解决Swift4 substring deprecated: Please use String slicing subscript with a ‘partial range upto’ operator.
extension String { func substring(from: Int?, to: Int?) -> String { if let start = from { guard start < self.characters.count else { return "" } } if let end = to { guard end >= 0 else { return "" } } if let start = from, let end = to { guard end - start >= 0 else { return "" } } let startIndex: String.Index if let start = from, start >= 0 { startIndex = self.index(self.startIndex, offsetBy: start) } else { startIndex = self.startIndex } let endIndex: String.Index if let end = to, end >= 0, end < self.characters.count { endIndex = self.index(self.startIndex, offsetBy: end + 1) } else { endIndex = self.endIndex } return String(self[startIndex ..< endIndex]) } func substring(from: Int) -> String { return self.substring(from: from, to: nil) } func substring(to: Int) -> String { return self.substring(from: nil, to: to) } func substring(from: Int?, length: Int) -> String { guard length > 0 else { return "" } let end: Int if let start = from, start > 0 { end = start + length - 1 } else { end = length - 1 } return self.substring(from: from, to: end) } func substring(length: Int, to: Int?) -> String { guard let end = to, end > 0, length > 0 else { return "" } let start: Int if let end = to, end - length > 0 { start = end - length + 1 } else { start = 0 } return self.substring(from: start, to: to) }}
let string = "Hello,World!"string.substring(from: 1, to: 7) print: ello,Wostring.substring(to: 7) print: Hello,Wostring.substring(from: 3) print: lo,World!string.substring(from: 1, length: 4) print: ellostring.substring(length: 4, to: 7) print: o,WoUpdated substring(from: Int?, length: Int) to support starting from zero.
阅读全文
1 0
- Swift4 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upt
- ruby 2.0.0p0 : DL is deprecated, please use Fiddle
- ActionContextCleanUp <<< is deprecated! Please use the new filters警告
- >>> FilterDispatcher <<< is deprecated! Please use the new filters!
- >>> FilterDispatcher <<< is deprecated! Please use the new filters!
- >>> FilterDispatcher <<< is deprecated! Please use the new filters!
- FilterDispatcher <<< is deprecated! Please use the new filters!
- >>> FilterDispatcher <<< is deprecated! Please use the new filters!
- >>> FilterDispatcher <<< is deprecated! Please use the new filters!
- FilterDispatcher <<< is deprecated! Please use the new filters!
- string subscript out of range
- An App ID with identifier is not available.Please enter a different string. 或者这样的,fail to code sign
- Deprecated: main(): The Mongo class is deprecated, please use the MongoClient class in
- DEPRECATED: Use of this script to execute hdfs command is deprecated. Instead use the hdfs command
- Error: An App ID with identifier "*****" is not avaliable. Please enter a different string.
- An App ID with Identifier is not available. Please enter a different string
- An App ID with Identifier 'com.domainName.AppName' is not available. Please enter a different string
- An App ID with Identifier 'XXXX' is not available. Please enter a different string.
- Window Sum
- [monitor] 6. Linux kdump(系统临终快照)
- [App] FTP 虚拟用户搭建服务器
- 使用form提交后返回值处理
- Oracle的完整练习
- Swift4 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upt
- hibernate乐观锁
- HDU 2700 Parity(找规律)
- 中文乱码问题
- HDU 2710 Max Factor
- 装饰者模式
- HTML 全局属性及其他
- HDU 2568 前进
- HDU 2552 三足鼎立