NSArray becomes NSCFArray when passed

后端 未结 3 1752
迷失自我
迷失自我 2021-01-13 14:29

I have a method that receives many different kinds of objects and decides what to do with them:

-(void)performAction:(NSObject *)myAction withItem:(Item *)my         


        
相关标签:
3条回答
  • 2021-01-13 15:01

    Two possibilities worth considering:

    • NSArray is a class cluster. I don't know the exact behaviour of -isMemberOfClass: and -isKindOfClass: on clusters. you can check this question for more precisions: Is it safe to use isKindOfClass: against an NSString instance to determine type?

    • Have you tried -isKindOfClass: instead of -isMemberOfClass:?

    0 讨论(0)
  • 2021-01-13 15:01

    NSСFArray is subclass of NSMutable Array

    You can use isKindOfClass to check it

    if ([myAction isMemberOfClass:[Look class]]) {
        currentActionArray = [self createLookArray:(Look *)myAction item:myItem];
    } else if ([myAction isMemberOfClass:[Use class]]) {
            currentActionArray = [self createUseArray:(Use *)myAction item:myItem];
    } else if ([myAction isMemberOfClass:[Exit class]]) {
            currentActionArray = [self createExitArray:(Exit *)myAction item:myItem];
    } else if ([myAction isKindOfClass:[NSArray class]] ) {
            NSLog(@"--- CUSTOM ACTION --- %@", myAction);
            currentActionArray = (NSArray *)myAction;
    } 
    
    0 讨论(0)
  • 2021-01-13 15:03

    You can try using isKindOfClass: instead of isMemberOfClass:.

    The first one will return YES for objects that are either instances of the class you send, or subclasses of it, as it may be the case for NSCFArray.

    0 讨论(0)
提交回复
热议问题