NSArray becomes NSCFArray when passed

倖福魔咒の 提交于 2019-12-01 04:32:07

问题


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

-(void)performAction:(NSObject *)myAction withItem:(Item *)myItem {

actionCount = -1;
NSLog(@"-- NEW ACTION ARRAY --");
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 isMemberOfClass:[NSArray class]] ) {
    NSLog(@"--- CUSTOM ACTION --- %@", myAction);
    currentActionArray = (NSArray *)myAction;
} 
[self performNextAction];

}

One of four things is going to come through here: Look, Use, Exit or NSArray. The first three are sent off to become NSArrays, the last is already an NSArray.

Now, when I do pass an NSArray in here from elsewhere, like this:

    NSArray *myAction = [[NSArray alloc] initWithObjects:myAction1, myAction2, nil];
[controller performAction:myAction withItem:nil];

...the custom action is never called, because it reads myAction as an NSCFArray rather than an NSArray. When I try [myAction isMemberOfClass:[NSCFArray class]] it doesn't recognise the CF. The simple way to get it working at the moment is just to assume that anything not a Look, Use or Exit is an NSArray (get rid of the last else if, and just leave it as an else), but that seems sloppy to me.

Anyone know how I can deal with this?

Thanks, -k.


回答1:


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.




回答2:


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:?




回答3:


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;
} 


来源:https://stackoverflow.com/questions/1438860/nsarray-becomes-nscfarray-when-passed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!