ios 警告:不推荐使用“字符”:请直接使用字符串或子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46467284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning: 'characters' is deprecated: Please use String or Substring directly
提问by Krunal
characters- an instance property of String, is deprecated from with Xcode 9.1
characters- String 的一个实例属性,在Xcode 9.1 中已弃用
It was very useful to get a substring from String by using the charactersproperty but now it has been deprecated and Xcode suggests to use substring. I've tried to check around SO questions and apple developer tutorials/guidelines for the same. But could not see any solution/alternate as suggested.
通过使用characters属性从 String 获取子字符串非常有用,但现在它已被弃用,Xcode 建议使用substring. 我试图检查 SO 问题和苹果开发人员教程/指南。但没有看到任何建议的解决方案/替代方案。
Here is warning message:
这是警告消息:
'characters' is deprecated: Please use String or Substring
不推荐使用“字符”:请使用字符串或子字符串
I've so many string operations are performed/handled using property characters.
我使用 property 执行/处理了很多字符串操作characters。
Anyone have any idea/info about this update?
有人对此更新有任何想法/信息吗?
回答by songxunzhao
Swift 4 introduced changes on string API.
You can just use !stringValue.isEmptyinstead of stringValue.characters.count > 0
Swift 4 引入了对字符串 API 的更改。
你可以使用!stringValue.isEmpty代替stringValue.characters.count > 0
for more information you get the sample from here
有关更多信息,请从此处获取样本
for e.g
例如
let edit = "Summary"
edit.count // 7
回答by Mohammad Sadegh Panadgoo
Swift 4 vs Swift 3 examples:
Swift 4 与 Swift 3 示例:
let myString = "test"
for char in myString.characters {print(char) } // Swift 3
for char in myString { print(char) } // Swift 4
let length = myString.characters.count // Swift 3
let length = myString.count // Swift 4
回答by Edison
One of the most common cases for manipulating strings is with JSON responses. In this example I created an extension in my watch app to drop the last (n) characters of a Bitcoin JSON object.
处理字符串的最常见情况之一是使用 JSON 响应。在这个例子中,我在我的手表应用程序中创建了一个扩展来删除比特币 JSON 对象的最后 (n) 个字符。
Swift 3:
斯威夫特 3:
func dropLast(_ n: Int = 0) -> String {
return String(characters.dropLast(n))
Xcode 9.1 Error Message:
Xcode 9.1 错误信息:
'characters' is deprecated: Please use String or Substring directly
'characters' 已弃用:请直接使用 String 或 Substring
Xcode is telling us to use the string variable or method directly.
Xcode是告诉我们要使用的字符串变量或方法直接。
Swift 4:
斯威夫特 4:
func dropLast(_ n: Int = 0) -> String {
return String(dropLast(n))
}
Complete Extension:
完整扩展:
extension String {
func dropLast(_ n: Int = 0) -> String {
return String(dropLast(n))
}
var dropLast: String {
return dropLast()
}
}
Call:
称呼:
print("rate:\(response.USDRate)")
let literalMarketPrice = response.USDRate.dropLast(2)
print("literal market price: \(literalMarketPrice)")
Console:
安慰:
//rate:7,101.0888 //JSON float
//literal market price: 7,101.08 // JSON string literal
Additional Examples:
其他示例:
print("Spell has \(invisibleSpellName.count) characters.")return String(dropLast(n))return String(removeLast(n))
print("Spell has \(invisibleSpellName.count) characters.")return String(dropLast(n))return String(removeLast(n))
Documentation:
文档:
You'll often be using common methods such as dropLast()or removeLast()or countso here is the explicit Apple documentation for each method.
你会经常使用常用的方法,例如dropLast()或removeLast()或count因此这里是每种方法的明确的苹果文档。
回答by Boris
That warning is just a top of the iceberg, there were a loot of string changes, strings are again a collection of characters, but we got soemthing new and cool, subStrings :)
该警告只是冰山一角,有大量字符串更改,字符串再次是字符的集合,但我们得到了一些新的和酷的子字符串 :)
This is a great read about this: https://useyourloaf.com/blog/updating-strings-for-swift-4/
这是一个很好的阅读:https: //useyourloaf.com/blog/updating-strings-for-swift-4/
回答by Pranavan Sp
Use this charactersbecause Stringstopped being a collection in Swift 2.0. However this is still valid code in Swift 4 but is no longer necessary now that Stringis a Collectionagain.
使用它characters是因为String在 Swift 2.0 中不再是一个集合。然而,这仍然是有效的代码斯威夫特4,但现在不再是必要的,String是Collection一次。
For example a Swift 4 Stringnow has a direct count property that gives the character count:
例如,Swift 4String现在有一个直接计数属性,可提供字符计数:
// Swift 4
let spString = "Stack"
spString.count // 5
Examples for String and SubString.
String 和 SubString 的示例。
String
细绳
Swift 4 Stringnow directly get Element that gives the first character of String: (string.characters.first)
Swift 4String现在直接获取给出字符串第一个字符的 Element:(string.characters.first)
let spString = "Stack"
let firstElement = spString.first //S
SubString
子串
Using SubString get first character.
使用 SubString 获取第一个字符。
let spstring = "Welcome"
let indexStartOfText = spstring.index(spstring.startIndex, offsetBy: 1)
let sub = spstring.substring(to: indexStartOfText)
print(sub) //W
回答by mahendra solanki
You can also use this code for dictionary grouping without using { $0.characters.first! }.
您也可以使用此代码进行字典分组而不使用 { $0.characters.first! }.
let cities = ["Shanghai": 24_256_800, "Karachi": 23_500_000, "Beijing": 21_516_000, "Seoul": 9_995_000]
let groupedCities = Dictionary(grouping: cities.keys) { ##代码##.first! }
print(groupedCities)
回答by Ayoub Bouya
Just remove characters For example:
只需删除字符 例如:
stringValue.characters.count to stringValue.count
stringValue.characters.count 到 stringValue.count


