ios - 在ios中添加带有字典键的数组
<p><p>我是 iOS 新手,正在开发一个应用程序。在这个我有一个数组,其中标题包含(a,b,c,....)作为值,在另一个数组中我有一个包含动物名称作为字符串的值,现在我想将此值作为键值存储在字典,意思是动物的名字以“a”开头应该像这样添加,</p>
<pre><code> NSDictionary *animals;
NSArray *animalSectionTitles;
//Edit by me
NSMutableDictionary *anim;
NSArray *animTitles;
NSMutableArray *commmonary;
NSMutableArray *myary;
}
@end
@implementation AnimalTableTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = ;
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
;
anim = [init];
// animals = @[@"Bear", @"Black Swan", @"Buffalo", @"Camel", @"Cockatoo", @"Dog", @"Donkey", @"Emu", @"Giraffe", @"Greater Rhea", @"Hippopotamus", @"Horse", @"Koala", @"Lion", @"Llama", @"Manatus", @"Meerkat", @"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear", @"Rhinoceros", @"Seagull", @"Tasmania Devil", @"Whale", @"Whale Shark", @"Wombat"];
animals = @{
@"A" :@[@"Affrican cat", @"Assian cat", @"Alsesian fox"],
@"B" : @[@"Bear", @"Black Swan", @"Buffalo"],
@"C" : @[@"Camel", @"Cockatoo"],
@"D" : @[@"Dog", @"Donkey"],
@"E" : @[@"Emu"],
@"G" : @[@"Giraffe", @"Greater Rhea"],
@"H" : @[@"Hippopotamus", @"Horse"],
@"K" : @[@"Koala"],
@"L" : @[@"Lion", @"Llama"],
@"M" : @[@"Manatus", @"Meerkat"],
@"P" : @[@"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear"],
@"R" : @[@"Rhinoceros"],
@"S" : @[@"Seagull"],
@"T" : @[@"Tasmania Devil"],
@"W" : @[@"Whale", @"Whale Shark", @"Wombat"]};
commmonary = ;
animalSectionTitles = [ sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
//adding alll keys in array from dictionary
animTitles = ;
NSLog(@"===My all keys for dictionary----%@",animTitles);
NSLog(@"===my anim common aray is---%li",commmonary.count);
//new logic..!
for(NSString *letter in animTitles)
{
forKey:letter];
}
for(NSString *animal in commmonary)
{
NSString *firstLetter = ;
NSMutableArray *arr = ;
;
;
NSLog(@"==my array is==%@",anim);
}
}
//new logic..!!
- (NSString *)getImageFilename:(NSString *)animal
{
NSString *imageFilename = [ stringByReplacingOccurrencesOfString:@" " withString:@"_"];
imageFilename = ;
return imageFilename;
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *sectionTitle = ;
NSArray *sectionAnimals = ;
return ;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = ;
// Configure the cell...
NSString *sectionTitle = ;
NSArray *sectionAnimals = ;
NSString *animal = ;
cell.textLabel.text = animal;
cell.imageView.image = ];
return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//return animalSectionTitles;
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return ;
}
</code></pre>
<p>这里 animTitles 包含 a,c,d...作为值。提前致谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>尝试像这样替换你的 for 循环</p>
<pre><code> for (int i=0; i<animTitles.count; i++) {
myary = [init];
for(int j=0;j<commmonary.count;j++){
NSString *a = ;
NSString *firstLetter = ;
if (]) {
//adding values to array.
;
}
}
];
NSLog(@"====my Dictionary is===%@-->",anim);
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在ios中添加带有字典键的数组,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34368186/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34368186/
</a>
</p>
页:
[1]