创建实体之间的关系

时间:2014-01-16 20:18:47

标签: ios objective-c core-data

我有一个带有播放列表的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

1 个答案:

答案 0 :(得分:0)

[optionsSingle.selectedRowNow addListObject:newManagedObject];

这会从'singleton'获取播放列表,并将该关系添加到歌曲中(附加到当前关系中的任何歌曲)。