我正在为 iOS 5 开发 iPhone 应用程序,但遇到了数组问题。我创建了一个自定义类,将其称为“Persons”。 Persons 由两个数组组成。一套给男孩,一套给女孩。首先,我想使用另一个类中的方法填充 Persons 对象,并且我非常确定这可以正常工作。但是,当我要将代码中的 Persons 对象的数组拆分为单个数组时,它不起作用。感觉就像 Persons 对象或其数组以某种方式卡住了。请帮忙。此代码向我抛出一个错误,如下所示: 2012-07-24 09:29:03.073 PersonApp[4375:f803] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFDictionary Boys” ]: 无法识别的选择器发送到实例 0x6ac0fe0'
Persons.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];
最佳答案
[comm getPersons] 返回什么?我认为这不是“Persons”对象。试试这个代码。
if ([[comm getPersons] isKindOfClass:[Persons class]])
{
NSLog(@"Not returning Persons object. So this is the error!");
}
关于objective-c - 使用 NSArray 时出现 NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11626094/