1
votes

I am reading jason data from my json file, and i am successful in printing the NSarray type arraytsik, now when i try to loop through the arraytsik using enumerateObjectsUsingBlock it's giving me outup or array with error

 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] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary enumerateObjectsUsingBlock:]: unrecognized selector sent to instance 0x102114700' * First throw call stack: ( 0 CoreFoundation 0x00007fff8a1190a6 exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff8a2963f0 objc_exception_throw + 43 2 CoreFoundation 0x00007fff8a1af6ea -[NSObject(NSObject) doesNotRecognizeSelector:] + 186 3 CoreFoundation 0x00007fff8a1075ce __forwarding + 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 called throwing an exception (lldb)

my main.m code is here

//  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;
}

my json content is

{"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":"དུང་དཀར་ཚིག་མཛོད།"}]}

my TsikzoeE.h code

#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 code

  //
//  TsikzoeE.m


#import "TsikzoeE.h"


@implementation TsikzoeE

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

@end
1

1 Answers

3
votes

The JSON data contain a dictionary (with one key "tsik") and not an array.

The following should work better:

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

There seems also to be a problem inside the enumerateObjectsUsingBlock block: obj is a dictionary here, so

tsikz.word=[obj objectAtIndex:3];

does not work. Perhaps you meant

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

Remark: You could also consider to save the managed object context after the loop has completed instead of saving it for each item added.