简体   繁体   中英

Array referencing an object in a different array

I have a list of trucks listed below. I also have a list of people also listed below. I want the second person (dondi) to be the owner of the Buttermilk Truck, but I don't know how to reference that in the attribute personsTruck. Any ideas?

 listTrucks = [[NSMutableArray alloc] initWithObjects:
               [Truck truckWithCuisine:@"American Cuisine" name:@"Buttermilk Truck" menu:[NSData dataWithContentsOfFile:@"/Users/Steve/Desktop/Truck Tracker App/Truck Tracker App/Buttermilk Truck Menu.tiff"] latitude: [NSNumber numberWithDouble: 0.1] longitude: [NSNumber numberWithDouble: 0.1]schedule:@"7/15/12"],


 listUsers = [[NSMutableArray alloc] initWithObjects:
             [Person personWithEmail:@"stephen" password:@"test" type:@"Eater" userFavoritesList:nil, personsTruck:nil],
             [Person personWithEmail:@"dondi" password:@"test" type:@"Owner" userFavoritesList:favoritedTrucks = [[NSMutableArray alloc] init], personsTruck:listTrucks.],
              nil];
 selectedPerson = nil;

Wether you know the owner of the truck, i mean is it static?

if yes you can go for this..

You can maintain the owner's name in trucks list by adding an index value of the users list in the Truck object, which gives the reference to users list.

in listUsers store personsTruck = 0 . It means first element at array listTrucks . To get value from listTrucks user objectAtIndex: 0 (personsTruck).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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