-[__NSArrayI objectForKeyedSubscript:]: 无法识别的选择器发送到实例 IN xcode6 objective-c ios
-[__NSArrayI objectForKeyedSubscript:]: unrecognized selector sent to instance IN xcode6 objective-c ios
我正在尝试学习 objective-c,遇到了一些我无法解决的崩溃。
我相信这是基本问题之一,但我是新来的,在中间迷路了。
我有:
- ModelViewController.h
- ModelViewController.m
- Schedule.h
- Schedule.m
在ModelViewController.h
#import <UIKit/UIKit.h>
@interface FetchScheduleVC : UIViewController
@property (copy, nonatomic) NSMutableArray *myMutableArray;
@end
在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
在Schedule.h
#import <Foundation/Foundation.h>
@interface Schedule : NSObject
@property (strong, nonatomic) NSMutableArray *classArray;
-(void) createClassFromArray: (NSArray *) selectedArr;
@end
在Schedule.m
#import "Schedule.h"
@implementation Schedule
-(void) createClassFromArray: (NSArray *) selectedArr {
for(NSNumber *i in selectedArr){
NSLog(@"number in array is : %@", i);
}
}
@end
我稍微简化了代码,但基本流程是一样的。
当我 运行 这个,并点击一个按钮调用 - (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'
这里通过方法发送 NSArray 有什么不对的地方吗?
正如您声明的 myMutableArray 和复制语义一样-
@property (copy, nonatomic) NSMutableArray *myMutableArray;
这将向数组发送复制消息,从而生成不可变副本。
因此,要在 NSMutableArray
上使用上述语义,您需要将 "setter" 方法重写为 -
- (void)setArray:(NSArray *)newArray
{
if ( array != newArray )
{
[array release];
array = [newArray mutableCopy];
}
}
上述setter方法只是将newArray的可变副本的引用分配给数组,这有助于您改变对象,从而避免错误。
我只能告诉你你的代码中发生了什么,而不是它发生的地方,因为你没有发布有问题的代码。
调用objectForKeyedSubscript
方法进行NSDictionary
下标,例如:
NSDictionary *dict = @{ @"key" : @"value" };
NSString *value = dict[@"key"]; // HERE
看起来你正在做这样的事情:
for(int i=0; i < 3; i++){
[_myMutableArray addObjects: [NSNumber numberWithInt: i]
}
NSNumber *num = _myMutableArray[@"3"]; // !!!!
还有一件事我可以告诉你,它与未初始化的数组无关,因为 Objective-C 只是忽略了取消引用 nil
对象的尝试,而且这个异常比那更进一步。
在上面的代码中,我忘了包含
_myMutableArray = [[NSMutableArray alloc] init];
虽然我在实际代码中有它。
这个问题很愚蠢。很抱歉这个误导性的问题。希望你忽略这个问题,因为这是一个粗心的错误。
在我的实际代码中,我有这样的重复变量名:
在Schedule.h
#import <Foundation/Foundation.h>
@interface Schedule : NSObject
@property (strong, nonatomic) NSMutableArray *selectedArr;
-(void) createClassFromArray: (NSArray *) selectedArr;
@end
..非常..粗心的错误。
我正在尝试学习 objective-c,遇到了一些我无法解决的崩溃。 我相信这是基本问题之一,但我是新来的,在中间迷路了。
我有:
- ModelViewController.h
- ModelViewController.m
- Schedule.h
- Schedule.m
在ModelViewController.h
#import <UIKit/UIKit.h>
@interface FetchScheduleVC : UIViewController
@property (copy, nonatomic) NSMutableArray *myMutableArray;
@end
在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
在Schedule.h
#import <Foundation/Foundation.h>
@interface Schedule : NSObject
@property (strong, nonatomic) NSMutableArray *classArray;
-(void) createClassFromArray: (NSArray *) selectedArr;
@end
在Schedule.m
#import "Schedule.h"
@implementation Schedule
-(void) createClassFromArray: (NSArray *) selectedArr {
for(NSNumber *i in selectedArr){
NSLog(@"number in array is : %@", i);
}
}
@end
我稍微简化了代码,但基本流程是一样的。 当我 运行 这个,并点击一个按钮调用 - (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'
这里通过方法发送 NSArray 有什么不对的地方吗?
正如您声明的 myMutableArray 和复制语义一样-
@property (copy, nonatomic) NSMutableArray *myMutableArray;
这将向数组发送复制消息,从而生成不可变副本。
因此,要在 NSMutableArray
上使用上述语义,您需要将 "setter" 方法重写为 -
- (void)setArray:(NSArray *)newArray
{
if ( array != newArray )
{
[array release];
array = [newArray mutableCopy];
}
}
上述setter方法只是将newArray的可变副本的引用分配给数组,这有助于您改变对象,从而避免错误。
我只能告诉你你的代码中发生了什么,而不是它发生的地方,因为你没有发布有问题的代码。
调用objectForKeyedSubscript
方法进行NSDictionary
下标,例如:
NSDictionary *dict = @{ @"key" : @"value" };
NSString *value = dict[@"key"]; // HERE
看起来你正在做这样的事情:
for(int i=0; i < 3; i++){
[_myMutableArray addObjects: [NSNumber numberWithInt: i]
}
NSNumber *num = _myMutableArray[@"3"]; // !!!!
还有一件事我可以告诉你,它与未初始化的数组无关,因为 Objective-C 只是忽略了取消引用 nil
对象的尝试,而且这个异常比那更进一步。
在上面的代码中,我忘了包含
_myMutableArray = [[NSMutableArray alloc] init];
虽然我在实际代码中有它。
这个问题很愚蠢。很抱歉这个误导性的问题。希望你忽略这个问题,因为这是一个粗心的错误。
在我的实际代码中,我有这样的重复变量名:
在Schedule.h
#import <Foundation/Foundation.h>
@interface Schedule : NSObject
@property (strong, nonatomic) NSMutableArray *selectedArr;
-(void) createClassFromArray: (NSArray *) selectedArr;
@end
..非常..粗心的错误。