-2

我想在 for 循环中实例化一些自定义对象,从数组中检索它们。我还想给指针一个数组值的名称。(例子)

personArray in generalArray value: 0=name , 1=address, 2=mobile...</p>

对象:Person(带有 personArray 的即时变量)

////////

for (NSArray*person in generalArray){

NSString*name=[[NSString alloc] initWithString:[person ObjectAtIndex:0];
NSString*address=[[NSString alloc] initWithString:[person ObjectAtIndex:1];
…

//Now I want to instantiate objects "person" with "name, address…" variables. I also tried to name pointers with the "name" NSString.

}

有什么解决办法吗?

4

1 回答 1

1

无法将变量、指针或其他名称的名称设置为另一个变量的值。因此,在您的情况下,您希望将指针的名称设置为名称/地址 NSString 的值。那只是不可能做到的事情。

从 for 循环中实例化一个“Person”对象:

for (NSArray*person in generalArray){

NSString*name=[[NSString alloc] initWithString:[person ObjectAtIndex:0];
NSString*address=[[NSString alloc] initWithString:[person ObjectAtIndex:1];
…

PersonObject *person = [[PersonObject alloc] init];

// Assuming your person object has a name and address property
person.name = name;
person.address = address;


// You should also add the newly created person object to a data collection in order for
// it to be saved. If you don't do that then each iteration will create a new person
// object, but it will not be saved anywhere, which means it's a pointless operation. 
}

这是在 for 循环的每次迭代中创建一个新的 person 对象。您现在正在从人员数组中获取姓名和地址信息并将其放入新创建的人员对象中。

于 2013-09-09T17:21:20.100 回答