0

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?

6
  • courseArray is an immutable array. Where's your code that actually assigns a value to the courseArray variable? Commented Mar 29, 2014 at 4:19
  • courseArray was defined as NSMutableArray in the very first line of the code. How is it immutable? courseArray is populated with sample data in viewDidLoad, adding to the question now. Commented Mar 29, 2014 at 4:21
  • @rar The declaration is irrelevant. You are assigning an immutable array to the variable. It's not magically made mutable just because of how you declare the variable. Commented Mar 29, 2014 at 4:25
  • I understand now. Assigning it the country values made it an immutable array. Commented Mar 29, 2014 at 4:27
  • FYI - here's a hint when looking at errors like you have. When you see __NSArrayI - that's an internal representation for an immutable array (hence the I). If the error was about an actual mutable array, you would probably see __NSArrayM. Commented Mar 29, 2014 at 4:30

2 Answers 2

2

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];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Since I didn't actually need those values, I simply used courseArray = [[NSMutableArray alloc] init]; to properly declare the array as I wanted it.
0

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

1 Comment

Same exception thrown: 2014-03-29 00:23:18.823 WebServiceTest[44834:60b] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0xde41590

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.