【问题标题】:NSInvalidArgumentException when working with NSArray使用 NSArray 时出现 NSInvalidArgumentException
【发布时间】:2012-07-24 07:29:58
【问题描述】:

我正在为 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'

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

【问题讨论】:

    标签: objective-c ios xcode ios5


    【解决方案1】:

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

    if ([[comm getPersons] isKindOfClass:[Persons class]])
    {
       NSLog(@"Not returning Persons object. So this is the error!");
    }
    

    【讨论】:

      【解决方案2】:

      ........我可能错了,

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

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

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

      你是在初始化/分配内存到 NSArray,做类似的事情

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

      这至少应该向您表明boys 数组存在,(假设它已创建)...

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-09
        • 2013-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多