【问题标题】:-[__NSArrayM objectForKey:]: unrecognized selector sent to instance Error in Objective c-[__NSArrayM objectForKey:]:无法识别的选择器发送到目标 c 中的实例错误
【发布时间】:2012-04-12 01:03:06
【问题描述】:

我尝试将 Json 数据插入 sqlite 数据库。我从另一个类获取数据,它们在字典(responce.item)中,但我无法插入数据库。我收到这样的错误:无法识别的选择器已发送到实例。我怎么解决这个问题?我的方法如下。感谢您的回复。

修改后的代码:

-(void)processDoneWithRequestName:(NSString*)tagName{

sqlite3_stmt *stmt=nil;
sqlite3 *cruddb;

const char *sql = "INSERT INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate, PackageNo, Status, Description) VALUES (?,?,?,?,?,?,?,?,?,?)";            

//NSArray *pathsArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath=@"/Users/ds/Desktop/SqliteTest/SqliteTest";
NSString *cruddatabase=[doumentDirectoryPath stringByAppendingPathComponent:@"SqliteTestDb.sqlite"];

if ([tagName isEqualToString:Logins]) {

    int keys = [[response.item objectForKey:@"Lipton"] count];
    NSLog(@"count %i",keys);
    for (int i=0; i<keys; i++)
    {

        NSString *str1 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"IsSuccess"];
        NSString *str2 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"ProducerId"];
        NSString *str3 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Latitude"];
        NSString *str4 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Longitude"];
        NSString *str5 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Altitude"];
        NSString *str6 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Slope"];
        NSString *str7 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"SampleDate"];
        NSString *str8 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"PackageNo"];
        NSString *str9 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Status"];
        NSString *str10 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Description"];
        NSLog(@"str1 %@",str1);
        NSLog(@"str2 %@",str2);
        NSLog(@"str3 %@",str3);
        NSLog(@"str4 %@",str4);
        NSLog(@"str5 %@",str5);
        NSLog(@"str6 %@",str6);
        NSLog(@"str7 %@",str7);
        NSLog(@"str8 %@",str8);
        NSLog(@"str9 %@",str9);
        NSLog(@"str10 %@",str10);




        sqlite3_open([cruddatabase UTF8String], &cruddb);
        sqlite3_prepare_v2(cruddb, sql, 1, &stmt, NULL);
        sqlite3_bind_int(stmt, 1, [str1 integerValue]);
        sqlite3_bind_int(stmt, 2, [str2 integerValue]);
        sqlite3_bind_double(stmt, 3, [str3 floatValue]);
        sqlite3_bind_double(stmt, 4, [str4 floatValue]);
        sqlite3_bind_double(stmt, 5, [str5 floatValue]);
        sqlite3_bind_double(stmt, 6, [str6 floatValue]);
        sqlite3_bind_text(stmt, 7, [str7 UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(stmt, 8, [str8 integerValue]);
        sqlite3_bind_int(stmt, 9, [str9 integerValue]);
        sqlite3_bind_text(stmt, 10, [str10 UTF8String], -1, SQLITE_TRANSIENT);


        sqlite3_step(stmt);
        sqlite3_finalize(stmt);
        sqlite3_close(cruddb); 



    }



}

}

【问题讨论】:

  • 您到底遇到了哪个错误。发布整个错误。
  • 您能发布一个 JSON 有效负载的示例吗?
  • 好的,我解决了使用 -objectAtIndex 的问题:但它进入了一次 for 循环。并且数据库文件中没有记录。 :S 我该怎么办?
  • 我编辑了我的代码,现在我可以访问字典中的所有值,但在数据库表中看不到。我该如何解决?

标签: objective-c ios xcode json sqlite


【解决方案1】:

该错误意味着该对象是NSArray 而不是NSDictionary。您可以使用-objectAtIndex: 访问字典对象。

编辑:根据您提供的信息很难判断,但请尝试更改此行:

int keys = [[response.item objectForKey:@"Lipton"] count];

到这里:

int keys = [[[response.item objectForKey:@"Lipton"] objectAtIndex:0] count];

或:

int keys = [[[response.item objectAtIndex:0] objectForKey:@"Lipton"] count];

就像我说的,如果没有更多信息,我无法给出决定性的答案。

【讨论】:

  • 好的,我使用 objectAtIndex 但键的计数为 1,所以我只能输入 1 次。我现在能做什么?
  • 好的,感谢您的回复,我已经解决了这个问题,但现在它在我调用上述代码的 RequestResponce 类中崩溃了 [delegate processDoneWithRequestName:headerTag];我该如何解决这个问题?
  • 您的委托是否实现了该方法?
  • 那个方法在什么类,delegate是什么类?
  • 错误:-[ViewController processDoneWithRequestName:] 中的断言失败,/Users/ds/Desktop/SqliteTest/SqliteTest/ViewController.m:186
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
相关资源
最近更新 更多