Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append values to an array in Objective-C

I'm doing this:

for(int i=0;i>count;i++) {     NSArray *temp=[[NSArray alloc]initWIthObjects]i,nil];     NSLog(@"%i",temp); } 

It returns to me 0,1,2,3....counting one by one, but I want an array with appending these values {0,1,2,3,4,5...}. This is not a big deal, but I'm unable to find it. I am new to iPhone.

like image 699
user440485 Avatar asked Sep 13 '10 06:09

user440485


People also ask

How do I add values to an array in Objective C?

The mutable version is NSMutableArray , which will allow you to append values. You can't add primitives like int to an NSArray or NSMutableArray class; they only hold objects. The NSNumber class is designed for this situation. You are leaking memory each time you are allocating an array.

Can you add more elements to an array in C?

C # in TeluguWe can insert the elements wherever we want, which means we can insert either at starting position or at the middle or at last or anywhere in the array. After inserting the element in the array, the positions or index location is increased but it does not mean the size of the array is increasing.

How do I create an NSArray in Objective C?

Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.

What is NSMutableArray Objective C?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .


2 Answers

NSMutableArray *myArray = [NSMutableArray array];  for(int i = 0; i < 10; i++) {    [myArray addObject:@(i)]; }  NSLog(@"myArray:\n%@", myArray); 
like image 137
vodkhang Avatar answered Oct 24 '22 13:10

vodkhang


This code is not doing what you want it to do for several reasons:

  1. NSArray is not a "mutable" class, meaning it's not designed to be modified after it's created. The mutable version is NSMutableArray, which will allow you to append values.

  2. You can't add primitives like int to an NSArray or NSMutableArray class; they only hold objects. The NSNumber class is designed for this situation.

  3. You are leaking memory each time you are allocating an array. Always pair each call to alloc with a matching call to release or autorelease.

The code you want is something like this:

NSMutableArray* array = [[NSMutableArray alloc] init]; for (int i = 0; i < count; i++) {     NSNumber* number = [NSNumber numberWithInt:i]; // <-- autoreleased, so you don't need to release it yourself     [array addObject:number];     NSLog(@"%i", i); } ... [array release]; // Don't forget to release the object after you're done with it 

My advice to you is to read the Cocoa Fundamentals Guide to understand some of the basics.

like image 36
Shaggy Frog Avatar answered Oct 24 '22 13:10

Shaggy Frog