核心数据不保存 NSString

发布于 2024-11-03 14:51:17 字数 4120 浏览 1 评论 0原文

目前在我的核心数据保存中发现一个问题。由于某种原因,NSString 没有被正确保存,无论如何总是返回 0。下面是我当前的代码。

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 *notecreated = [singleNote objectForKey:@"note created"];
                NSString *noteupdated = [singleNote valueForKey:@"note updated"];
                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];


                NSLog(@"Note Title: %@",formattedTitle);
                NSLog(@"Note Summary: %@",formattedSummary);
                NSLog(@"Note ID: %@",noteid);
                NSLog(@"Note Content: %@",formattedContent);
                NSLog(@"Note Created: %@",notecreated);
                NSLog(@"Note Updated: %@",noteupdated);
                NSLog(@"Note Locked: %@",notelocked);

                newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:appDelegate.managedObjectContext];
                [newNote autorelease];

                [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:@"updated"];
                [newNote setValue:notelocked forKey:@"locked"];

                NSLog(@"Note Updated in MOC:%@",[newNote valueForKey:@"updated"]);

                NSError *error = nil;
                if (![appDelegate.managedObjectContext save:&error]) {
                    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                    abort();
                }


            }

            [noteFetch release];

        }

尝试保存更新的字符串时会出现问题。由于某种原因,无论其中包含什么字符串,它都不会保存到 Core Data 属性中。

我正在努力寻找任何不储蓄的连贯理由,但我即将认为这是可能的。这是该应用程序上架之前需要解决的最后一个问题。任何帮助都会很棒!

编辑

没有错误,只有调试控制台输出如下。

 2011-04-24 10:53:22.815 Notacious[5660:707] Note Title: Hello there!
 2011-04-24 10:53:22.816 Notacious[5660:707] Note Summary: 
 2011-04-24 10:53:22.817 Notacious[5660:707] Note ID: 1240
 2011-04-24 10:53:22.817 Notacious[5660:707] Note Content: Hello there!

 This is a new note made from the iPhone client. Woot?
 2011-04-24 10:53:22.818 Notacious[5660:707] Note Created: Thu Apr 21 19:19:39 -0700 2011
 2011-04-24 10:53:22.819 Notacious[5660:707] Note Updated: Thu Apr 21 19:19:39 -0700 2011
 2011-04-24 10:53:22.820 Notacious[5660:707] Note Locked: 0
 2011-04-24 10:53:22.823 Notacious[5660:707] Note Updated in MOC:0

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.

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 *notecreated = [singleNote objectForKey:@"note created"];
                NSString *noteupdated = [singleNote valueForKey:@"note updated"];
                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];


                NSLog(@"Note Title: %@",formattedTitle);
                NSLog(@"Note Summary: %@",formattedSummary);
                NSLog(@"Note ID: %@",noteid);
                NSLog(@"Note Content: %@",formattedContent);
                NSLog(@"Note Created: %@",notecreated);
                NSLog(@"Note Updated: %@",noteupdated);
                NSLog(@"Note Locked: %@",notelocked);

                newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:appDelegate.managedObjectContext];
                [newNote autorelease];

                [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:@"updated"];
                [newNote setValue:notelocked forKey:@"locked"];

                NSLog(@"Note Updated in MOC:%@",[newNote valueForKey:@"updated"]);

                NSError *error = nil;
                if (![appDelegate.managedObjectContext save:&error]) {
                    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                    abort();
                }


            }

            [noteFetch release];

        }

The issue arises when trying to save the updated string. For some reason, regardless of what string is in there, it will not save to the Core Data attribute.

I'm struggling to find any coherent reason for it to not be saving, but I'm about to deem it possible. This is the last issue that needs to be sorted before this application can hit the shelves. Any help would be great!

Edit

There is no error, only the following output in the debug console.

 2011-04-24 10:53:22.815 Notacious[5660:707] Note Title: Hello there!
 2011-04-24 10:53:22.816 Notacious[5660:707] Note Summary: 
 2011-04-24 10:53:22.817 Notacious[5660:707] Note ID: 1240
 2011-04-24 10:53:22.817 Notacious[5660:707] Note Content: Hello there!

 This is a new note made from the iPhone client. Woot?
 2011-04-24 10:53:22.818 Notacious[5660:707] Note Created: Thu Apr 21 19:19:39 -0700 2011
 2011-04-24 10:53:22.819 Notacious[5660:707] Note Updated: Thu Apr 21 19:19:39 -0700 2011
 2011-04-24 10:53:22.820 Notacious[5660:707] Note Locked: 0
 2011-04-24 10:53:22.823 Notacious[5660:707] Note Updated in MOC:0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀梦 2024-11-10 14:51:17

您真的确定字典中的“注释更新”不是 NSDate 而不是 NSString 吗?日志输出使它看起来很糟糕,就像里面有一个 NSDate 一样。

如果是这样,您需要使用 NSDateFormatter 将其保存到 NSString 对象属性中。

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.

羁绊已千年 2024-11-10 14:51:17

以下代码已起作用。

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

        }

CoreData 似乎不喜欢我访问名为 updated 的属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文