get type of NSNumber

后端 未结 12 582
鱼传尺愫
鱼传尺愫 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:58

    NSNumber explicitly doesn't guarantee that the returned type will match the method used to create it, so doing this at all is probably a bad idea.

    However, you could probably do something like this (you could also compare to objc_getClass("NSCFNumber") etc., but this is arguably more portable):

    Class boolClass = [[NSNumber numberWithBool:YES] class];
    /* ... */
    if([myNum isKindOfClass:boolClass]) {
      /* ... */
    }
    
    0 讨论(0)
  • 2020-11-29 03:00

    To check that NSNumber contains a bool value Try this:

    if (strcmp([myNumber objCType], [@(YES) objCType]) == 0)
    NSLog(@"%@", [myNumber boolValue] ? @"true" : @"false");
    
    0 讨论(0)
  • 2020-11-29 03:02

    Use the method -[NSNumber objCType] method to get the type.

    If the type's equal to @encode(BOOL), or the number itself is kCFBooleanFalse, or kCFBooleanTrue, it's a boolean.

    If it's anything else but 'c', it's a number.

    If it's 'c', what appears to be the only way supported way, without checking against private class names, or comparing against undocumented singletons, is to turn make an array of one element, the number, and then use NSJSONSerialization to get the string representation. Finally, check if the string representation contains the string "true" or "false". Here is the full code for checking if an NSNumber is a BOOL:

    -(BOOL)isBool
    {
        if(!strcmp(self.objCType, @encode(BOOL)) ||
            self == (void*)kCFBooleanFalse ||
            self == (void*)kCFBooleanTrue)
        {
            return YES;
        }
    
        if(strcmp(self.objCType, "c"))
        {
            return NO;
        }
    
        NSString * asString = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:@[self] options:kNilOptions error:nil] encoding:NSUTF8StringEncoding];
    
        return [asString containsString:@"true"] || [asString containsString:@"false"];
    }
    

    Note that using NSJSONSerialization is slow and if @NO/@YES ever stops always equalling kCFBooleanFalse/kCFBooleanTrue, then this method probably shouldn't be used in a tight loop.

    0 讨论(0)
  • 2020-11-29 03:04

    If all you want is to differentiate between booleans and anything else, you can make use of the fact that boolean NSNumbers always return a shared instance:

    NSNumber *num = ...;
    if (num == (void*)kCFBooleanFalse || num == (void*)kCFBooleanTrue) {
       // num is boolean
    } else {
       // num is not boolean
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 03:10
    NSString *classString = NSStringFromClass([myNum class]);
    

    That should ger the string you want.

    0 讨论(0)
提交回复
热议问题