我有一個ViewController與保存在NSMutableArray中的播放列表。我使用Singleton將所選對象傳遞給SecondViewController。創建實體之間的關係
optionsSingle.selectedRowNow = [devices objectAtIndex:indexPath.row];
在SecondViewController中,我添加了歌曲。我如何關聯傳遞給SecondViewController的optionsSingle.selectedRowNow與保存的歌曲?我會很感激代碼片段/示例,因爲我一直在努力掙扎幾個小時,並且一直沒能在互聯網上找到有用的東西。
歌曲添加方法:
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSManagedObject *newManagedObject;
newManagedObject = (Songs*)[NSEntityDescription insertNewObjectForEntityForName:@"Songs" inManagedObjectContext:context];
NSDate *today= [NSDate date];
[newManagedObject setValue:video.author forKey:@"author"];
[newManagedObject setValue:video.videoid forKey:@"link"];
[newManagedObject setValue:video.title forKey:@"songName"];
[newManagedObject setValue:today forKey:@"created"];
實體
songs.h
@class Playlists;
@interface Songs : NSManagedObject
@property (nonatomic, retain) NSString * author;
@property (nonatomic, retain) NSDate * created;
@property (nonatomic, retain) NSString * link;
@property (nonatomic, retain) NSString * songName;
@property (nonatomic, retain) NSSet *songs;
@end
@interface Songs (CoreDataGeneratedAccessors)
- (void)addSongsObject:(Playlists *)value;
- (void)removeSongsObject:(Playlists *)value;
- (void)addSongs:(NSSet *)values;
- (void)removeSongs:(NSSet *)values;
@end
playlists.h
@class Songs;
@interface Playlists : NSManagedObject
@property (nonatomic, retain) NSString * playlistName;
@property (nonatomic, retain) NSSet *list;
@end
@interface Playlists (CoreDataGeneratedAccessors)
- (void)addListObject:(Songs *)value;
- (void)removeListObject:(Songs *)value;
- (void)addList:(NSSet *)values;
- (void)removeList:(NSSet *)values;
@end
請注意,您關係的命名是「非常規」,可能是您混淆的原因(請比較我的建議以迴應您之前的問題:http://stackoverflow.com/a/21149021/1187415)。從「歌曲」到「播放列表」的關係應該被稱爲「列表」或「播放列表」,但不是「歌曲」。從「播放列表」到「歌曲」的關係應該被稱爲「歌曲」,而不是「列表」。 –