开发者

Difference between NSArray and NSMutable Array

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 fini开发者_如何学运维shes 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???


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


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


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.


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.)


The basic difference is that you cant add an object to NSArray. So if you want to have an array to which you want to add an object you have to use NSMutable array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜