2015-02-23 82 views
0

我的應用程序使用核心數據存儲由用戶標記的書籍的信息時遇到一些困難。 它目前將這些信息保存到一個xml文件(沒有核心數據),我不確定如何將其轉換爲使用核心數據。使用XCode的iOS 8.0應用程序的核心數據

到目前爲止,我有這在我的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

您缺少如何使用核心數據的基礎知識。這不是一個教程網站,雖然迷你教程可能會導致SO問題。首先,閱讀有關如何使用核心數據的文檔。 Apple在這方面有非常好的文檔,你可以通過在Xcode中使用幫助來找到它的大部分內容。還有一些非常好的書籍和教程可用。谷歌應該爲你提供超過你的要求。最後,一旦你研究了基本知識,請回答你可能有的任何具體問題。 – 2015-02-24 14:09:29

回答

相關問題