gpt4 book ai didi

Linux 上的 Swift NSString 到 String

转载 作者:搜寻专家 更新时间:2023-11-01 05:37:55 24 4
gpt4 key购买 nike

在文档中我找到了文章:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html

还有一个代码:

import Foundation
let myString: NSString = "123"
if let integerValue = Int(myString as String) {
print("\(myString) is the integer \(integerValue)")
}
// prints "123 is the integer 123"

在 MacOS 上可以,但在 Lunux 上不行。我收到一个错误:

error: cannot convert value of type 'NSString' to type 'String' in coercion if let integerValue = Int(myString as String) {

最佳答案

无法访问 Linux 编译器……我建议采用以下解决方法。

let myString: NSString = "123"
if let integerValue = Int(String(myString)) {
print("\(myString) is the integer \(integerValue)")
}

let myString: NSString = "123"
if myString.isEqualToString("0") || myString.integerValue != 0 {
print("\(myString) is the integer \(myString.integerValue)")
}

关于Linux 上的 Swift NSString 到 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34641327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com