Difference between NSArray and NSMutable Array

后端 未结 5 1523
北海茫月
北海茫月 2021-01-07 04:44

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

    -(void)simplearrays
{
 NSMutableArray *arr = [NSMutableArray array         


        
相关标签:
5条回答
  • 2021-01-07 05:10

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

    0 讨论(0)
  • 2021-01-07 05:19

    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]);
        }
    }
    
    0 讨论(0)
  • 2021-01-07 05:25

    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.

    0 讨论(0)
  • 2021-01-07 05:26

    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

    0 讨论(0)
  • 2021-01-07 05:30

    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 讨论(0)
提交回复
热议问题