美文网首页
Class Cluster

Class Cluster

作者: MAXcrazs | 来源:发表于2021-04-14 22:47 被阅读0次

类簇实际上是Foundation framework框架下的一种设计模式,它管理了一组隐藏在公共接口下的私有类。

NSNumber

void Cluster_NumberTest(){
    NSNumber *n1 = [NSNumber alloc];
    NSNumber *n2 = [NSNumber alloc];
    
    NSLog(@"n1:%p, n2:%p", n1, n2);
    >>> n1:0x7fff803cd610, n2:0x7fff803cd610
      
    NSLog(@"n1:%@, n2:%@", NSStringFromClass([n1 class]), NSStringFromClass([n2 class]));
    >>> n1:NSPlaceholderNumber, n2:NSPlaceholderNumber
      
    NSNumber *n3 = [n1 init];
    NSNumber *n4 = [n2 init];
 
    NSLog(@"n3:%p, n4:%p", n3, n4);
    >>> n3:0x0, n4:0x0
    NSLog(@"n3:%@, n4:%@", NSStringFromClass([n3 class]), NSStringFromClass([n4 class]));
    >>> n3:(null), n4:(null)
  
  
        NSNumber *n5 = [n1 initWithInt:10];
    NSNumber *n6 = [n1 initWithInt:11];
    NSNumber *n7 = [n1 initWithBool:YES];
    
    NSLog(@"n5:%p, n6:%p, n7:%p", n5, n6, n7);
    >>> n5:0x93e948d562ffb823, n6:0x93e948d562ffb923, n7:0x7fff8009e710
    NSLog(@"n5:%@, n6:%@, n7:%@", NSStringFromClass([n5 class]), NSStringFromClass([n6 class]), NSStringFromClass([n7 class]));
    >>> n5:__NSCFNumber, n6:__NSCFNumber, n7:__NSCFBoolean
    
    NSNumber *n8 = [n2 initWithChar:'a'];
    NSNumber *n9 = [n2 initWithFloat:10.0f];
    NSLog(@"n8:%p, n9:%p", n8, n9);
    >>> n8:0x93e948d562ffd303, n9:0x93e948d562ffb843
    NSLog(@"n8:%@, n9:%@", NSStringFromClass([n8 class]), NSStringFromClass([n9 class]));
    >>> n8:__NSCFNumber, n9:__NSCFNumber
      
    NSLog(@"n1:%d, n3:%d, n5:%d, n7:%d", [n1 isKindOfClass:[NSNumber class]], [n3 isKindOfClass:[NSNumber class]], [n5 isKindOfClass:[NSNumber class]], [n7 isKindOfClass:[NSNumber class]]);
    >>> n1:1, n3:0, n5:1, n7:1
    NSLog(@"n1:%d, n3:%d, n5:%d, n7:%d", [n1 isMemberOfClass:[NSNumber class]], [n3 isMemberOfClass:[NSNumber class]], [n5 isMemberOfClass:[NSNumber class]], [n7 isMemberOfClass:[NSNumber class]]);
        >>> n1:0, n3:0, n5:0, n7:0
}

结论

  • [Number alloc] 返回一个NSPlaceholderNumber对象,且地址相同。
  • 单纯的[[NSNumber alloc] init]没有意义
  • NSNumber在使用- initWithxxx创建时,才会”真正”创建一个“NSNumber”对象
  • 对象的- isKindOfClass返回YES
  • 对象的- isMemberOfClass返回NO,在使用时需要谨慎

NSArray

void Cluster_ArrayTest(void) {
    
    NSArray *a1 = [NSArray alloc];
    NSArray *a2 = [NSArray alloc];
    
    NSLog(@"a1:%p, a2:%p", a1, a2);
    >>> a1:0x7fff801a7610, a2:0x7fff801a7610
    NSLog(@"a1:%@, a2:%@", NSStringFromClass([a1 class]), NSStringFromClass([a2 class]));
    >>> a1:__NSPlaceholderArray, a2:__NSPlaceholderArray  
  
    NSArray *a3 = [a1 init];
    NSArray *a4 = [a2 init];
    
    NSLog(@"a3:%p, a4:%p", a3, a4);
    >>> a3:0x7fff8003d7f0, a4:0x7fff8003d7f0
    NSLog(@"a3:%@, a4:%@", NSStringFromClass([a3 class]), NSStringFromClass([a4 class]));
    >>> a3:__NSArray0, a4:__NSArray0
    
    NSArray *a5 = [a1 initWithObjects:@"qwe", nil];
    NSArray *a6 = [a2 initWithObjects:@"qwe", nil];
    NSLog(@"a5:%p, a6:%p", a5, a6);
    >>> a5:0x10592b3e0, a6:0x10592b400
    NSLog(@"a5:%@, a6:%@", NSStringFromClass([a5 class]), NSStringFromClass([a6 class]));
        >>> a5:__NSArrayI, a6:__NSArrayI
  
    NSString *string[3];
    string[0] = @"qwe";
    string[1] = @"asd";
    string[2] = @"zxc";
    NSArray *a7 = [a1 initWithObjects:string count:1];
    NSArray *a8 = [a1 initWithObjects:string count:2];
    NSArray *a9 = [a1 initWithObjects:@"qwe", @"asd", nil];

    NSLog(@"a7:%p, a8:%p, a9:%p", a7, a8, a9);
        >>> a7:0x10592b7b0, a8:0x10592b460, a9:0x10592b420
    NSLog(@"a7:%@, a8:%@, a9:%@", NSStringFromClass([a7 class]), NSStringFromClass([a8 class]), NSStringFromClass([a9 class]));
    >>> a7:__NSSingleObjectArrayI, a8:__NSArrayI, a9:__NSArrayI
    
    
    NSArray *a10 = @[@"qwe"];
    NSArray *a11 = @[@"qwe", @"asd"];
    NSLog(@"a10:%p, a11:%p", a10, a11);
    >>> a10:0x10592b840, a11:0x10592b440
    NSLog(@"a10:%@, a11:%@", NSStringFromClass([a10 class]), NSStringFromClass([a11 class]));
        >>> a10:__NSSingleObjectArrayI, a11:__NSArrayI
      
    NSLog(@"a1:%d, a3:%d, a5:%d, a7:%d, a8:%d", [a1 isKindOfClass:[NSArray class]], [a3 isKindOfClass:[NSArray class]], [a5 isKindOfClass:[NSArray class]], [a7 isKindOfClass:[NSArray class]], [a8 isKindOfClass:[NSArray class]]);
    >>> a1:1, a3:1, a5:1, a7:1, a8:1

    NSLog(@"a1:%d, a3:%d, a5:%d, a7:%d, a8:%d", [a1 isMemberOfClass:[NSArray class]], [a3 isMemberOfClass:[NSArray class]], [a5 isMemberOfClass:[NSArray class]], [a7 isMemberOfClass:[NSArray class]], [a8 isMemberOfClass:[NSArray class]]);
    >>> a1:0, a3:0, a5:0, a7:0, a8:0
      
    // 下接MutableArray
}

结论

  • [NSArray alloc] 返回一个__NSPlaceholderArray对象,且地址相同。
  • 单纯的[[NSArray alloc] init]返回一个__NSArray0对象,地址相同
  • NSArray在使用- initWithObjects创建时,会返回一个__NSArrayI对象。
  • NSArray在使用- initWithObjects:count:创建,如果count=1,返回__NSSingleObjectArrayI对象;count>1返回__NSArrayI对象
  • 使用语法糖@[]创建NSArray对象,如果只有一个元素,会创建__NSSingleObjectArrayI对象,否则创建__NSArrayI对象
  • 对象的- isKindOfClass返回YES
  • 对象的- isMemberOfClass返回NO,在使用时需要谨慎
        // 上接Array
        NSMutableArray *ma1 = [NSMutableArray alloc];
    NSMutableArray *ma2 = [NSMutableArray alloc];
    
    NSLog(@"ma1:%p, ma2:%p", ma1, ma1);
        >>> ma1:0x7fff801a7620, ma2:0x7fff801a7620
    NSLog(@"ma1:%@, ma2:%@", NSStringFromClass([ma1 class]), NSStringFromClass([ma2 class]));
    >>> ma1:__NSPlaceholderArray, ma2:__NSPlaceholderArray
      
    NSMutableArray *ma3 = [ma1 init];
    NSMutableArray *ma4 = [ma2 init];
    
    NSLog(@"ma3:%p, ma4:%p", ma3, ma4);
        >>> ma3:0x105b04f80, ma4:0x105b04fb0
    NSLog(@"ma3:%@, ma4:%@", NSStringFromClass([ma3 class]), NSStringFromClass([ma4 class]));
    >>> ma3:__NSArrayM, ma4:__NSArrayM

    NSMutableArray *ma5 = [ma1 initWithObjects:obj, nil];
    NSMutableArray *ma6 = [ma2 initWithObjects:obj, nil];
    NSLog(@"ma5:%p, ma6:%p", ma5, ma6);
        >>> ma5:0x105b04f50, ma6:0x105b04fe0
    NSLog(@"ma5:%@, ma6:%@", NSStringFromClass([ma5 class]), NSStringFromClass([ma6 class]));
        >>> ma5:__NSArrayM, ma6:__NSArrayM      

    NSMutableArray *ma7 = [ma1 initWithObjects:string count:1];
    NSMutableArray *ma8 = [ma1 initWithObjects:string count:2];
    NSMutableArray *ma9 = [ma1 initWithObjects:@"qwe", @"asd", nil];

    NSLog(@"ma7:%p, ma8:%p, ma9:%p", ma7, ma8, a9);
        >>> ma7:0x105b04f10, ma8:0x105b05010, ma9:0x105805380
    NSLog(@"ma7:%@, ma8:%@, ma9:%@", NSStringFromClass([ma7 class]), NSStringFromClass([ma8 class]), NSStringFromClass([ma9 class]));
        >>> ma7:__NSArrayM, ma8:__NSArrayM, ma9:__NSArrayM
    
    NSMutableArray *ma10 = [ma1 initWithArray:ma5];
    NSMutableArray *ma11 = [ma2 initWithArray:ma6];
    NSLog(@"ma10:%p, ma11:%p", ma10, ma11);
        >>> ma10:0x1006064a0, ma11:0x100690220
    NSLog(@"ma10:%@, ma11:%@", NSStringFromClass([ma10 class]), NSStringFromClass([ma11 class]));
        >>> ma10:__NSArrayM, ma11:__NSArrayM
    
    NSLog(@"ma1:%d, ma3:%d, ma5:%d, ma7:%d, ma8:%d", [ma1 isKindOfClass:[NSMutableArray class]], [ma3 isKindOfClass:[NSMutableArray class]], [ma5 isKindOfClass:[NSMutableArray class]], [ma7 isKindOfClass:[NSMutableArray class]], [ma8 isKindOfClass:[NSMutableArray class]]);
        >>> ma1:1, ma3:1, ma5:1, ma7:1, ma8:1
    NSLog(@"ma1:%d, ma3:%d, ma5:%d, ma7:%d, ma8:%d", [ma1 isMemberOfClass:[NSMutableArray class]], [ma3 isMemberOfClass:[NSMutableArray class]], [ma5 isMemberOfClass:[NSMutableArray class]], [ma7 isMemberOfClass:[NSMutableArray class]], [ma8 isMemberOfClass:[NSMutableArray class]]);
        >>> ma1:0, ma3:0, ma5:0, ma7:0, ma8:0
    

结论

  • [NSMutableArray alloc] 返回一个__NSPlaceholderArray对象,且地址相同。
    • 虽然都是__NSPlaceholderArray类型,但是和[NSArray alloc]返回的对象地址不同
  • [[NSMutableArray alloc] init]返回一个__NSArrayM对象,地址不相同,这非常服务预期
  • NSMutableArray在无论初始化是否有元素,创建的对象都是__NSArrayM
  • 对象的- isKindOfClass返回YES
  • 对象的- isMemberOfClass返回NO,在使用时需要谨慎

NSDictionary

void Cluster_DictionaryTest(void) {
    
    NSDictionary *d1 = [NSDictionary alloc];
    NSDictionary *d2 = [NSDictionary alloc];
    
    NSLog(@"d1:%p, d2:%p", d1, d2);
    >>> d1:0x7fff801e7550, d2:0x7fff801e7550
    NSLog(@"d1:%@, d2:%@", NSStringFromClass([d1 class]), NSStringFromClass([d2 class]));
    >>> d1:__NSPlaceholderDictionary, d2:__NSPlaceholderDictionary  
  
    NSDictionary *d3 = [d1 init];
    NSDictionary *d4 = [d2 init];
    
    NSLog(@"d3:%p, d4:%p", d3, d4);
    >>> d3:0x7fff801ae5f0, d4:0x7fff801ae5f0
    NSLog(@"d3:%@, d4:%@", NSStringFromClass([d3 class]), NSStringFromClass([d4 class]));
    >>> d3:__NSDictionary0, d4:__NSDictionary0  
  
    CommonClass *obj = [[CommonClass alloc] init];
    NSArray *objs = @[obj];
    NSArray *keys = @[@"qwe"];
    NSDictionary *d5 = [d1 initWithObjects:objs forKeys:keys];
    NSDictionary *d6 = [d2 initWithObjects:objs forKeys:keys];
    NSLog(@"d5:%p, d6:%p", d5, d6);
    >>> d5:0x100681850, d6:0x100681870
    NSLog(@"d5:%@, d6:%@", NSStringFromClass([d5 class]), NSStringFromClass([d6 class]));
        >>> d5:__NSSingleEntryDictionaryI, d6:__NSSingleEntryDictionaryI
  
    NSString *string[3];
    string[0] = @"qwe";
    string[1] = @"asd";
    string[2] = @"zxc";
    NSDictionary *d7 = [d1 initWithObjects:string forKeys:string count:1];
    NSDictionary *d8 = [d1 initWithObjects:string forKeys:string count:2];
    NSDictionary *d9 = [d1 initWithObjects:@[@"qwe", @"asd"] forKeys:@[@"qwe", @"asd"]];

    NSLog(@"d7:%p, d8:%p, d9:%p", d7, d8, d9);
    >>> d7:0x1006818d0, d8:0x1006818f0, d9:0x100681a70
    NSLog(@"d7:%@, d8:%@, d9:%@", NSStringFromClass([d7 class]), NSStringFromClass([d8 class]), NSStringFromClass([d9 class]));
    >>> d7:__NSSingleEntryDictionaryI, d8:__NSDictionaryI, d9:__NSDictionaryI
    
    NSDictionary *d10 = @{@"qwe":@"qwe"};
    NSDictionary *d11 = @{@"qwe":@"qwe", @"asd":@"asd"};
    NSLog(@"d10:%p, d11:%p", d10, d11);
    >>> d10:0x100746250, d11:0x100746270
    NSLog(@"d10:%@, d11:%@", NSStringFromClass([d10 class]), NSStringFromClass([d11 class]));
        >>> d10:__NSSingleEntryDictionaryI, d11:__NSDictionaryI
    
    NSLog(@"d1:%d, d3:%d, d5:%d, d7:%d, d8:%d", [d1 isKindOfClass:[NSDictionary class]], [d3 isKindOfClass:[NSDictionary class]], [d5 isKindOfClass:[NSDictionary class]], [d7 isKindOfClass:[NSDictionary class]], [d8 isKindOfClass:[NSDictionary class]]);
    >>> d1:1, d3:1, d5:1, d7:1, d8:1
    NSLog(@"d1:%d, d3:%d, d5:%d, d7:%d, d8:%d", [d1 isMemberOfClass:[NSDictionary class]], [d3 isMemberOfClass:[NSDictionary class]], [d5 isMemberOfClass:[NSDictionary class]], [d7 isMemberOfClass:[NSDictionary class]], [d8 isMemberOfClass:[NSDictionary class]]);
        >>> d1:0, d3:0, d5:0, d7:0, d8:0
      
    // 下接MutableDictionary
}

结论

  • [NSDictionary alloc] 返回一个__NSPlaceholderDictionary对象,且地址相同。
  • 单纯的[[NSDictionary alloc] init]返回一个__NSDictionary0对象,地址相同
  • NSDictionary在使用- initWithObjects:forKeys:- initWithObjects:forKeys:count:创建时,如果只有一组keyValue,返回一__NSSingleEntryDictionaryI类型;keyValue大于一组,返回__NSDictionaryI类型
  • 使用语法糖@{}创建NSDictionary对象,如果只有一个组keyValue,会创建__NSSingleEntryDictionaryI对象,否则创建__NSDictionaryI对象
  • 对象的- isKindOfClass返回YES
  • 对象的- isMemberOfClass返回NO,在使用时需要谨慎
        // 上接MutableDictionary
        
    NSMutableDictionary *md1 = [NSMutableDictionary alloc];
    NSMutableDictionary *md2 = [NSMutableDictionary alloc];
    
    NSLog(@"md1:%p, md2:%p", md1, md1);
        >>> md1:0x7fff801e7560, md2:0x7fff801e7560
    NSLog(@"md1:%@, md2:%@", NSStringFromClass([md1 class]), NSStringFromClass([md2 class]));
        >>> md1:__NSPlaceholderDictionary, md2:__NSPlaceholderDictionary    

    NSMutableDictionary *md3 = [md1 init];
    NSMutableDictionary *md4 = [md2 init];
    
    NSLog(@"md3:%p, md4:%p", md3, md4);
        >>> md3:0x100745ce0, md4:0x100745dc0
    NSLog(@"md3:%@, md4:%@", NSStringFromClass([md3 class]), NSStringFromClass([md4 class]));
        >>> md3:__NSDictionaryM, md4:__NSDictionaryM        

    NSMutableDictionary *md5 = [md1 initWithObjects:objs forKeys:keys];
    NSMutableDictionary *md6 = [md2 initWithObjects:objs forKeys:keys];
    NSLog(@"md5:%p, md6:%p", md5, md6);
        >>> md5:0x100746430, md6:0x100746450
    NSLog(@"md5:%@, md6:%@", NSStringFromClass([md5 class]), NSStringFromClass([md6 class]));
        >>> md5:__NSDictionaryM, md6:__NSDictionaryM

    NSMutableDictionary *md7 = [md1 initWithObjects:string forKeys:string count:1];
    NSMutableDictionary *md8 = [md1 initWithObjects:string forKeys:string count:2];
    NSMutableDictionary *md9 = [md1 initWithObjects:@[@"qwe", @"asd"] forKeys:@[@"qwe", @"asd"]];

    NSLog(@"md7:%p, md8:%p, md9:%p", md7, md8, d9);
        >>> md7:0x105b04080, md8:0x105b040d0, md9:0x100681a70
    NSLog(@"md7:%@, md8:%@, md9:%@", NSStringFromClass([md7 class]), NSStringFromClass([md8 class]), NSStringFromClass([md9 class]));
    >>> md7:__NSDictionaryM, md8:__NSDictionaryM, md9:__NSDictionaryM
    
    NSMutableDictionary *md10 = [md1 initWithDictionary:md5];
    NSMutableDictionary *md11 = [md2 initWithDictionary:md6];
    NSLog(@"md10:%p, md11:%p", md10, md11);
        >>> md10:0x105b04130, md11:0x105b041b0
    NSLog(@"md10:%@, md11:%@", NSStringFromClass([md10 class]), NSStringFromClass([md11 class]));
        >>> md10:__NSDictionaryM, md11:__NSDictionaryM
    
    NSLog(@"md1:%d, md3:%d, md5:%d, md7:%d, md8:%d", [md1 isKindOfClass:[NSMutableDictionary class]], [md3 isKindOfClass:[NSMutableDictionary class]], [md5 isKindOfClass:[NSMutableDictionary class]], [md7 isKindOfClass:[NSMutableDictionary class]], [md8 isKindOfClass:[NSMutableDictionary class]]);
        >>> md1:1, md3:1, md5:1, md7:1, md8:1
    NSLog(@"md1:%d, md3:%d, md5:%d, md7:%d, md8:%d", [md1 isMemberOfClass:[NSMutableDictionary class]], [md3 isMemberOfClass:[NSMutableDictionary class]], [md5 isMemberOfClass:[NSMutableDictionary class]], [md7 isMemberOfClass:[NSMutableDictionary class]], [md8 isMemberOfClass:[NSMutableDictionary class]]);
        >>> md1:0, md3:0, md5:0, md7:0, md8:0
    

结论

  • [NSMutableDictionary alloc] 返回一个__NSPlaceholderDictionary对象,且地址相同。
    • 虽然都是__NSPlaceholderDictionary类型,但是和[NSDictionary alloc]返回的对象地址不同
  • [[NSMutableDictionary alloc] init]返回一个__NSDictionaryM对象,地址不相同,这非常服务预期
  • NSMutableDictionary在无论初始化是否有元素,创建的对象都是__NSDictionaryM
  • 对象的- isKindOfClass返回YES
  • 对象的- isMemberOfClass返回NO,在使用时需要谨慎

NSString

void Cluster_StringTest(void) {
    NSString *s1 = [NSString alloc];
    NSString *s2 = [NSString alloc];
    
    NSLog(@"s1:%p, s2:%p", s1, s2);
    >>> s1:0x7fff803d6158, s2:0x7fff803d6158
    NSLog(@"s1:%@, s2:%@", NSStringFromClass([s1 class]), NSStringFromClass([s2 class]));
    >>> s1:NSPlaceholderString, s2:NSPlaceholderString
      
    NSString *s3 = [s1 init];
    NSString *s4 = [s2 init];
    
    NSLog(@"s3:%p, s4:%p", s3, s4);
    >>> s3:0x7fff803dc728, s4:0x7fff803dc728
    NSLog(@"s3:%@, s4:%@", NSStringFromClass([s3 class]), NSStringFromClass([s4 class]));
    >>> s3:__NSCFConstantString, s4:__NSCFConstantString
      
    NSString *s5 = @"qwe";
    NSString *s6 = [s1 initWithString:@"qwe"];
    unichar sc[3] = {'q', 'w', 'e'};
    NSString *s7 = [s1 initWithCharacters:sc length:3];
    NSString *s8 = [s1 initWithCString:"qwe" encoding:NSUTF8StringEncoding];
    NSLog(@"s5:%p, s6:%p, s7:%p, s8:%p", s5, s6, s7, s8);
    >>> s5:0x100008158, s6:0x100008158, s7:0x38894644d118ba07, s8:0xb53270ee89ab8a5f
    NSLog(@"s5:%@, s6:%@, s7:%@", NSStringFromClass([s5 class]), NSStringFromClass([s6 class]), NSStringFromClass([s7 class]));
    >>> __NSCFConstantString, s6:__NSCFConstantString, s7:NSTaggedPointerString, s8:NSTaggedPointerString

    NSLog(@"s1:%d, s3:%d, s5:%d, s7:%d", [s1 isKindOfClass:[NSString class]], [s3 isKindOfClass:[NSString class]], [s5 isKindOfClass:[NSString class]], [s7 isKindOfClass:[NSString class]]);
    >>> s1:1, s3:1, s5:1, s7:1
    NSLog(@"s1:%d, s3:%d, s5:%d, s7:%d", [s1 isMemberOfClass:[NSString class]], [s3 isMemberOfClass:[NSString class]], [s5 isMemberOfClass:[NSString class]], [s7 isKindOfClass:[NSString class]]);
    >>> s1:0, s3:0, s5:0, s7:1
    // 下接NSMutableString
}

结论

  • [NSString alloc] 返回一个NSString对象,且地址相同。
  • [[NSString alloc] init]返回一个__NSCFConstantString对象,地址相同
  • 使用语法糖@""initWithString:创建__NSCFConstantString对象,因为字符串的内容相同,所以地址相同
  • 使用方法initWithCharacters:length:initWithCString:encoding:创建NSTaggedPointerString对象,地址不相同
  • 对象的- isKindOfClass返回YES
  • 对象的- isMemberOfClass返回NO,在使用时需要谨慎
        // 上接NSString
        NSMutableString *ms1 = [NSMutableString alloc];
    NSMutableString *ms2 = [NSMutableString alloc];

    NSLog(@"ms1:%p, ms2:%p", ms1, ms1);
        >>> ms1:0x7fff803d4530, ms2:0x7fff803d4530
    NSLog(@"ms1:%@, ms2:%@", NSStringFromClass([ms1 class]), NSStringFromClass([ms2 class]));
        >>> ms1:NSPlaceholderMutableString, ms2:NSPlaceholderMutableString

    NSMutableString *ms3 = [ms1 init];
    NSMutableString *ms4 = [ms2 init];

    NSLog(@"ms3:%p, ms4:%p", ms3, ms4);
        >>> ms3:0x101906cf0, ms4:0x101906d20
    NSLog(@"ms3:%@, ms4:%@", NSStringFromClass([ms3 class]), NSStringFromClass([ms4 class]));
        >>> ms3:__NSCFString, ms4:__NSCFString
      
    NSString *ss = @"qwe";
    NSMutableString *ms5 = [ms1 initWithString:ss];
    NSLog(@"ms5:%p", ms5);
        >>> ms5:0x101906dd0
    NSLog(@"ms5:%@", NSStringFromClass([ms5 class]));
        >>> ms5:__NSCFString
      
    NSLog(@"ms1:%d, ms3:%d, ms5:%d", [ms1 isKindOfClass:[NSMutableString class]], [ms3 isKindOfClass:[NSMutableString class]], [ms5 isKindOfClass:[NSMutableString class]]);
        >>> ms1:1, ms3:1, ms5:1
    NSLog(@"ms1:%d, ms3:%d, ms5:%d", [ms1 isMemberOfClass:[NSMutableString class]], [ms3 isMemberOfClass:[NSMutableString class]], [ms5 isMemberOfClass:[NSMutableString class]]);
        >>> ms1:0, ms3:0, ms5:0

结论

  • [NSMutableString alloc] 返回一个NSPlaceholderMutableString对象,且地址相同。
  • [[NSMutableString alloc] init]返回一个__NSCFString对象,地址不相同,这非常服务预期
  • 对象的- isKindOfClass返回YES
  • 对象的- isMemberOfClass返回NO,在使用时需要谨慎

相关文章

  • Class Cluster

    类簇实际上是Foundation framework框架下的一种设计模式,它管理了一组隐藏在公共接口下的私有类。 ...

  • Objective-C的类族模式

    简介 类族(class cluster)模式,可以隐藏其抽象基类(abstract base class)背后的实...

  • 类簇(class cluster)

    类簇实际上是Foundation framework框架下的一种设计模式,它管理了一组隐藏在公共接口下的私有类。 ...

  • Apple Documents -- Class Cluster

    Class Clusters 类簇 类簇是Foundation框架中的一种设计模式,类簇用来组织所有继承于某个抽象...

  • 类簇class cluster

    an architecture that groups a number of private, concrete...

  • MongoDB副本集

    前提 nfs及class已经创建ok cluster-mongo.yaml 验证 用户

  • OC——52个有效方法:9.类族模式

    "类族"(class cluster),较为有用的模式(pattern),可以隐藏"抽象基类"(abstract ...

  • Kmeans

    1. 语法 class sklearn.cluster.KMeans(n_clusters=8, init='k-...

  • python实现聚类算法(一)

    sklearn包中的K-Means算法 函数:sklearn.cluster.``KMeans class skl...

  • iOS 类簇(class cluster)

    概括 类簇是一种设计模式(抽象工厂模式),它管理了一组隐藏在公共接口下的私有类。 详解 简单来说,我们调用的是父类...

网友评论

      本文标题:Class Cluster

      本文链接:https://www.haomeiwen.com/subject/wrlrlltx.html