以下代码在我的实现文件中:

NSMutableArray *courseArray;


- (IBAction)btnClick:(id)sender
{
    NSDictionary *courseNames;
    if(![_txtBox.text isEqual:@""]) //if not empty
    {
        courseNames = [self retrieveCourseNamesForSemester:_txtBox.text];
        for (NSString *key in courseNames)
        {
            NSString *val = [NSString stringWithFormat:@"%@-%@",key,[courseNames objectForKey:key]];
            _txtView.text = val;
            @try
            {
                [courseArray addObject:val];
            }
            @catch(NSException *e)
            {
                NSLog(@"Exception: %@ for value = %@", e, val);
            }
        }
    }
    [_coursePicker reloadAllComponents];
    _coursePicker.hidden=false;
    [_txtBox resignFirstResponder];
}


在您看到对NSLog()的调用的地方,我收到以下错误消息:

2014-03-29 00:02:25.830 WebServiceTest[44646:60b] Exception: -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x8d82c30 for value = 73-522-Course Name


编辑:另外,courseArray填充有viewDidLoad中的示例数据:

- (void)viewDidLoad
{
    [super viewDidLoad];
    courseArray = @[@"Australia (AUD)", @"China (CNY)",
                    @"France (EUR)", @"Great Britain (GBP)", @"Japan (JPY)"];
}


我应该在什么地方定义courseArray将使用NSString对象吗?

最佳答案

viewDidLoad中的代码创建一个不可变数组。您需要像这样制作可变的副本

(void)viewDidLoad
{
    [super viewDidLoad];
    courseArray = [@[@"(AUD)", @"(CNY)", @"(EUR)"] mutableCopy];
}

关于ios - 添加到NSMutableArray时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22726881/

10-13 06:34