Question

The following code is in my implementation file:

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

Where you see the call to NSLog(), I get the following error message:

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

EDIT: Also, courseArray is populated with sample data in viewDidLoad:

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

Is there somewhere I should be defining that courseArray will take NSString objects?

Was it helpful?

Solution

The code in viewDidLoad creates an immutable array. You need to make a mutable copy, like this

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

OTHER TIPS

Try this code,

for (NSString *key in courseNames)
{
    NSString *val = [NSString stringWithFormat:@"%@-%@",key,[courseNames objectForKey:key]];
            _txtView.text = val;


    if ([CourseArray count]==0)
    {
        CourseArray= [NSMutableArray arrayWithObject:val];
    }
    else
    {
        [CourseArray addObject:val];
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top