它为NSNumber
和NSNumber
说“使用未解析的标识符”。我无法找到解决方案。
我应该能够创建一个程序,将每个数字从给定的整数转换为其等效的单词值。接受的值应仅为UInt16
public extension Int {
public var asWord: String {
let numberValue = NSNumber(value: self)
let formatter = NSNumberFormatter()
formatter.numberStyle = .spellOut
return formatter.string(from: numberValue)!
}
}
var value = 2500
if value > UInt16.max || value < UInt16.min {
print("The accepted value should be UInt16 only")
}
else {
print("\(value.asWord)")
}
答案 0 :(得分:1)
你忘记了导入基金会。
试试这个(Swift 4):
import Foundation
public extension Int {
public var asWord: String {
let numberValue = NSNumber(value: self)
let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
return formatter.string(from: numberValue)!
}
}
var value = 2500
if value > UInt16.max || value < UInt16.min{
print("The accepted value should be UInt16 only")
} else {
print("\(value.asWord)")
}
答案 1 :(得分:0)
你在课堂上使用if block
和value definition
吗?!!
像这样:
public extension Int {
public var asWord: String {
let numberValue = NSNumber(value: self)
let formatter = NSNumberFormatter()
formatter.numberStyle = .spellOut
return formatter.string(from: numberValue)!
}
}
class yourClassName {
var value = 2500
if value > UInt16.max || value < UInt16.min {
print("The accepted value should be UInt16 only")
}
else {
print("\(value.asWord)")
}
}