get type of NSNumber

后端 未结 12 580
鱼传尺愫
鱼传尺愫 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 03:10

    I recommend using the -[NSNumber objCType] method.

    It allows you to do:

    NSNumber * n = [NSNumber numberWithBool:YES];
    if (strcmp([n objCType], @encode(BOOL)) == 0) {
        NSLog(@"this is a bool");
    } else if (strcmp([n objCType], @encode(int)) == 0) {
        NSLog(@"this is an int");
    }
    

    For more information on type encodings, check out the Objective-C Runtime Reference.

提交回复
热议问题