ARC下实现一个NSSting类的简单探究

在非ARC下实现一个NSString类的实现代码如下:

+ (id)initWithCString:(c*****t char *)nullTerminatedCString encoding:(NSStringEncoding)encoding; 

+ (id) stringWithCString: (c*****t char*)nullTerminatedCString  
            encoding: (NSStringEncoding)encoding 
{ 
  NSString  *obj; 
  obj = [self allocWithZone: NSDefaultMallocZone()]; 
  obj = [obj initWithCString: nullTerminatedCString encoding: encoding]; 
  return AUTORELEASE(obj);    //这是比较老的代码了,反正我是没能实现
} 

在ARC下的实现代码

+(id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;

+(id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc
{
    NSString *obj;
    obj = [NSString alloc];
    obj = [obj initWithCString:cString encoding:enc];
    return  obj;
}

ARC下实现一个NSSting类的简单探究_第1张图片

 当sting是9位数字时,sting的类型是NSTaggedPointerString,而当string是10位数时,string是__NSCFString类型,NSTaggedPointerString可以提高性能,节省内存,详细讲解见:  http://blog.csdn.net/demondev/article/details/50396815
  [NSString string] //返回的实例是一个__NSCFConstantString类型的对象

你可能感兴趣的:(ios,ios,nsstring,ARC)