tags:

views:

129

answers:

5

hi am working with NSArrays using foundation tool and i have wrote the following code

    -(void)simplearrays
{
 NSMutableArray *arr = [NSMutableArray arrayWithCapacity:3];

 for(int i =0;i<3;i++)
 {
  scanf("%d",&arr[i]);
 }
 for(int j =0; j<3;j++)
 {
  printf("\n%d",arr[j]);
 }
}

My query is that the above code when executed shows the given output but the i get an error once the application finishes execution which says that "can't allocate region" can you please help.

Also i wanted to know the difference between NSArray and NSMutable Array in icode blog i have read that nsarray can dynamically resize so if NSArray can dynamically resize then why to use NSMutable array or a better one would be when to use NSArray and when to use NSMutable Array???

A: 

You use NSMutableArray when you want to add/remove objects from the array.

If you only create array with objects and you don't plan to modify the contents then use NSArray.

You can convert from one to the other.

stefanB
+1  A: 

NSArray and NSMutableArray aren't just synonyms for C arrays, they are container objects with their own interfaces. If you want to insert objects into one, it needs to be an NSMutableArray and you need to call mutator methods such as addObject:. You can't just use the [] operator. Similarly for reading objects out -- you have to go through methods such as objectAtIndex:.

(Note that your code is sort-of syntactically valid, which is why the compiler lets you do it at all -- you're using [] to dereference a pointer. But it is semantically very wrong. It will not do what you want and will likely trash memory in potentially disastrous ways.)

walkytalky
+1  A: 

NSArray and NSMutableArray are Foundation classes and data types that represent the behaviour of an array. You can store any object of non-primitive types in both of them. Both retain the objects they store, and release objects when they are removed or the array object itself is released. When to use which? Well, if you are not likely to add/remove objects to/from the array, you should use NSArray by calling one of the static methods and specifying the objects to store, i.e.:

NSArray *colors = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", nil];

If you are likely to add/remove objects to/from the array after you create it, you should use NSMutableArray. You can create the array with or without specifying the initial objects to store, and then add/remove objects to/from the array anytime, i.e.:

NSMutableArray *colors = [[NSMutableArray alloc] init];
[colors addObject:@"Red"];
[colors addObject:@"Green"];
[colors addObject:@"Blue"];
[colors removeObjectAtIndex:0];
NSLog(@"Color: %@", [colors objectAtIndex:1]);
[colors release];

Check this for more: Collections Programming Topics

ardalahmet
+2  A: 

Cocoa arrays are not C arrays. They are container objects with some similarities to Java vectors and array lists.

You cannot add objects or retrieve them using the C subscript syntax, you need to send messages to the object.

-(void)simplearrays
{
    NSMutableArray *arr = [NSMutableArray array]; 
    // arrayWithCapacity: just gives a hint as to how big the array might become.  It always starts out as
    // size 0.

    for(int i =0;i<3;i++)
    {
        int input;
        scanf("%d",&input);
        [array addObject: [NSNumber numberWithInt: input]];
        // You can't add primitive C types to an NSMutableArray.  You need to box them
        // with an Objective-C object
    }
    for(int j =0; j<3;j++)
    {
       printf("\n%d", [[arr objectAtIndex: j] intValue]);
       // Similarly you need to unbox C types when you retrieve them
    }
    // An alternative to the above loop is to use fast enumeration.  This will be
    // faster because you effectively 'batch up' the accesses to the elements
    for (NSNumber* aNumber in arr)
    {
       printf("\n%d", [aNumber intValue]);
    }
}
JeremyP
Hey Jeremy thanks for posting the answer it helped me a lot but i just wana ask you that is their any way we can use NSArray to accept values from the user at runtime
Radix
No. You need to use NSMutableArray in a way similar to the example above - you can't add new elements to an NSArray. However, there are other ways to get the integer from the input e.g. use NSScanner.
JeremyP
A: 

Thank u every one for your reply i am trying the same with NSArray and hers the code for it

NSArray *arr1 = [[NSArray alloc]init];
int input1;
for(int i =0;i<3;i++)
{
    scanf("%d",&input1);
    arr1 = [NSArray arrayWithObjects:[NSNumber numberWithInt:input1]];
}

The above code takes the user input very well but when i try to display the values then i get an error heres my code for the same

for(int j =0;j<3;j++)
{

    printf("\n%d",[[arr1 objectAtIndex:j]intValue]);
}

Can you please tell me where i am wrong

Radix