简体   繁体   中英

Add same hard coded object to NSDictionary for the count of an NSMutableArray

I have an array that has 5 object in. like so:

  NSMutableArray *categoriesMutableNameArray = [NSMutableArray arrayWithArray:[prefs arrayForKey:@"categoriesImageUrl"]];

I get the array values from a web service and the array looks like: fashion, Mens, Ladies, Gadgets.

I then save the array to a dictionary like so:

  NSDictionary = @{ @"title" : categoriesMutableNameArray, @"selected": @"No"};

You will notice that I have a hard coded field in the dictionary called "selected": @"No".

How can I make another array with selected so that the selected array equals that to the count of categoriesMutableNameArray so for each entry in categoriesMutableNameArray I want to make a selected object. I basically want the following:

NSArray *select = [arraWithObjects:@"No"]; 

but I want to add @"No" to the select array to be the same as the count for categoriesMutableNameArray.

So if there are 4 objects in categoriesMutableNameArray like so:

fashion, Mens, Ladies, Gadgets. I would like NSArray *select to have the same amount of objects categoriesMutableNameArray has like so :

NSArray *select  =[arraWithObjects:@"No",@"No",@"No",@"No"]; 

Regards

Try this,

 NSMutableArray *categoriesMutableNameArray = [NSMutableArray arrayWithArray:[prefs arrayForKey:@"categoriesImageUrl"]];
 NSMutableArray *array = [[NSMutableArray alloc] init];
 for (id element in categoriesMutableNameArray) {
     [array addObject:@"No"];
 }

 NSDictionary *dict = @{ @"title" : categoriesMutableNameArray, @"selected": array};

hm...

NSMutableArray *select = [[NSMutableArray alloc] init];
for (int count = 0; count < [categoriesMutableNameArray count]; count++) {
   [select addObject:@"No"];
}

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