The order of a dictionary's entries is not defined. You have to sort the keys (e.g. with any of the sortXXX methods) and save them in a collection which is ordered (e.g. an array). Then use this collection to access the dictionary entries in your desired order.
Sorting items In NSDictionary
-
07-07-2023 - |
题
I am reading the items from plist into dictionary, i am using keys of dictionary to set title and value as tag to the buttons. now i have to sort the items in the dictionary, i don't want to put sorted items into array, as i am using dictionary to set title and tag for the button. I checked the links but most of them are putting sorted items into array. How can i do it with out using array , and instead use same dictionary. here is code snippet.
while (key = [keyEnumerator nextObject])
{
UIButton *btnWith = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnWith.tag = [[topics objectForKey:key] integerValue] ;
[btnWith setTitle:key forState:UIControlStateNormal];
btnWith.titleLabel.font = [UIFont fontWithName:@"Aller-Light" size:15];
[btnWith setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnWith.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btnWith setBackgroundImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[btnWith addTarget:self action:@selector(CheckboxClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollview setContentSize: CGSizeMake(320, topics.count *50)];
}
解决方案
不隶属于 StackOverflow