你的位置:首页 > 操作系统

[操作系统]MRC下NSNumber,NSString,及NSArray,NSMutableArray的引用计数,个人学习


1、提问:甲乙丙分别输出的retainCount为多少?

    NSMutableArray* ary = [[NSMutableArray array] retain];

    NSString *str = [NSString stringWithFormat:@"test"];//1 __NSCFString,  <Foundation/Foundation.h>框架下-1  NSTaggedPointerString,如果长度超过一定范围或者为中文时,引用计数为1__NSCFString

 

          ---此处特别说明,当 NSString *str = @“test”;这种方式创建时 __NSCFConstantString,引用计数在 <Foundation/Foundation.h>框架下 -1,否则

              18446744073709551615

 

    [str retain];//+1

    [ary addObject:str];//+1

    NSLog(@"甲------%@%ld",str,[str retainCount]);//3

    [str retain];//+1

    [str release];//-1

    [str release];//-1

    NSLog(@"乙------%@%ld",str,[str retainCount]);//2

    [ary removeAllObjects];//-1

    NSLog(@"丙------%@%ld",str,[str retainCount]);//1

 

2、NSArray,NSMutableArray

  NSArray * array = [NSArray array];// -1  __NSArray0

    [array retain];//-1

    NSArray * array1 = [NSArray  arrayWithObject:@"1"];//1  __NSArrayI

   NSMutableArray * array3 = [NSMutableArray array];//1  __NSArrayM

    NSMutableArray * array4 = [NSMutableArray arrayWithObject:@"1"];//1 __NSArrayM

 

3、NSNumber

    NSNumber *num1 = @(100);// __NSCFNumber9223372036854775807

    NSNumber *num2 = [[NSNumber alloc] initWithInt:1000000];// __NSCFNumber9223372036854775807

 

以上关于NSString的解释,因为会出现多种情况,欢迎交流。