[英]Swift Converting Character to String
I have an issue with converting character type to String type.我在将字符类型转换为字符串类型时遇到问题。 First of all, I have below extension of String for finding nth character within String.首先,我有下面的字符串扩展名,用于在字符串中查找第 n 个字符。
extension String {
func characterAtIndex(index: Int) -> Character? {
var cur = 0
for char in self {
if cur == index {
return char
}
cur++
}
return nil
}
}
I get what I want with this class extension.我通过这个类扩展得到了我想要的东西。 However when I use that nth character for title of my custom UIButton, gives an error.但是,当我使用第 n 个字符作为自定义 UIButton 的标题时,会出现错误。 My Uibutton Class is我的 Uibutton 类是
class hareketliHarfler: UIButton {
init(frame: CGRect) {
super.init(frame: frame)
// Initialization code
}
func getLetter(letter:String!){
self.titleLabel.text = letter
}
}
The error show when i try to access "getLetter(letter:String)" function.当我尝试访问“getLetter(letter:String)”函数时显示错误。 Here is example of main view Controller codes:这是主视图控制器代码的示例:
var harfim = hareketliHarfler(frame: CGRectMake(100,100,100,100))
var str="This is my String"
var bufi=str.characterAtIndex(3)
harfim.getLetter(bufi as AnyObject) ****
In * section I try .getLetter(bufi), .getLetter(bufi as String) also I try to change parameter type of function.在*部分我尝试 .getLetter(bufi), .getLetter(bufi as String) 我也尝试改变函数的参数类型。 Look like: func getLetter(letter:Character!) or func getLetter(letter:AnyObject!)...etc Didn't find a way.看起来像: func getLetter(letter:Character!) 或 func getLetter(letter:AnyObject!)...etc 没有找到方法。 Need a help on that.需要帮助。 Thank you谢谢
Your problem is quite simple: your characterAtIndex function returns a Character, and self.titleLabel.text is a String.你的问题很简单:你的 characterAtIndex 函数返回一个 Character,而 self.titleLabel.text 是一个 String。 You can't convert between the two implicitly.您不能在两者之间进行隐式转换。 The easiest way would be to turn the Character into a String using the String initialiser:最简单的方法是使用 String 初始值设定项将 Character 转换为 String:
// ch will be Character? type.
if let ch = str.characterAtIndex(3) {
// Initialise a new String containing the single character 'ch'
harfim.getLetter(String(ch))
} else {
// str didn't have a third character.
}
Unlike other solutions, this is safe for unusual Unicode characters, and won't initialise a potentially large array or iterate the whole String just to get the third character.与其他解决方案不同,这对于不寻常的 Unicode 字符是安全的,并且不会初始化一个潜在的大数组或迭代整个 String 来获取第三个字符。
How about the simple String(theCharacter)
简单的String(theCharacter)
怎么样
Works in Swift 4 and Swift 5适用于Swift 4和Swift 5
Change this:改变这个:
var bufi=str.characterAtIndex(3)
harfim.getLetter(bufi as AnyObject)
to this:对此:
harfim.getLetter(String(Array(str)[3]))
So what happening here:那么这里发生了什么:
we create an array from our string.我们从我们的字符串创建一个数组。 Array elements are symbols from original string.数组元素是来自原始字符串的符号。 Such break down correctly tracks symbols that are presented with a sequences of two or more code points.这种分解正确地跟踪以两个或更多代码点的序列呈现的符号。 Eg emoji or flag as noted by @MartinR.例如@MartinR 指出的表情符号或标志。
We access element at 4-th position.我们访问第 4 个位置的元素。
Note that as we crate an array from initial string then performance wise is better to use this method only with short strings and avoid it in oft-repeated routines.请注意,当我们从初始字符串创建一个数组时,性能方面最好仅将此方法用于短字符串,并避免在经常重复的例程中使用。 But in your case it seems to be OK.但在你的情况下,它似乎没问题。
Can also use Character(text).isNumber
if you want to get localised numbers.如果您想获得本地化的数字,也可以使用Character(text).isNumber
。
Reference: https://developer.apple.com/documentation/swift/character/3127015-isnumber参考: https : //developer.apple.com/documentation/swift/character/3127015-isnumber
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.