xcode -[__NSArrayI objectForKeyedSubscript:]: 无法识别的选择器发送到实例 IN xcode6 object-c ios
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29598996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
-[__NSArrayI objectForKeyedSubscript:]: unrecognized selector sent to instance IN xcode6 objective-c ios
提问by Joon. P
I am trying to learn objective-c, and came across some crash I couldn't resolve. I believe it's one of the basic problems, but I am new here and got lost in the middle.
我正在尝试学习objective-c,但遇到了一些我无法解决的崩溃。我相信这是基本问题之一,但我是新来的,在中间迷路了。
I have :
我有 :
- ModelViewController.h
- ModelViewController.m
- Schedule.h
- Schedule.m
- 模型视图控制器.h
- 模型视图控制器.m
- 时间表.h
- 时间表.m
in ModelViewController.h
在 ModelViewController.h 中
#import <UIKit/UIKit.h>
@interface FetchScheduleVC : UIViewController
@property (copy, nonatomic) NSMutableArray *myMutableArray;
@end
in ModelViewController.m
在 ModelViewController.m 中
#import "ModelViewController.h"
#import "Schedule.h"
@implementation Model
- (void)viewDidLoad {
[super viewDidLoad];
for(int i=0; i < 3; i++){
[_myMutableArray addObjects: [NSNumber numberWithInt: i]
}
}
- (IBAction)saveBtn:(UIButton *)sender {
Schedule *newSchedule = [[Schedule alloc]init];
[newSchedule createClassFromArray: _myMutableArray];
}
@end
in Schedule.h
在 Schedule.h
#import <Foundation/Foundation.h>
@interface Schedule : NSObject
@property (strong, nonatomic) NSMutableArray *classArray;
-(void) createClassFromArray: (NSArray *) selectedArr;
@end
in Schedule.m
在 Schedule.m
#import "Schedule.h"
@implementation Schedule
-(void) createClassFromArray: (NSArray *) selectedArr {
for(NSNumber *i in selectedArr){
NSLog(@"number in array is : %@", i);
}
}
@end
I simplified my codes a little, but the basic flow is the same. When I run this, and click a button to call - (IBAction)saveBtn:(UIButton *)sender, I get:
我稍微简化了我的代码,但基本流程是相同的。当我运行它,并单击一个按钮来调用 - (IBAction)saveBtn:(UIButton *)sender,我得到:
-[__NSArrayI objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7fcb61d2fd10
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7fcb61d2fd10'
Anything wrong doings in sending NSArray through Method up here?
在这里通过 Method 发送 NSArray 有什么错误吗?
回答by trojanfoe
I can only tell you what's happening in your code, not where it's happening, as you've not posted the offending code.
我只能告诉你你的代码中发生了什么,而不是它发生的地方,因为你没有发布有问题的代码。
The objectForKeyedSubscript
method is called for NSDictionary
subscripting, for example:
objectForKeyedSubscript
调用该方法进行NSDictionary
下标,例如:
NSDictionary *dict = @{ @"key" : @"value" };
NSString *value = dict[@"key"]; // HERE
so it looks like you are doing something like this:
所以看起来你正在做这样的事情:
for(int i=0; i < 3; i++){
[_myMutableArray addObjects: [NSNumber numberWithInt: i]
}
NSNumber *num = _myMutableArray[@"3"]; // !!!!
One more thing I can tell you, it's not to do with uninitialized array as Objective-C simply ignores attempts to dereference nil
objects and this exception has gone further than that.
我可以告诉你的另一件事是,它与未初始化的数组无关,因为 Objective-C 简单地忽略了取消引用nil
对象的尝试,而这个异常比这更进一步。
回答by Sanjay Mohnani
As you have declared myMutableArray with copy semantic as-
正如您已将 myMutableArray 声明为具有复制语义为-
@property (copy, nonatomic) NSMutableArray *myMutableArray;
this will sends a copy message to the array, which results in an immutable copy.
这将向数组发送一个复制消息,从而产生一个不可变的副本。
So, to use above semantics on NSMutableArray
you need to override the "setter" method as -
因此,要使用上述语义,NSMutableArray
您需要将“setter”方法覆盖为 -
- (void)setArray:(NSArray *)newArray
{
if ( array != newArray )
{
[array release];
array = [newArray mutableCopy];
}
}
the above setter method just assign the reference of mutable copy of the newArray to the array, which help you to mutate the objects and thus avoiding the error.
上面的 setter 方法只是将 newArray 的可变副本的引用分配给数组,这有助于您对对象进行变异,从而避免错误。
回答by Joon. P
In the codes above, I forgot to include
在上面的代码中,我忘了包括
_myMutableArray = [[NSMutableArray alloc] init];
although i had that in my actual code.
虽然我在我的实际代码中有那个。
the problem was very dumb. Sorry for this misleading question. Hope you just ignore this question cuz it's due to such a careless mistake.
这个问题很愚蠢。很抱歉这个误导性的问题。希望你忽略这个问题,因为这是一个粗心大意的错误。
In my actual code, I had duplicate variable names like this:
在我的实际代码中,我有重复的变量名,如下所示:
in Schedule.h
在 Schedule.h
#import <Foundation/Foundation.h>
@interface Schedule : NSObject
@property (strong, nonatomic) NSMutableArray *selectedArr;
-(void) createClassFromArray: (NSArray *) selectedArr;
@end
..very.. careless mistake.
..非常..粗心的错误。