Difference between NSArray and NSMutable Array

后端 未结 5 1548
北海茫月
北海茫月 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.)

提交回复
热议问题