Passing NSInteger variable to NSMutableDictionary or NSMutableArray

后端 未结 7 890
北恋
北恋 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:52

    In order to store numbers in collections, you have to wrap them up in an NSNumber instance.

    double aDouble = 20.3d;
    NSInteger anInt = 20;
    
    NSNumber *aWrappedDouble = [NSNumber numberWithDouble:aDouble];
    NSNumber *aWrappedInt = [NSNumber numberWithInteger:anInt];
    
    NSArray *anArray = [NSArray arrayWithObjects:aWrappedDouble, aWrappedInt, nil];
    
    0 讨论(0)
  • 2020-12-28 12:52

    Whatever you pass through setObject has to be derived from NSObject. NSInteger is not, it's a simple int typedef. In your 2nd example you use NSString, which is derived from NSObject.

    0 讨论(0)
  • 2020-12-28 12:54

    Referred to @Matt Ball's answer: NSInteger isn't an object -- it's simply typecast to int on 32-bit or long on 64-bit. Since NSDictionary can only store objects, you need to wrap the integer into an object before you can store it.

    So, in case of Swift, this is how you do it:

    let temp:NSInteger = yourInteger.hashValue
    
    0 讨论(0)
  • 2020-12-28 12:57

    Correction: Whatever is passed through setObject: do not have to be derived from the NSObject class, but it must conform to the NSObject protocol, that defines retain and release.

    It can be confusing, but classes and protocols have different name spaces. And there is a both a class and a protocol named NSObject, the class NSObject conforms to the protocol NSObject. There is one more root class, the NSProxy class that also conforms to the NSObject protocol.

    This is important, because otherwise proxies could not be used in collections and auto release pools, while still having a lightweight proxy root class.

    0 讨论(0)
  • 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];
    
    0 讨论(0)
  • 2020-12-28 13:00

    just use: $(variable) syntax to convert primitive type to object.

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