iOS 无法识别的选择器发送到实例 - 将 NSDictionary 添加到 NSMutableArray

发布于 2024-12-06 04:44:26 字数 2328 浏览 0 评论 0原文

对大量代码表示歉意,但无论如何人们通常最终都会要求很多代码。

当尝试将 NSDictionary 添加到 NSMutableArray 时,我收到以下错误

2011-09-22 18:52:54.153 NPT[4788:1603] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x72f1280
2011-09-22 18:52:54.154 NPT[4788:1603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x72f1280'
Call stack at first throw:
(
0   CoreFoundation                      0x028e5919 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x02a335de objc_exception_throw + 47
2   CoreFoundation                      0x028e742b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3   CoreFoundation                      0x02857116 ___forwarding___ + 966
4   CoreFoundation                      0x02856cd2 _CF_forwarding_prep_0 + 50
5   NPT                                 0x00007197 -[Search addPlateToFinals:withSubCount:] + 791
6   NPT                                 0x0000626f -[Search makeNumberedPlatesForPass:] + 1132
7   NPT                                 0x0000401f __-[FirstViewController improveSearch]_block_invoke_1 + 173
8   libSystem.B.dylib                   0x96f02a24 _dispatch_call_block_and_release + 16
9   libSystem.B.dylib                   0x96ef4cf2 _dispatch_worker_thread2 + 228
10  libSystem.B.dylib                   0x96ef4781 _pthread_wqthread + 390
11  libSystem.B.dylib                   0x96ef45c6 start_wqthread + 30
)
terminate called after throwing an instance of 'NSException'

据我从堆栈跟踪和使用换行符可以看出,这是抛出错误的代码:

NSDictionary *dPlateToAdd = [NSDictionary dictionaryWithObjects:objects forKeys:keys];       
[self.aFinals addObject:dPlateToAdd];

self.aFinals 在界面中声明,然后作为属性,然后合成并初始化如下:

Search.h

@interface Search : NSObject {
...
NSMutableArray *aFinals;
...

}
...
@property (nonatomic, retain) NSMutableArray *aFinals;
...
@end

Search.m

...
@synthesize aFinals;
...

-(id)initWithTerm:(NSString *)thisTerm andPrevResults:(NSMutableArray *)aPrevResults {

    ...
    self.aFinals = [aPrevResults copy];
    ...
}

传入的 aPrevResults 是先前搜索对象的属性,该对象已被释放,但已被复制到视图控制器的属性,并发送回新的搜索对象,添加更多结果。

引发错误的行在第一次运行时工作正常,但仅在第二次运行时导致问题,当我重用现在为 aPrevResults 的数组时。

我做错了什么?这一切有道理吗?

Apologies for the masses of code but people usually ask for lots in the end anyway.

I'm getting the following error when trying to add an NSDictionary to an NSMutableArray

2011-09-22 18:52:54.153 NPT[4788:1603] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x72f1280
2011-09-22 18:52:54.154 NPT[4788:1603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x72f1280'
Call stack at first throw:
(
0   CoreFoundation                      0x028e5919 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x02a335de objc_exception_throw + 47
2   CoreFoundation                      0x028e742b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3   CoreFoundation                      0x02857116 ___forwarding___ + 966
4   CoreFoundation                      0x02856cd2 _CF_forwarding_prep_0 + 50
5   NPT                                 0x00007197 -[Search addPlateToFinals:withSubCount:] + 791
6   NPT                                 0x0000626f -[Search makeNumberedPlatesForPass:] + 1132
7   NPT                                 0x0000401f __-[FirstViewController improveSearch]_block_invoke_1 + 173
8   libSystem.B.dylib                   0x96f02a24 _dispatch_call_block_and_release + 16
9   libSystem.B.dylib                   0x96ef4cf2 _dispatch_worker_thread2 + 228
10  libSystem.B.dylib                   0x96ef4781 _pthread_wqthread + 390
11  libSystem.B.dylib                   0x96ef45c6 start_wqthread + 30
)
terminate called after throwing an instance of 'NSException'

This is the code throwing the error, as far as I can tell from the stack trace and using line breaks:

NSDictionary *dPlateToAdd = [NSDictionary dictionaryWithObjects:objects forKeys:keys];       
[self.aFinals addObject:dPlateToAdd];

self.aFinals is decalared in the interface, and then as a property, then synthesized and then initialized as follows:

Search.h

@interface Search : NSObject {
...
NSMutableArray *aFinals;
...

}
...
@property (nonatomic, retain) NSMutableArray *aFinals;
...
@end

Search.m

...
@synthesize aFinals;
...

-(id)initWithTerm:(NSString *)thisTerm andPrevResults:(NSMutableArray *)aPrevResults {

    ...
    self.aFinals = [aPrevResults copy];
    ...
}

The aPrevResults that is passed in, was a property of a previous search object that has since been released, but has been copied to a property of the view controller, and sent back into a new search object, to add more results.

The line that is throwing the error works fine on the first pass, but is only causing a problem on this second run, when I reuse the array that is now aPrevResults.

What have I done wrong? Does any of this make sense as at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烈酒灼喉 2024-12-13 04:44:27

-copy 返回不可变对象,即使您在可变数组上调用它也是如此。要获得可变副本,您必须调用 mutableCopy 方法:

-(id)initWithTerm:(NSString *)thisTerm andPrevResults:(NSMutableArray *)aPrevResults {

    ...
    self.aFinals = [aPrevResults mutableCopy];
    ...
}

此外,由于您使用的是保留属性,因此您需要补偿额外的增量以保留副本上发生的计数:

 self.aFinals = [[aPrevResults mutableCopy] autorelease];

或者,可能是使用便利类方法的最佳和最干净的解决方案:

 self.aFinals = [NSMutableArray arrayWithArray:aPrevResults];

-copy returns immutable object even if you call it on a mutable array. To get mutable copy you must call mutableCopy method:

-(id)initWithTerm:(NSString *)thisTerm andPrevResults:(NSMutableArray *)aPrevResults {

    ...
    self.aFinals = [aPrevResults mutableCopy];
    ...
}

Also since you're using retaining property you need to compensate extra increment to retain count occurring on copy:

 self.aFinals = [[aPrevResults mutableCopy] autorelease];

Or, probably the best and the cleanest solution using convenience class method:

 self.aFinals = [NSMutableArray arrayWithArray:aPrevResults];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文