如何从无序 NSDictionary 中获取有序值

发布于 2025-01-08 02:19:19 字数 1856 浏览 4 评论 0原文

我正在尝试从表中获取并显示内容。目前我正在运行查询并使用 NSMutableDictionary 存储这些 id 和类别名称,效果很好。

但是当我将内容存储到 NSMutableDictionary 中时,我遇到一个问题,顺序发生了变化。在搜索此问题时,似乎这是 NSMutableDictionary 的默认行为。

我不确定如何使用以及什么用途来存储密钥对值。

请告诉我如何克服这个问题

感谢您过来...

//====================================================================
- ( NSMutableDictionary * ) getDataToDisplayTierOne:(NSString*)dbPath{
//====================================================================

    NSMutableDictionary *aTierOneTemplateData = [[NSMutableDictionary alloc]init];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 
    {
        const char *sql_query_stmt = "select * from main_categories";

        sqlite3_stmt *selectstmt;

        if(sqlite3_prepare_v2(database, sql_query_stmt, -1, &selectstmt, NULL) == SQLITE_OK) 
        {           
            while(sqlite3_step(selectstmt) == SQLITE_ROW) 
            {   
                NSString *aValue = [[NSString alloc] initWithUTF8String:
                                    (const char *) sqlite3_column_text(selectstmt, 1)];

                NSString *aId = [[NSString alloc] initWithUTF8String:
                                 (const char *) sqlite3_column_text(selectstmt, 0)];


                [aTierOneTemplateData setObject:aId forKey:aValue];

                [aValue release];
                [aId release];
            }
        }

        sqlite3_finalize(selectstmt);    
    }   
    sqlite3_close(database); 

    return aTierOneTemplateData;

}

注意:-我了解使用 nsmutabledictionery 的限制 所以我期待你的建议,我该如何克服/获取ID, 其他方式的价值。请建议

更新

例如

Apple 1 橙2 Papaya 3

实际上我希望该列表显示在我的 uitableview

Apple 上 橙子 木瓜

当点击每个单元格时,我想要它的 id 值 例如,我点击 Orange ,现在我想将该 id 值设置为 2

你能告诉我如何迭代字典数组吗

I am trying to fetch and display contents from table.Currently i am running a query and store those id, and category name using NSMutableDictionary which works fine.

But I face one issue when i store the contents into NSMutableDictionary the order gets changed.When searching for this issue , it seems that this is the default behavior of the NSMutableDictionary.

I am not sure and how to use and what use to store the key pair value.

Please advice me that how can i overcome this problem

Thanks for stopping by...

//====================================================================
- ( NSMutableDictionary * ) getDataToDisplayTierOne:(NSString*)dbPath{
//====================================================================

    NSMutableDictionary *aTierOneTemplateData = [[NSMutableDictionary alloc]init];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 
    {
        const char *sql_query_stmt = "select * from main_categories";

        sqlite3_stmt *selectstmt;

        if(sqlite3_prepare_v2(database, sql_query_stmt, -1, &selectstmt, NULL) == SQLITE_OK) 
        {           
            while(sqlite3_step(selectstmt) == SQLITE_ROW) 
            {   
                NSString *aValue = [[NSString alloc] initWithUTF8String:
                                    (const char *) sqlite3_column_text(selectstmt, 1)];

                NSString *aId = [[NSString alloc] initWithUTF8String:
                                 (const char *) sqlite3_column_text(selectstmt, 0)];


                [aTierOneTemplateData setObject:aId forKey:aValue];

                [aValue release];
                [aId release];
            }
        }

        sqlite3_finalize(selectstmt);    
    }   
    sqlite3_close(database); 

    return aTierOneTemplateData;

}

Note : - I understand the limitations on using the nsmutabledictionery
so i expecting your advice to that how can i overcome / fetch id,
value in other way.please suggest

Updated

For example

Apple 1
Orange 2
Papaya 3

Actually i want that list to display it on my uitableview

Apple
Orange
Papaya

When tapping on each cell , i want to have its id value
for instance , i tap on Orange , now i want to have that id value as 2

Can you please tell me how to iterate over array of dictionery

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花落人断肠 2025-01-15 02:19:19

使用 NSMutableArray 而不是 NSMutableDictionary。

将声明更改

NSMutableDictionary *aTierOneTemplateData = [[NSMutableDictionary alloc]init];

NSMutableArray *aTierOneTemplateList = [[NSMutableArray alloc]init];

And add the abjects to the array,

[aTierOneTemplateList addObject:aId];

And return the NSMutableArray,

return aTierOneTemplateList;

编辑:

如果按如下方式修改 while 循环,您将获得 NSDictionary 数组相同的顺序。

NSString *aValue = [[NSString alloc] initWithUTF8String:
                    (const char *) sqlite3_column_text(selectstmt, 1)];

NSString *aId = [[NSString alloc] initWithUTF8String:
                 (const char *) sqlite3_column_text(selectstmt, 0)];

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:aValue,@"value",aId,@"key", nil];
[aTierOneTemplateList addObject:dict];
[dict release];
[aValue release];
[aId release];

编辑2:

现在考虑在你的tableviewcontroller中,你有一个NSDictionary数组(比如aTierOneTemplateList)。

cellForRowAtIndexPath方法中,

NSDictionary *dict = [aTierOneTemplateList objectAtIndex:indexPath.row];
NSString *key = [dict objectForKey:@"key"];
NSString *value = [dict objectForKey:@"value"];

didSelectRowAtIndexPath方法中类似。

Use NSMutableArray instead of NSMutableDictionary.

Change the declaration

NSMutableDictionary *aTierOneTemplateData = [[NSMutableDictionary alloc]init];

to

NSMutableArray *aTierOneTemplateList = [[NSMutableArray alloc]init];

And add the abjects to the array,

[aTierOneTemplateList addObject:aId];

And return the NSMutableArray,

return aTierOneTemplateList;

Edit:

If you modify the while loop as follows, you will get array of NSDictionarys with the same order.

NSString *aValue = [[NSString alloc] initWithUTF8String:
                    (const char *) sqlite3_column_text(selectstmt, 1)];

NSString *aId = [[NSString alloc] initWithUTF8String:
                 (const char *) sqlite3_column_text(selectstmt, 0)];

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:aValue,@"value",aId,@"key", nil];
[aTierOneTemplateList addObject:dict];
[dict release];
[aValue release];
[aId release];

Edit2:

Consider now in your tableviewcontroller, you have array of NSDictionarys (say aTierOneTemplateList).

In cellForRowAtIndexPath method,

NSDictionary *dict = [aTierOneTemplateList objectAtIndex:indexPath.row];
NSString *key = [dict objectForKey:@"key"];
NSString *value = [dict objectForKey:@"value"];

Similarly in didSelectRowAtIndexPath method.

番薯 2025-01-15 02:19:19

NSDictionary 和 NSSet 都不是有序集合。如果您想要排序,请使用 NSArray。如果要强制排序,请将对象添加到具有自己的索引属性的无序集合中。

Neither NSDictionary nor NSSet are ordered collections. If you want ordering, use NSArray. If you want to impose ordering, add objects to unordered collections that have their own index attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文