1

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

 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:] ce 186 38CoreFoundation 07x+ 414 4 CoreFoundation 0x00007fff8a1073b8 _CF_forwarding_prep_0 + 232 5 tsikzoeMig 0x0000000100001cd3 主要 + 851 6 libdyld.dylib 0x00007fff8acd97e1 开始 + 0 7 ??? 0x0000000000000001 0x0 + 1)libc++abi.dylib:终止调用抛出异常(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
4

1 回答 1

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

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

于 2012-12-17T11:51:37.033 回答