ios - 比较数据库和Json的值并显示结果

标签 ios objective-c asihttprequest fmdb sbjson

我正在创建一个包含多列的表格。在左列中,我想获取存储在数据库中的指标名称,我必须检查数据库中的指标 ID 和 json (API) 中的指标 ID 是否匹配,然后在该列中显示名称。我已经尝试了一些代码

在 View 加载方法中,我从数据库中获取值并将其存储在数组中

- (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];

}

在请求完成的方法中,我从 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];
}

我正在使用 FMDB、ASIHTTPRequest、SBJSON、XCMultisortTableView。

请帮忙。

最佳答案

好吧,您正在创建一个空的新 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]];
    }
}

关于ios - 比较数据库和Json的值并显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620218/

相关文章:

ios - 如何在iOS上使用Dispatch Queue问题?

ios - 使用未声明的类型 'MessagingDelegate'

ios - 在 Xcode 6.1 的 plcrashreporter 中使用未声明的标识符 UNWIND_ARM64_MODE_FRAME_OLD

objective-c - 在 Objective C 中为 iPhone SDK 生成随机数

ios - 我应该如何快速获得 OAuth2.0 访问 token ?

ios - AFNetworking 2.0//如何读取响应头

iphone - 跟进 viewDidUnload vs. dealloc 问题

ios - ASIHTTP请求如何处理帐户注册超时

ios - 为什么我无法从网络服务获取数据,我得到反馈“列表不存在”。

ios - 进度条不适用于 ASIHTTPRequest