const keyword in Objective-c

Posted by user392412 on Stack Overflow See other posts from Stack Overflow or by user392412
Published on 2012-10-29T16:54:33Z Indexed on 2012/10/29 17:00 UTC
Read the original article Hit count: 178

Filed under:
|
 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?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about const