本文介绍了NSArray在传递时变为NSCFArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法接收许多不同类型的对象,并决定如何处理:

   - (void)我们可以使用下面的代码来实现这个目的。 
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];

}



通过这里:看,使用,退出或NSArray。



现在,当我从其他地方传递NSArray时,像这样:

p>

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

...自定义操作从未被调用,因为它将myAction读为NSCFArray,而不是NSArray 。当我尝试[myAction isMemberOfClass:[NSCFArray类]]它不能识别CF。现在让它工作的简单方法只是假设任何不是一个看,使用或退出是一个NSArray(摆脱最后一个如果,只是把它作为一个else),但似乎马虎我。



任何人都知道如何处理这个问题。



谢谢,
-k。您可以尝试使用 isKindOfClass:,而不是。 isMemberOfClass:



第一个会为您发送的类的实例或其子类的对象返回YES,可能是 NSCFArray 的情况。


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.

解决方案

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.

这篇关于NSArray在传递时变为NSCFArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:05