ios Swift 将字符转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25106603/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:41:09  来源:igfitidea点击:

Swift Converting Character to String

iosswift

提问by Antiokhos

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. My Uibutton Class is

我通过这个类扩展得到了我想要的东西。但是,当我使用第 n 个字符作为自定义 UIButton 的标题时,会出现错误。我的 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. Here is example of main view Controller codes:

当我尝试访问“getLetter(letter:String)”函数时显示错误。这是主视图控制器代码的示例:

    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. Look like: func getLetter(letter:Character!) or func getLetter(letter:AnyObject!)...etc Didn't find a way. Need a help on that. Thank you

*部分我尝试 .getLetter(bufi), .getLetter(bufi as String) 我也尝试改变函数的参数类型。看起来像: func getLetter(letter:Character!) 或 func getLetter(letter:AnyObject!)...etc 没有找到方法。需要帮助。谢谢

回答by Matt Gibson

Your problem is quite simple: your characterAtIndex function returns a Character, and self.titleLabel.text is a 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:

你的问题很简单:你的 characterAtIndex 函数返回一个 Character,而 self.titleLabel.text 是一个 String。您不能隐式地在两者之间进行转换。最简单的方法是使用 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 来获取第三个字符。

回答by Naishta

How about the simple String(theCharacter)

简单的怎么样 String(theCharacter)

Works in Swift 4

适用于Swift 4

回答by Keenle

Change this:

改变这个:

var bufi=str.characterAtIndex(3)
harfim.getLetter(bufi as AnyObject)

to this:

对此:

harfim.getLetter(String(Array(str)[3]))

So what happening here:

那么这里发生了什么:

  1. 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. E.g. emoji or flag as noted by @MartinR.

  2. We access element at 4-th position.

  1. 我们从我们的字符串创建一个数组。数组元素是来自原始字符串的符号。这种分解正确地跟踪以两个或更多代码点的序列呈现的符号。例如@MartinR 指出的表情符号或标志。

  2. 我们访问第 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.

请注意,当我们从初始字符串创建一个数组时,性能方面最好仅将此方法用于短字符串,并避免在经常重复的例程中使用。但在你的情况下,它似乎没问题。

回答by Ezzer

Can also use Character(text).isNumberif 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