Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making an array of integers in iOS

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

like image 323
neuromancer Avatar asked Jul 27 '10 01:07

neuromancer


People also ask

What is an array in IOS?

Arrays are one of the most commonly used data types in an app. You use arrays to organize your app's data. Specifically, you use the Array type to hold elements of a single type, the array's Element type. An array can store any kind of elements—from integers to strings to classes.

How do you declare an int array in Swift?

Array in swift is written as **Array < Element > **, where Element is the type of values the array is allowed to store. The type of the emptyArray variable is inferred to be [String] from the type of the initializer. The groceryList variable is declared as “an array of string values”, written as [String].

How do you create an integer array in Objective-C?

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]);

What is a Swift array?

Swift is a type inference language that is, it can automatically identify the data type of an array based on its values. Hence, we can create arrays without specifying the data type. For example, var numbers = [2, 4, 6, 8] print("Array: \(numbers)") // [2, 4, 6, 8]


2 Answers

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]); 
like image 160
dreamlax Avatar answered Oct 15 '22 23:10

dreamlax


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 
like image 45
Frédéric Adda Avatar answered Oct 15 '22 22:10

Frédéric Adda