iOS无法识别的选择器发送到实例

编程入门 行业动态 更新时间:2024-10-28 12:25:07
iOS无法识别的选择器发送到实例 - 将NSDictionary添加到NSMutableArray(iOS unrecognized selector sent to instance - adding NSDictionary to NSMutableArray)

为大量代码道歉,但人们最终总是要求批量。

尝试将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在接口中被decalared,然后作为属性,然后合成然后初始化如下:

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?

最满意答案

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

更多推荐

本文发布于:2023-04-28 00:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   实例   无法识别   选择器   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!