OBJECTIVE-C:MRC (reference counter) variable objects within OC are applicable, immutable objects are not applicable (e.g. NSString, nsarray, etc.)

Source: Internet
Author: User


reference counts and stringsThe spatial allocation of constant strings in memory is different from other objects, they do not have a reference counting mechanismall custom objects have a reference counting mechanism;objects within OC are divided into mutable objects (nsmutablestring, etc.) and immutable objects (NSString, Nsarray, etc.).immutable objects do not apply to the mechanism of reference counting, and variable objects apply a reference counting mechanism.  
1 // main.m
 2 // 03-unmutableobject
 3 //
 4 // Created by ma c on 15/8/13.
 5 // Copyright (c) 2015 bjsxt. All rights reserved.
 6 //
 7
 8 #import <Foundation/Foundation.h>
 9 
10 int main(int argc, const char * argv[])
11 {
12 @autoreleasepool
13 {
14 // immutable object does not apply to reference counting
15 //1. Create 3 objects
16 NSString *str1 = @"hello world";//immutable object
17
18 NSString *str2 = [NSString stringWithString:@"how arer you"];//immutable object
19
20 NSMutableString *str3 = [NSMutableString stringWithString:@"I am fine"];//Variable object
twenty one         
22 NSLog(@"str1 = %lu,str2 = %lu,str3 = %lu",[str1 retainCount],[str2 retainCount],[str3 retainCount]);
twenty three         
twenty four         
25 //2. Add 3 objects to the mutable array
26 NSMutableArray *array = [NSMutableArray array];
27 [array addObject:str1];
28 [array addObject:str2];
29 [array addObject:str3];
30 NSLog(@"str1 = %lu,str2 = %lu,str3 = %lu",[str1 retainCount],[str2 retainCount],[str3 retainCount]);
31
32
33 //3. Retaining 3 objects
34 [str1 retain];
35 [str2 retain];
36 [str3 retain];
37 NSLog(@"str1 = %lu,str2 = %lu,str3 = %lu",[str1 retainCount],[str2 retainCount],[str3 retainCount]);
38
39 //4. Remove 3 objects from the array
40 [array removeObject:str1];
41 [array removeObject:str2];
42 [array removeObject:str3];
43 NSLog(@"str1 = %lu,str2 = %lu,str3 = %lu",[str1 retainCount],[str2 retainCount],[str3 retainCount]);
44
45 //5. Release operation on the object --- corresponding to the above retain operation
46 [str1 release];
47 [str2 release];
48 [str3 release];
49 NSLog(@"str1 = %lu,str2 = %lu,str3 = %lu",[str1 retainCount],[str2 retainCount],[str3 retainCount]);
50
51
52 //6. Release the object - corresponding to the above object creation operation (to avoid memory leaks)
53 [str1 release];
54 [str2 release];
55 [str3 release];
56 }
57 return 0;
58 } 





    The test results are:


 
2015-08-13 17:41:26.569 03-unmutableobject[1622:105614] str1 = 18446744073709551615,str2 = 18446744073709551615,str3 = 1
2015-08-13 17:41:26.570 03-unmutableobject[1622:105614] str1 = 18446744073709551615,str2 = 18446744073709551615,str3 = 2
2015-08-13 17:41:26.571 03-unmutableobject[1622:105614] str1 = 18446744073709551615,str2 = 18446744073709551615,str3 = 3
2015-08-13 17:41:26.571 03-unmutableobject[1622:105614] str1 = 18446744073709551615,str2 = 18446744073709551615,str3 = 2
2015-08-13 17:41:26.571 03-unmutableobject[1622:105614] str1 = 18446744073709551615,str2 = 18446744073709551615,str3 = 1
Program ended with exit code: 0





Note: It is obvious that nsstring is not applicable to the reference counting mechanism.



OBJECTIVE-C:MRC (reference counter) variable objects within OC are applicable, immutable objects are not applicable (e.g. NSString, nsarray, etc.)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.