【问题标题】:[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa6a2750[__NSCFString objectForKey:]:无法识别的选择器发送到实例 0xa6a2750
【发布时间】:2014-04-26 18:45:45
【问题描述】:

我将我的数据库值转换为 JSON 格式。它在 URL 中显示为 JSON。然后我从 JSON URL 获取值然后存储到本地数据库。但我遇到以下错误:[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa6a2750*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa6a2750'

URL 中的 JSON 值:

[{"cat_id":"196","category_name":"Performance Parts"},{"cat_id":"212","category_name":"Car Care"}]

获取值并插入到本地:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];

 NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Mdb.sqlite"];
 NSLog(@"filepath %@",path);



 //NSDictionary *jsonDict = [stringFromFileAtURL JSONValue];
 //array

 NSArray *userData = [str JSONValue];

 //  NSLog(@"userData is %@", userData);


 BOOL notExist = TRUE;
 sqlite3_stmt *statement, *addStmt;



 for (NSArray *skarray in userData) {

 NSLog(@"test");


 for (NSDictionary *tuser in skarray) {

 NSLog(@"test1");

 //write all this in the table
 //if already exists in data base id then overwrite the name

 //category table
 //NSLog(@"CategoryId is %@",[tuser objectForKey:@"cat_id"]);
 //NSLog(@"CategoryName is %@",[tuser objectForKey:@"cat_name"]);

 if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

 NSLog(@"test2");


 const char *sql = [[NSString stringWithFormat:@"SELECT cat_id FROM categories where cat_id = '%@'",[tuser objectForKey:@"cat_id"]] cStringUsingEncoding:NSUTF8StringEncoding];

 NSLog(@"test3");


 NSLog(@"check category is %s", sql);

 if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {

 notExist = TRUE;

 while (sqlite3_step(statement) == SQLITE_ROW) {

 notExist = FALSE;

 }

 }


 if(notExist){
 //NSLog(@"cat id does not exist");

 const char *sqlInsert = [[NSString stringWithFormat:@"insert into categories (cat_id, category_name) values ('%@', '%@')", [tuser objectForKey:@"cat_id"], [tuser objectForKey:@"category_name"]] cStringUsingEncoding:NSUTF8StringEncoding];


 NSLog(@"Insert category is %s", sqlInsert);


 if(sqlite3_prepare_v2(database, sqlInsert, -1, &addStmt, NULL) != SQLITE_OK)
 NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));

 if(SQLITE_DONE != sqlite3_step(addStmt))
 NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));





 }

 }



 }
 }

【问题讨论】:

    标签: ios objective-c json sqlite


    【解决方案1】:

    删除for (NSArray *skarray in userData) {循环并使用

    for (NSDictionary *tuser in userData) {
    
        NSLog(@"test1");
        if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
    
         const char *sql = [[NSString stringWithFormat:@"SELECT cat_id FROM categories where cat_id = '%@'",[tuser objectForKey:@"cat_id"]] cStringUsingEncoding:NSUTF8StringEncoding];
    
    
         if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
    
        notExist = TRUE;
    
        while (sqlite3_step(statement) == SQLITE_ROW) {
    
           notExist = FALSE;
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      您正在尝试从NSDictionary(或可变)获取数据,但在您的情况下,它以某种方式转换为NSStringNSCFString)并且没有objectForKey 属性。查看使用objectForKey 的编译步骤。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多