使用XCode的iOS 8.0应用程序的核心数据

时间:2015-02-23 17:42:39

标签: ios objective-c xcode core-data

我在让我的应用程序使用核心数据来存储由用户标记的书籍时遇到了一些困难。 它目前将此信息保存到xml文件(没有核心数据),我不知道如何将其转换为使用核心数据。

到目前为止,我的AppDelegate.m

中有这个
    // Create Managed Object
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:self.managedObjectContext];

[[self managedObjectContext] save:nil];

然后我尝试使用我创建的实体,称为“相册”,实体中的属性为'albumName'和'albumArtist'两个'字符串'

这是TableViewController.m

中的代码
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        if (searching) {
    // use for interaction with search list
    NSDictionary *tune = [searchResults objectAtIndex:[indexPath row]];

    //check label for system messages
    if ([[tune valueForKey:@"mbid"] intValue] != -1) {
        //Add the new album to your list
        [albums addObject:tune];

        // clear the search text
        [searchBar setText:@""];

        //Remove the cancel/done button from navigation bar
        [[self navigationItem] setRightBarButtonItem:nil];
        [[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];

        //clear the search results and reset state
        searching = NO;
        [searchResults removeAllObjects];

    //trying to use core data here to store the row selected by the user 

        [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
        // step 1 - Create new NSManagedObject for your CoreData model

        // step 2 - Set values for its attributes
        [newAlbum setValue:name.varchar forKey:@"name"];
        [newAlbum setValue:artist.varchar forKey:@"artist"];

        // step 3 - Save NSManagedContext

        // step 4 - Update your list of object in 'albums' for the table view.

        //force the table to reload and redraw
        [[self tableView] reloadData];

        /*
        //Sort albums
        NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        [albums sortUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];

        // this was the original method for Storing data

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *yourArrayFileName = [documentsDirectory stringByAppendingPathComponent:@"albums.xml"];
        [albums writeToFile:yourArrayFileName atomically:YES];
         */

    }
} else {
    // use for interaction with album list

    NSDictionary *tune = [albums objectAtIndex:[indexPath row]];
    FilmDetailViewController *vc = [[FilmDetailViewController alloc]
                                   initWithNibName:@"FilmDetailViewController" bundle:nil];
    [vc setTune:tune];

    [[self navigationController] pushViewController:vc animated:YES]; //slides the view in from the right

}
    }

我试图概述我需要采取的步骤,但不知道从哪里开始。任何帮助将非常感激。 :)

1 个答案:

答案 0 :(得分:0)

网上有很多很好的教程。这是一个非常好的分步教程,可以使用核心数据。 (http://code.tutsplus.com/series/core-data-from-scratch--cms-653

另请查看一些YouTube视频。 (https://www.youtube.com/watch?v=IgNq5-go-ws