Passing NSInteger variable to NSMutableDictionary or NSMutableArray

后端 未结 7 931
北恋
北恋 2020-12-28 12:42

Why does this not work:

NSInteger temp = 20;
[userSettingsFromFile setObject:temp forKey:@\"aTemp\"];

but this does:

[userS         


        
7条回答
  •  不思量自难忘°
    2020-12-28 12:59

    NSInteger is synonym for long integer.What follows is how NSInteger is defined:

    #if __LP64__ || NS_BUILD_32_LIKE_64
    typedef long NSInteger;
    typedef unsigned long NSUInteger;
    #else
    typedef int NSInteger;
    typedef unsigned int NSUInteger;
    #endif
    

    NSNumber is an Objective-C class, a subclass of NSValue to be specific. You can create an NSNumber object from a signed or unsigned char, short int, int, long int, long long int, float, double or BOOL

    One of the primary distinctions is that you can use NSNumber in collections, such as NSArray, where an object is required. For example, if you need to add a float into an NSArray, you would first need to create an NSNumber object from the float:

    float percentage = 40.5;
    

    ... // Create NSNumber object, which can now be inserted into an NSArray

    NSNumber *percentageObject = [NSNumber numberWithFloat:percentage];
    

提交回复
热议问题