Core Data not saving NSString

后端 未结 2 1718
时光取名叫无心
时光取名叫无心 2021-01-26 16:14

Currently seeing an issue in my Core Data saving. For some reason a NSString isn\'t being saved properly, always returning 0 regardless. Below is my current code.



        
相关标签:
2条回答
  • 2021-01-26 16:29

    Are you really sure "note updated" in the dictionary is not an NSDate instead of an NSString? The log output makes it look an AWFUL lot like you have an NSDate in there.

    You would need to use an NSDateFormatter to save it into an NSString object property if so.

    0 讨论(0)
  • 2021-01-26 16:40

    The following code has worked.

    appDelegate =(NotaciousAppDelegate *) [[UIApplication sharedApplication] delegate];
    
            NSEntityDescription *entity = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:appDelegate.managedObjectContext];
            NSArray *fetchedNotes;
    
            NSArray *notes = [object valueForKey:@"result"];
            for (NSDictionary *singleNote in notes){
    
                noteFetch=[[NSFetchRequest alloc] init];
                [noteFetch setEntity:entity];
                NSPredicate *pred=[NSPredicate predicateWithFormat:@"ID==%@",[singleNote objectForKey:@"note id"]];
                [noteFetch setPredicate:pred];
    
                NSError *fetchError=nil;
                fetchedNotes=[appDelegate.managedObjectContext executeFetchRequest:noteFetch error:&fetchError];
    
                if (fetchError!=nil) {
                    NSLog(@"syncNotes fetchError=%@,details=%@",fetchError,fetchError.userInfo);
                }
                if ([fetchedNotes count]==0) {
    
                    NSString *notelocked = [singleNote objectForKey:@"note locked"];
    
                    NSString *noteupdated = [singleNote valueForKey:@"note updated"];
                    NSString *notecreated = [singleNote objectForKey:@"note created"];
                    NSString *notetitle = [singleNote objectForKey:@"note title"];
                    NSString *notesummary = [singleNote objectForKey:@"note summary"];
                    NSString *noteid = [singleNote objectForKey:@"note id"];
                    NSString *notecontent = [singleNote objectForKey:@"note content"];
    
                    NSString *formattedTitle = [NSString decodeShit:notetitle];
                    NSString *formattedSummary = [NSString decodeShit:notesummary];
                    NSString *formattedContent = [NSString decodeShit:notecontent];
    
                    newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:appDelegate.managedObjectContext];
    
                    [newNote setValue:formattedContent forKey:@"content"];
                    [newNote setValue:formattedSummary forKey:@"summary"];
                    [newNote setValue:formattedTitle forKey:@"title"];
                    [newNote setValue:noteid forKey:@"ID"];
                    [newNote setValue:notecreated forKey:@"created"];
                    [newNote setValue:noteupdated forKey:@"noteupdated"];
                    [newNote setValue:notelocked forKey:@"locked"];
    
                    NSError *error = nil;
                    if (![appDelegate.managedObjectContext save:&error]) {
                        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                        abort();
                    }
    
    
                }
    
                [noteFetch release];
    
            }
    

    It seems that CoreData didn't like me accessing an attribute with the name updated.

    0 讨论(0)
提交回复
热议问题