简体   繁体   English

数组上对象的属性(Objective-c)

[英]Properties of objects on array (Objective-c)

I have a NSObject class which's name is test. 我有一个名为test.NSObjecttest.
class test has 3 property. class test有3个属性。 Name , age , id ; Nameageid ;
I have 3 Objects in test class. 我在测试类中有3个对象。 s , b , c . sbc
I am putting all of the objects to the array with: NSArray *ary = [NSArray arrayWithObjects:@"a", @"b", @"c", nil]; 我将所有对象放到数组中: 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). 这意味着我必须阅读,在循环中写入对象的属性(for循环或while循环)。
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. 这种方法适用于setter和getter。 But it did give a horrible error. 但它确实给出了一个可怕的错误。 Is there any "WORKING" method to do it? 有没有“工作”的方法呢?
Thanks... 谢谢...

Let's imagine a Person class: 让我们想象一个Person类:

@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 *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
}

Try this 尝试这个

STEP 1 : Cast it to the appropriate object type first 第1步:首先将其转换为适当的对象类型

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 第2步:设置/获取您想要的任何属性

myS.name = @"example";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM