const keyword in Objective-c
- by user392412
int main(int argc, char *argv[]) {
@autoreleasepool {
const int x = 1;
const NSMutableArray *array1 = [NSMutableArray array];
const NSMutableString *str1 = @"1";
NSString * const str2 = @"2";
// x = 2; compile error
[array1 addObject:@"2"]; // ok
// [str1 appendString:@"2"]; // runtime error
// Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'
// str2 = @"3"; compile error
}
}
my Question is Why array1 addObject is legal and why str1 appendString is forbidden?