3

I have a NSObject class which's name is test.
class test has 3 property. Name, age, id;
I have 3 Objects in test class. s, b, c.
I am putting all of the objects to the array with: NSArray *ary = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
I am trying to access to the data of property in that array. Which means I have to read, write the property of the object in array in the loop (for loop or while loop).
I found a lot of materials on the internet. The method that I was close to do was:

[[ary objectAtIndex:0] setName:@"example"];

This method was working with setters and getters. But it did give a horrible error. Is there any "WORKING" method to do it?
Thanks...

5
  • 1
    @userXXX Now the title's better. (We don't appreciate all caps on Stack Overflow.) Commented Sep 27, 2013 at 18:23
  • The three objects in your array are the character strings "a", "b", and "c". NSStrings are immutable so you can't change them. Commented Sep 27, 2013 at 18:32
  • 2
    "A horrible error" is rather nonspecific. Commented Sep 27, 2013 at 18:38
  • Do this: NSArray *ary = [NSArray arrayWithObjects:a, b, c, nil]; Commented Sep 27, 2013 at 19:07
  • 1
    You say you have an array of test objects, but you don't. You have an array of strings. If you want an array of objects, you have to create three instances of your test class and add those to your array. Commented Sep 27, 2013 at 19:08

2 Answers 2

13

Let's imagine a Person class:

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic) NSInteger age;
@property (nonatomic) long long identifier;

+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age identifier:(long long)identifier;

@end

@implementation Person

+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age identifier:(long long)identifier {
    Person *person = [[self alloc] init];
    person.name = name;
    person.age = age;
    person.identifier = identifier;

    return person;
}

@end

You can then create an array of people like so:

NSArray *people = @[[Person personWithName:@"Rob" age:32 identifier:2452323],
                    [Person personWithName:@"Rachel" age:29 identifier:84583435],
                    [Person personWithName:@"Charlie" age:4 identifier:389433]];

You can then extract an array of people's names like so:

NSArray *names = [people valueForKey:@"name"];
NSLog(@"%@", names);

That will generate:

2013-09-27 14:57:13.791 MyApp[33198:a0b] (
    Rob,
    Rachel,
    Charlie
)

If you want to extract information about the second Person, that would be:

Person *person = people[1];
NSString *name = person.name;
NSInteger age = person.age;
long long identifier = person.identifier;

If you want to change the age of the third person, it would be:

Person *person = people[2];
person.age = 5;

Or, if you want to iterate through the array to extract the information, you can do that, too:

for (Person *person in people) {
    NSString *name = person.name;
    NSInteger age = person.age;
    long long identifier = person.identifier;

    // now do whatever you want with name, age, and identifier
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

STEP 1 : Cast it to the appropriate object type first

s *myS = (s *)[array objectAtIndex:0];
b *myB = (b *)[array objectAtIndex:1]; 
c *myC = (c *)[array objectAtIndex:2]; 

STEP 2 : Set / get whatever property you want to

myS.name = @"example";

2 Comments

STEP 2.5: REMOVE ALL CAPS FROM THE WORD "STEP". Step 3: remove the harmful typecasts as well, because objectAtIndex: returns id. Step 4: use the more verbose subscript notation for NSArray instead of objectAtIndex:.
Step 5: apply cold water to burn.

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.