gpt4 book ai didi

objective-c - NSDictionary 中保存的搜索数组

转载 作者:行者123 更新时间:2023-12-01 16:53:18 24 4
gpt4 key购买 nike

我有一个要求,我有 5 个键存储在一个 NSDictionary 中,对应于我存储了来自不同数组的 5 个值。我的数据结构如下:

字典是 {

1 =     (
{
SubRegionName = "abc";
WalkId = 123;
WalkName = xyz;
},
{
},....
2 = (
{
SubRegionName = "abc";
WalkId = 123;
WalkName = xyz;
},
{
},....
3 = (
{
SubRegionName = "abc;
WalkId = 123;
WalkName = xyz;
},
{
},....
4 = (
{
SubRegionName = "abc";
WalkId = 123;
WalkName = xyz;
},
{
},....
5 = (
{
SubRegionName = "abc";
WalkId = 123;
WalkName = xyz;
},
{
},
)

我根据一些 Id 说 tempId(1,2,3,4,5) 从 sqlite 获得的上述数据,我想单独使用它们。因为我必须在 tableview 中将 walkName 显示为 cell.textlabel.text 并将 SubregionName 显示为 cell.detailtext.text。

我不知道如何从字典中访问这些数据。谁能建议我正确的方法。

我的代码是:

// 用于数据库查询(从数据库中获取数据) ***********
-(void)getMainRegions
{
sqlite3 *walkNameDB;
if (sqlite3_open([[self databasePath] UTF8String], &walkNameDB) != SQLITE_OK) {
sqlite3_close(walkNameDB);
NSAssert(0, @"Failed to open Walk name database");
}



NSString *regionQuery = @"SELECT Rid,RName from MainRegions";

sqlite3_stmt *teststatement = nil;

// NSLog(@"%d",sqlite3_prepare_v2(walkNameDB,[regionQuery UTF8String], -1, &teststatement, nil));

if (sqlite3_prepare_v2(walkNameDB,[regionQuery UTF8String], -1, &teststatement, nil) == SQLITE_OK)
{
RNameArray = [[NSMutableArray alloc] init];
RIdArray = [[NSMutableArray alloc] init];

while( sqlite3_step(teststatement) == SQLITE_ROW )
{

NSNumber *RId;
int temp1 = (int)sqlite3_column_int(teststatement, 0);
RId = [[NSNumber alloc] initWithInt:temp1];

char *RNameCharacter;
RNameCharacter = (char *) sqlite3_column_text(teststatement, 1);
NSString *RNameString = [[NSString alloc] initWithUTF8String:RNameCharacter];

[RNameArray addObject:RNameString];
[RIdArray addObject:RId];


}

}
[self getWalks];
}

-(void)getWalks
{
sqlite3 *walkNameDB;
if (sqlite3_open([[self databasePath] UTF8String], &walkNameDB) != SQLITE_OK) {
sqlite3_close(walkNameDB);
NSAssert(0, @"Failed to open Walk name database");
}

RegionWalksDictionary = [[NSMutableDictionary alloc] init];

for(regionId in RIdArray)
{
regionWalkArray = [[NSMutableArray alloc] init];
NSString *walkQuery = [[NSString alloc] initWithFormat:@"SELECT Wid,WName,SName from Walks,SubRegions WHERE Walks.Sid=SubRegions.Sid AND Rid = %d",[regionId integerValue] ];

sqlite3_stmt *walkstatement = nil;

// NSLog(@"%d",sqlite3_prepare_v2(walkNameDB,[walkQuery UTF8String], -1, &walkstatement, nil));

if (sqlite3_prepare_v2(walkNameDB,[walkQuery UTF8String], -1, &walkstatement, nil) == SQLITE_OK)
{
while( sqlite3_step(walkstatement) == SQLITE_ROW )
{
NSNumber *WId;
int temp1 = (int)sqlite3_column_int(walkstatement, 0);
WId = [[NSNumber alloc] initWithInt:temp1];

char *WNameCharacter;
WNameCharacter = (char *) sqlite3_column_text(walkstatement, 1);
NSString *WNameString = [[NSString alloc] initWithUTF8String:WNameCharacter];

char *SNameCharacter;
SNameCharacter = (char *) sqlite3_column_text(walkstatement, 2);
NSString *SNameString = [[NSString alloc] initWithUTF8String:SNameCharacter];

NSMutableDictionary *tempWalk = [[NSMutableDictionary alloc] init];
[tempWalk setObject:WId forKey:@"WalkId"];
[tempWalk setObject:WNameString forKey:@"WalkName"];
[tempWalk setObject:SNameString forKey:@"SubRegionName"];
[regionWalkArray addObject:tempWalk];
}
}


[RegionWalksDictionary setObject:regionWalkArray forKey:regionId];
// NSLog(@"arr%@",RegionWalksDictionary);

}

}

// *** 从数据库获取数据的方法到此结束 **************

最佳答案

试试这个方法...

for(id dict in RegionWalksDictionary){

NSLog(@"WalkId: %@",[[[RegionWalksDictionary objectForKey:dict]objectAtIndex:0]objectForKey:@"WalkId"] );
NSLog(@"WalkName: %@",[[[RegionWalksDictionary objectForKey:dict]objectAtIndex:0]objectForKey:@"WalkName"] );
NSLog(@"SubRegionName: %@",[[[RegionWalksDictionary objectForKey:dict]objectAtIndex:0]objectForKey:@"SubRegionName"] );
NSLog(@"----");

}

我使用如下(如果这不是结构或您的数据,请评论)
NSMutableDictionary *RegionWalksDictionary=[NSMutableDictionary new];
NSMutableArray *regionWalkArray=[NSMutableArray new];
NSMutableDictionary *tempWalk=[NSMutableDictionary new];

[tempWalk setObject:@"wk1" forKey:@"WalkId"];
[tempWalk setObject:@"wkName1" forKey:@"WalkName"];
[tempWalk setObject:@"srName1" forKey:@"SubRegionName"];

[regionWalkArray addObject:tempWalk];

[RegionWalksDictionary setObject:regionWalkArray forKey:@"1"];
[RegionWalksDictionary setObject:regionWalkArray forKey:@"2"];
[RegionWalksDictionary setObject:regionWalkArray forKey:@"3"];

NSLog(@"tempWalkd dict is %@",RegionWalksDictionary);

关于objective-c - NSDictionary 中保存的搜索数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13910276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com