What's wrong with my code? It says unresolved Identifier

a 夏天 提交于 2019-12-13 03:29:37

问题


It says "use of unresolved identifier" for both NSNumber and NSNumber. I can't find a solution for this.

I should be able to create a program that will convert each number from a given integer to its equivalent word value. The accepted value should be UInt16 only

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)")
}

回答1:


You forgot import Foundation.

Try this (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)")
}



回答2:


Do you use the if block and value definition in a class?!! like this:

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)")
    }
}


来源:https://stackoverflow.com/questions/50132685/whats-wrong-with-my-code-it-says-unresolved-identifier

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!