我的代码出了什么问题?它说未解决的标识符

时间:2018-05-02 10:41:05

标签: swift nsnumber

它为NSNumberNSNumber说“使用未解析的标识符”。我无法找到解决方案。

我应该能够创建一个程序,将每个数字从给定的整数转换为其等效的单词值。接受的值应仅为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)")
}

2 个答案:

答案 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 blockvalue 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)")
    }
}