2016-09-16 51 views
0

在下面的代碼我檢查陣列isKindOfClass,但是當我檢查與它是否會真的兩個條件:CFArrayRef類使用iskindofclass

NSString *values[] = {@"hello", @"world"}; 
    CFArrayRef arrayRef = CFArrayCreate(kCFAllocatorDefault, (void *)values, (CFIndex)2, NULL); 
    NSArray *array = (__bridge NSArray *)arrayRef; 
    if ([array isKindOfClass:[NSMutableArray class]]) { 
     NSLog(@"array is mutableArray"); 
    } 
    if ([array isKindOfClass:[NSArray class]]) { 
     NSLog(@"array is immutabelArray"); 
} 

結果:

2016-09-16 10:00:32.873 TestArray[12246:198137] array is mutableArray 
2016-09-16 10:00:32.874 TestArray[12246:198137] array is immutabelArray 

結果讓我迷惑 數組是不可變的,但爲什麼打印數組是可變的。

+0

添加另一個日誌:'NSLog(@「array是%@」,[array class]);'。 – rmaddy

+0

你應該也看到http://stackoverflow.com/questions/1788690/objective-c-how-to-check-if-variable-is-nsarray-or-nsmutablearray – rmaddy

回答

0

如果你閱讀蘋果文檔,你會看到 @interface NSMutableArray<ObjectType> : NSArray<ObjectType> 所以,如果你的對象是實物的NSMutableArray的,那絕對是一種NSArray中的。 你應該檢查NSLog(@"array class %@", [array class]);

+0

我的問題是一個NSArray對象,它被確定爲一個NSMutableArray –