Ios 比较数据库和Json的值并显示结果
我正在创建一个包含多列的表。在左列中,我想获取存储在数据库中的指示符名称,我必须检查数据库中的指示符id和json(API)中的指示符id是否匹配,然后在该列中显示该名称。我试过一些代码 在视图加载方法中,我从数据库中获取值并将其存储在数组中Ios 比较数据库和Json的值并显示结果,ios,objective-c,asihttprequest,fmdb,sbjson,Ios,Objective C,Asihttprequest,Fmdb,Sbjson,我正在创建一个包含多列的表。在左列中,我想获取存储在数据库中的指示符名称,我必须检查数据库中的指示符id和json(API)中的指示符id是否匹配,然后在该列中显示该名称。我试过一些代码 在视图加载方法中,我从数据库中获取值并将其存储在数组中 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
indicatorName = [[NSMutableArray alloc] init];
NSString *path = appDelegate.databasePath;
FMDatabase *db = [[FMDatabase alloc] initWithPath:path];
[db open];
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Topic AS t INNER JOIN TopicIndicators AS ti ON t.Id = ti.TopicId INNER JOIN Indicators AS i ON ti.IndicatorID = i.Id"];// WHERE t.Id = %ld", (long)self.Tid];
FMResultSet *fresult = [db executeQuery:sql];
while ([fresult next])
{
TopicModel *topicModel = [[TopicModel alloc]init];
topicModel.TId = [fresult intForColumn:@"Id"];
topicModel.TTopicType = [fresult stringForColumn:@"TopicType"];
topicModel.TCode = [fresult stringForColumn:@"Code"];
topicModel.TName = [fresult stringForColumn:@"Name"];
topicModel.IId = [fresult intForColumn:@"Id"];
topicModel.ICodeId = [fresult stringForColumn:@"CodeId"];
topicModel.IName = [fresult stringForColumn:@"Name"];
topicModel.INotes = [fresult stringForColumn:@"Notes"];
topicModel.TIId = [fresult intForColumn:@"Id"];
topicModel.TITopicId = [fresult intForColumn:@"TopicId"];
topicModel.TIIndicatorID = [fresult intForColumn:@"IndicatorId"];
[indicatorName addObject:topicModel];
}
[db close];
mainTableData = [[NSMutableArray alloc] init];
[self callPages];
self.title = NSLocalizedString(@"Compare By", nil);
XCMultiTableView *tableView = [[XCMultiTableView alloc] initWithFrame:CGRectInset(self.view.bounds, 5.0f, 5.0f)];
tableView.leftHeaderEnable = YES;
tableView.datasource = self;
[self.view addSubview:tableView];
}
在RequestFinished方法中,我从json中获取值,并在适当的列中分配值
-(void) requestFinished: (ASIHTTPRequest *) request
{
NSString *theJSON = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSMutableArray *jsonDictionary = [parser objectWithString:theJSON error:nil];
headData = [[NSMutableArray alloc] init];
NSMutableArray *head = [[NSMutableArray alloc] init];
leftTableData = [[NSMutableArray alloc] init];
NSMutableArray *left = [[NSMutableArray alloc] init];
rightTableData = [[NSMutableArray alloc]init];
for (NSMutableArray *dictionary in jsonDictionary)
{
Model *model = [[Model alloc]init];
model.cid = [[dictionary valueForKey:@"cid"]intValue];
model.iid = [[dictionary valueForKey:@"iid"]intValue];
model.yr = [[dictionary valueForKey:@"yr"]intValue];
model.val = [dictionary valueForKey:@"val"];
[head addObject:[NSString stringWithFormat:@"%ld", model.yr]];
[left addObject:[NSString stringWithFormat:@"%ld", model.iid]];
[mainTableData addObject:model];
}
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:head];
headData = [[orderedSet array] mutableCopy];
NSOrderedSet *orderedSet1 = [NSOrderedSet orderedSetWithArray:left];
NSMutableArray *arrLeft = [[orderedSet1 array] mutableCopy];
//remove duplicate enteries from header array
[leftTableData addObject:arrLeft];
NSMutableArray *right = [[NSMutableArray alloc]init];
for (int i = 0; i < arrLeft.count; i++)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int j = 0; j < headData.count; j++)
{
/* NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.iid == %ld", [[arrLeft objectAtIndex:i] intValue]];
NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];*/
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.iid == %ld AND SELF.yr == %ld", [[arrLeft objectAtIndex:i] intValue], [[headData objectAtIndex:j] intValue]];
NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];
NSMutableArray *newArray = [[NSMutableArray alloc] init];
TopicModel *topicModel = [[TopicModel alloc]init];
for (int k = 0; k < arrLeft.count; k++)
{
if (topicModel.IId == arrLeft[k])
{
[newArray addObject:[NSString stringWithFormat:@"%@",topicModel.IName]];
}
}
if([filteredArray count]>0)
{
Model *model = [filteredArray objectAtIndex:0];
[array addObject:model.val];
}
}
[right addObject:array];
}
[rightTableData addObject:right];
}
-(void)requestFinished:(ASIHTTPRequest*)请求
{
NSString*theJSON=[请求-响应字符串];
SBJsonParser*parser=[[SBJsonParser alloc]init];
NSMutableArray*jsonDictionary=[parser objectWithString:theJSON错误:nil];
headData=[[NSMutableArray alloc]init];
NSMutableArray*head=[[NSMutableArray alloc]init];
leftTableData=[[NSMutableArray alloc]init];
NSMUTABLEARRY*左=[[NSMUTABLEARRY alloc]init];
rightTableData=[[NSMutableArray alloc]init];
for(jsonDictionary中的NSMutableArray*字典)
{
模型*Model=[[Model alloc]init];
model.cid=[[dictionary valueForKey:@“cid”]intValue];
model.iid=[[dictionary valueForKey:@“iid”]intValue];
model.yr=[[dictionary valueForKey:@“yr”]intValue];
model.val=[dictionary valueForKey:@“val”];
[head addObject:[NSString stringWithFormat:@“%ld”,model.yr]];
[左添加对象:[NSString stringWithFormat:@“%ld”,model.iid]];
[maintabledataaddobject:model];
}
NSOrderedSet*orderedSet=[NSOrderedSet orderedSetWithArray:head];
headData=[[orderedSet数组]可变表副本];
NSOrderedSet*orderedSet1=[NSOrderedSet orderedSetWithArray:左];
NSMutableArray*arrLeft=[[orderedSet1数组]mutableCopy];
//从标头数组中删除重复的条目
[leftTableData添加对象:arrLeft];
NSMUTABLEARRY*right=[[NSMUTABLEARRY alloc]init];
for(int i=0;i0)
{
模型*Model=[filteredArray对象索引:0];
[数组addObject:model.val];
}
}
[右添加对象:数组];
}
[rightTableData添加对象:右];
}
我正在使用FMDB、ASIHTTPRequest、SBJSON、xcmultiporttableview
请提供帮助。您正在创建新的
TopicModel
对象,该对象为空!您需要使用在viewDidLoad:
方法中设置的方法
for (int k = 0; k < arrLeft.count; k++) {
if ([(TopicModel *)indicatorName[k] IId] == [arrLeft[k] integerValue]) {
[newArray addObject:[NSString stringWithFormat:@"%@",[(TopicModel *)indicatorName[k] IName]];
}
}
for(int k=0;k
它给出错误错误接收器类型“NSInteger”(又名“long”)。我假设这是一个字符串比较,但您正在比较long。更新了答案!仍然收到错误[\uu NSCFString longValue]:发送到实例的选择器无法识别。您的topicModel.IId
的类型是什么?我想在这个循环中,我不会在topicModel中获取值。我不知道如何从view load方法获取这些值