xcode 将数组中的字符转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24463992/
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
Converting Character in an array to an Integer
提问by Brejuro
I can't seem to figure out how to do this even though I've searched through documentation.
即使我已经搜索了文档,我似乎也无法弄清楚如何做到这一点。
I'm trying to figure out how to convert a character at an index in an array to an integer.
我想弄清楚如何将数组中索引处的字符转换为整数。
For example, say I have a character array named "container", I can't figure out how to do:
例如,假设我有一个名为“容器”的字符数组,我不知道该怎么做:
var number:Integer = container[3]
Thanks for the help!
谢谢您的帮助!
回答by CodaFi
Swift doesn't make it easy to convert between primitive and typed representations of things. Here's an extension that should help in the meantime:
Swift 并不能轻松地在事物的原始和类型表示之间进行转换。这是一个在此期间应该有所帮助的扩展:
extension Character {
func utf8Value() -> UInt8 {
for s in String(self).utf8 {
return s
}
return 0
}
func utf16Value() -> UInt16 {
for s in String(self).utf16 {
return s
}
return 0
}
func unicodeValue() -> UInt32 {
for s in String(self).unicodeScalars {
return s.value
}
return 0
}
}
This allows you to get pretty close to what you want:
这使您可以非常接近您想要的:
let container : Array<Character> = [ "a", "b", "c", "d" ]
/// can't call anything here, subscripting's also broken
let number = container[2]
number.unicodeValue() /// Prints "100"
For any engineers that come across this question, see rdar://17494834
对于遇到此问题的任何工程师,请参阅 rdar://17494834
回答by ACengiz
I am not sure that it is effective or not but at least it worked. I converted Character to String then to Int.
我不确定它是否有效,但至少它有效。我将 Character 转换为 String 然后转换为 Int。
String(yourCharacterInArray).toInt()
回答by Michele Dall'Agata
You may try this:
你可以试试这个:
var container = "23456789" var number:Int = Array(container.utf8).map { Int(
) }[3]var container = "23456789" var number:Int = Array(container.utf8).map { Int(
) }[3]typealias CString = Array<CChar> func toCString(string: String) -> CString { return Array(string.utf8).map { CChar(
) } + [0] } var cString = toCString("$ 0123456789") println("The 2nd character in cString has value \(cString[1])") // It outputs 32typealias CString = Array<CChar> func toCString(string: String) -> CString { return Array(string.utf8).map { CChar(
) } + [0] } var cString = toCString("$ 0123456789") println("The 2nd character in cString has value \(cString[1])") // It outputs 32var container = "$ 0123456789" var containerAsCString = Array(container.utf8).map { CChar(
) } + [0] println("The 2nd character in container has value \(containerAsCString[1])") // It outputs 32var container = "$ 0123456789" var containerAsCString = Array(container.utf8).map { CChar(
) } + [0] println("The 2nd character in container has value \(containerAsCString[1])") // It outputs 32var chr: [Character] = ["C", "B", "A"] for a in String(chr[1]).unicodeScalars { println(a.value)}
"\(container[3])".toInt()
It's totally ugly, but it does the job. Also it is a bit computational expensive (O(n) each time one access a character in a string). Still this can be a trick to get back a way to build the CStrings:
这完全是丑陋的,但它可以完成工作。此外,它的计算成本有点高(每次访问字符串中的一个字符时为 O(n))。尽管如此,这仍然是一种找回构建 CStrings 方法的技巧:
##代码##
##代码##
or without implementing a function:
或不实现功能:
##代码##
##代码##
回答by Betkowski
Why not just for loop the array and convert everything to Int?
为什么不只是循环数组并将所有内容转换为 Int?
回答by Mathias Magnusson
Why not just convert the character to String, get the unicodeScalars for it and extract the .value on the scalar?
为什么不直接将字符转换为字符串,获取它的 unicodeScalars 并提取标量上的 .value?
something like:
就像是:
##代码##回答by Jaro
For me worked something like:
对我来说,工作类似于:
##代码##