[__NSCFDictionary enumerateObjectsUsingBlock:]:无法识别的选择器发送到实例0x101918a10'

时间:2012-12-17 11:42:15

标签: ios xcode json core-data

我正在从我的json文件中读取jason数据,并且我成功打印了NSarray类型的arraytsik,现在当我尝试使用enumerateObjectsUsingBlock循环遍历arrayts时,它给了我outup或者数组有错误

 2012-12-17 16:59:04.711 tsikzoeMig[6416:303] here i am printing the array {
    tsik =     (
                {
            author = 2;
            authorName = fgfg;
            dateSubmitted = "2012-08-19 00:00:00";
            dictionaryType = 1;
            id = 1;
            word = fgdf;
            wordDef = fdgdfg;
        },
                {
            author = 2;
            authorName = "\U0f51\U0f74\U0f44\U0f0b\U0f51\U0f40\U0f62\U0f0b\U0f5a\U0f72\U0f42\U0f0b\U0f58\U0f5b\U0f7c\U0f51\U0f0d";
            dateSubmitted = "2012-08-19 00:00:00";
            dictionaryType = 1;
            id = 2;
            word = "\U0f21\U0f20\U0f24\U0f20\U0f56\U0f66\U0f7c\U0f0d";
            wordDef = "\U0f56\U0f66\U0f7c\U0f0b\U0f56\U0f66\U0f7c\U0f0b\U0f5e\U0f7a\U0f66\U0f0b\U0f54\U0f60\U0f72\U0f0b\U0f66\U0f92\U0fb2\U0f60\U0f72\U0f0b\U0f41\U0fb1\U0f51\U0f0b\U0f54\U0f62\U0f0b\U0f42\U0fb1\U0f72\U0f0b\U0f58\U0f72\U0f44\U0f0c\U0f0d";
        }
    );
}
2012-12-17 16:59:04.712 tsikzoeMig[6416:303] -[__NSCFDictionary enumerateObjectsUsingBlock:]: unrecognized selector sent to instance
     

0x102114700       2012-12-17 16:59:04.713 tsikzoeMig [6416:303] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:   ' - [__ NSCFDictionary enumerateObjectsUsingBlock:]:无法识别   选择器发送到实例0x102114700'       * 第一次抛出调用堆栈:       (         0 CoreFoundation 0x00007fff8a1190a6 exceptionPreprocess + 198         1 libobjc.A.dylib 0x00007fff8a2963f0 objc_exception_throw + 43         2 CoreFoundation 0x00007fff8a1af6ea - [NSObject(NSObject)doesNotRecognizeSelector:] + 186         3 CoreFoundation 0x00007fff8a1075ce __ 转发 + 414         4 CoreFoundation 0x00007fff8a1073b8 _CF_forwarding_prep_0 + 232         5 tsikzoeMig 0x0000000100001cd3 main + 851         6 libdyld.dylib 0x00007fff8acd97e1 start + 0         7 ??? 0x0000000000000001 0x0 + 1       )       libc ++ abi.dylib:terminate调用抛出异常       (LLDB)

我的main.m代码在这里

//  main.m

#import "TsikzoeE.h"
static NSManagedObjectModel *managedObjectModel()
{
    static NSManagedObjectModel *model = nil;
    if (model != nil) {
        return model;
    }

    NSString *path = @"tsikzoe";
    NSURL *modelURL = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"momd"]];
    model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    return model;
}

static NSManagedObjectContext *managedObjectContext()
{
    static NSManagedObjectContext *context = nil;
    if (context != nil) {
        return context;
    }

    @autoreleasepool {
        context = [[NSManagedObjectContext alloc] init];

        NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel()];
        [context setPersistentStoreCoordinator:coordinator];

        NSString *STORE_TYPE = NSSQLiteStoreType;

        NSString *path = [[NSProcessInfo processInfo] arguments][0];
        path = [path stringByDeletingPathExtension];
        NSURL *url = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"sqlite"]];

        NSError *error;
        NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE configuration:nil URL:url options:nil error:&error];

        if (newStore == nil) {
            NSLog(@"Store Configuration Failure %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
        }
    }
    return context;
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        // Create the managed object context
        NSManagedObjectContext *context = managedObjectContext();

        // Custom code here...
        // Save the managed object context
        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
            exit(1);
        }
        NSError* err = nil;

        NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"tsikzoe" ofType:@"json"];
        NSArray* arraytsik = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]options:kNilOptions error:&err];

        // array outputing here to check

        NSLog(@"here i am printing the array %@",arraytsik);


        [arraytsik enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {


           TsikzoeE *tsikz = [NSEntityDescription insertNewObjectForEntityForName:@"TsikzoeE"inManagedObjectContext:context];
            tsikz.id = [obj objectForKey:@"id"];
            tsikz.dictionaryType=[obj objectForKey:@"dictionaryType"];
            tsikz.word=[obj objectAtIndex:3];
            tsikz.wordDef=[obj objectForKey:@"wordDef"];
            tsikz.author=[obj objectForKey:@"author"];
            tsikz.authorName=[obj objectForKey:@"authorName"];
            NSError *error;
            if (![context save:&error]) {
                NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
            }
        }];

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"TsikzoeE"
                                                  inManagedObjectContext:context];
        [fetchRequest setEntity:entity];
        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
        for (TsikzoeE *tsikz in fetchedObjects) {
            NSLog(@"word: %@", tsikz.word);

            NSLog(@"wordDef: %@", tsikz.wordDef);
        }

    }
    return 0;
}

我的json内容是

{"tsik":[{"id":1,"dictionaryType":1,"word":"fgdf","wordDef":"fdgdfg","dateSubmitted":"2012-08-19 00:00:00","author":2,"authorName":"fgfg"},{"id":2,"dictionaryType":1,"word":"༡༠༤༠བསོ།","wordDef":"བསོ་བསོ་ཞེས་པའི་སྒྲའི་ཁྱད་པར་གྱི་མིང༌།","dateSubmitted":"2012-08-19 00:00:00","author":2,"authorName":"དུང་དཀར་ཚིག་མཛོད།"}]}

我的TsikzoeE.h代码

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface TsikzoeE : NSManagedObject

@property (nonatomic, retain) NSNumber * author;
@property (nonatomic, retain) NSString * authorName;
@property (nonatomic, retain) NSNumber * dictionaryType;
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * word;
@property (nonatomic, retain) NSString * wordDef;


@end

tsikzoeE.m代码

  //
//  TsikzoeE.m


#import "TsikzoeE.h"


@implementation TsikzoeE

@dynamic author;
@dynamic authorName;
@dynamic dictionaryType;
@dynamic id;
@dynamic word;
@dynamic wordDef;

@end

1 个答案:

答案 0 :(得分:3)

JSON数据包含字典(带有一个键“tsik”)而不是数组

以下情况应该更好:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath] options:kNilOptions error:&err];
NSArray *arraytsik = [dict objectForKey:@"tsik"];

enumerateObjectsUsingBlock块内似乎也存在问题:obj这里是一个字典,所以

tsikz.word=[obj objectAtIndex:3];

不起作用。也许你的意思是

tsikz.word=[obj objectForKey:@"word"];

备注:您还可以考虑在循环完成后保存托管对象上下文,而不是为每个添加的项目保存它。