Making an array of integers in iOS

后端 未结 7 1977
悲哀的现实
悲哀的现实 2020-12-07 12:17

If you want to make an array of integers, can you use NSInteger? Do you have to use NSNumber? If so, then why?

相关标签:
7条回答
  • 2020-12-07 12:24

    C array:

    NSInteger array[6] = {1, 2, 3, 4, 5, 6};
    

    Objective-C Array:

    NSArray *array = @[@1, @2, @3, @4, @5, @6];
    // numeric values must in that case be wrapped into NSNumbers
    

    Swift Array:

    var array = [1, 2, 3, 4, 5, 6]
    

    This is correct too:

    var array = Array(1...10)
    

    NB: arrays are strongly typed in Swift; in that case, the compiler infers from the content that the array is an array of integers. You could use this explicit-type syntax, too:

    var array: [Int] = [1, 2, 3, 4, 5, 6]
    

    If you wanted an array of Doubles, you would use :

    var array = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] // implicit type-inference
    

    or:

    var array: [Double] = [1, 2, 3, 4, 5, 6] // explicit type
    
    0 讨论(0)
  • 2020-12-07 12:24

    If the order of your integers is not required, and if there are only unique values

    you can also use NSIndexSet or NSMutableIndexSet You will be able to easily add and remove integers, or check if your array contains an integer with

    - (void)addIndex:(NSUInteger)index
    - (void)removeIndex:(NSUInteger)index
    - (BOOL)containsIndexes:(NSIndexSet *)indexSet
    

    Check the documentation for more info.

    0 讨论(0)
  • 2020-12-07 12:30

    You can use a plain old C array:

    NSInteger myIntegers[40];
    
    for (NSInteger i = 0; i < 40; i++)
        myIntegers[i] = i;
    
    // to get one of them
    NSLog (@"The 4th integer is: %d", myIntegers[3]);
    

    Or, you can use an NSArray or NSMutableArray, but here you will need to wrap up each integer inside an NSNumber instance (because NSArray objects are designed to hold class instances).

    NSMutableArray *myIntegers = [NSMutableArray array];
    
    for (NSInteger i = 0; i < 40; i++)
        [myIntegers addObject:[NSNumber numberWithInteger:i]];
    
    // to get one of them
    NSLog (@"The 4th integer is: %@", [myIntegers objectAtIndex:3]);
    
    // or
    NSLog (@"The 4th integer is: %d", [[myIntegers objectAtIndex:3] integerValue]);
    
    0 讨论(0)
  • 2020-12-07 12:31

    I created a simple Objective C wrapper around the good old C array to be used more conveniently: https://gist.github.com/4705733

    0 讨论(0)
  • 2020-12-07 12:32

    I think it's a lot easier to use NSNumbers. This all you need to do:

    NSNumber *myNum1 = [NSNumber numberWithInt:myNsIntValue1];
    NSNumber *myNum2 = [NSNumber numberWithInt:myNsIntValue2];
    .
    .
    .
    NSArray *myArray = [NSArray arrayWithObjects: myNum1, myNum2, ..., nil];
    
    0 讨论(0)
  • 2020-12-07 12:44

    If you want to use a NSArray, you need an Objective-C class to put in it - hence the NSNumber requirement.

    That said, Obj-C is still C, so you can use regular C arrays and hold regular ints instead of NSNumbers if you need to.

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