get type of NSNumber

后端 未结 12 583
鱼传尺愫
鱼传尺愫 2020-11-29 02:33

I want to get the type of NSNumber instance.

I found out on http://www.cocoadev.com/index.pl?NSNumber this:

 NSNumber *myNum = [[NSNumber alloc] initWithB         


        
12条回答
  •  执笔经年
    2020-11-29 02:47

    Swift Version

    NSNumber is a class-cluster so each underlying type can be figured from the instance. This code avoids hard-coding the different NSNumber types by creating an instance of the expected type, and then comparing it against the unknown type.

    extension NSNumber {
        var isBool: Bool {
            return type(of: self) == type(of: NSNumber(booleanLiteral: true))
        }
    }
    

提交回复
热议问题