Arrays and objects


Hey I’m new to coding objective C. I want to create an array of an object with a string and a number.

    [code]NSString *jumpingJacks = @"jumpingJacks";
    NSString *laps = @"laps";

    NSMutableArray *exercise  = [NSMutableArray array];
    [exercise addObject:jumpingJacks, addInt:30];
    [exercise addObject:laps, addInt:4];[/code]

Is that even possible? If so, how would I code it?


Hey if you open up the Class Reference for NSMutableArray and have a peek at the section entitled Adding Objects, you will see that the method you are invoking on your array object does not exist.

What you are trying to do is possible, but how you are doing it is not correct.

Aren’t you reading the BNR Objective-C Programming Book?


If the code worked I would not have posted a question about it. This was an example stating what I want. I do have the Objective C Programming book. Would you like to give me an example, tell me where in the book to find and example or what to look up?


It looks like you haven’t finished reading the book yet. What you are trying to do is covered in Chapter 21 - Collection Classes in the copy of my book.

TIP: You can’t add instances of fundamental types (such as char, int, float, etc.) to collection objects. However, you can wrap them in something (an instance of NSNumber or NSValue) that can be added to a collection object. (That’s why I have asked you to take a look at the NSMutableArray Class Reference.)


Thanks :slight_smile: I found it