0

我正在为 iOS 5 开发 iPhone 应用程序,但遇到了数组问题。我有一个自定义类,我称之为 Persons。Persons 由两个数组组成。一个给男孩,一个给女孩。首先,我想使用另一个类的方法来填充 Persons 对象,我很确定这可以正常工作。但是,当我要在代码中将数组从 Persons 对象拆分为单个数组时,它不起作用。感觉就像 Persons 对象,或者它的数组以某种方式被卡住了。请帮忙。这段代码向我抛出了一个错误,如下所示:2012-07-24 09:29:03.073 PersonApp[4375:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary Boys]: unrecognized selector sent to instance 0x6ac0fe0'

人员.h

#import <Foundation/Foundation.h>

@interface Persons : NSObject {
    NSArray *Boys;
    NSArray *Girls;
}

@property (nonatomic, copy) NSArray *Boys;
@property (nonatomic, copy) NSArray *Girls;

@end

PersonsViewController.h

#import <UIKit/UIKit.h>
#import "Persons.h"

@interface PersonsViewController : UITableViewController

@property (nonatomic, strong) Persons *persons;

@end

PersonsViewController.m

#import "PersonsViewController.h"

NSArray *boys;
NSArray *girls;
...................
@synthesize persons;     
...................
Communication *comm = [[Communication alloc] init];    
self.persons = [comm getPersons];
boys = [self.persons Boys];
girls = [self.persons Girls];
4

2 回答 2

1

[comm getPersons] 返回的是什么?我认为它不是“人”对象。试试这个代码。

if ([[comm getPersons] isKindOfClass:[Persons class]])
{
   NSLog(@"Not returning Persons object. So this is the error!");
}
于 2012-07-24T07:34:30.097 回答
0

........我在这里可能是错的,

但是你有没有尝试过类似的东西

NSArray *boys = [[[NSArray alloc] initWithArray[[comm getPersons]boys]autorelease];

很抱歉,距离我上次打开 Xcode 已经过去了几个星期,所以对于任何代码错误,我们深表歉意,

您是否正在为 NSArray 初始化/分配内存,执行类似的操作

NSLog(@"count %d",[[[comm getPersons]boys]count]);

那至少应该向您表明男孩数组的存在,(假设它已创建)...

NSLog 是你最好的朋友,我也建议学习如何使用调试器。

于 2012-07-25T15:19:46.710 回答