如何保存多个对象到关系的实体。 X $ C $Ç [英] How to save several objects into relationship entity. Xcode
问题描述
我在阵列中的数据。我装他们进入的UITableViewController,当我选择一个单元格,从单元格数据添加到阵列。然后我想将数据从核心数据阵列保存,但我不能做到这一点。我可以只保存一个文件。我怎么做?
@IBAction FUNC saveAllData(发件人:的UIBarButtonItem){
VAR指数= tableView.indexPathForSelectedRow()!
VAR行= tableView.indexPathForSelectedRow()!行
VAR playlistEntity = NSEntityDescription.insertNewObjectForEntityForName(播放列表,inManagedObjectContext:managedObjectContext)作为! NSManagedObject
VAR songEntity = NSEntityDescription.insertNewObjectForEntityForName(宋史,inManagedObjectContext:managedObjectContext)作为! NSManagedObject 如果boolForName == TRUE {
playlistEntity.setValue(currentSong,forKey:namePlaylist)
boolForName = FALSE
} 在arraySong {项目
的println(项目)
songEntity.setValue(项目,forKey:dataSong)
}
playlistEntity.setValue(的NSSet(对象:songEntity),forKey:曲)
managedObjectContext.save(无)
的println(播放列表\\(playlistEntity))
的println(宋实体\\(songEntity))
VAR分镜= UIStoryboard(名称:主,捆绑:NSBundle.mainBundle())
变种导航= storyboard.instantiateViewControllerWithIdentifier(导航)作为!的UINavigationController
presentViewController(NAVI,动画:真的,完成:无)
}
更新
我重拍这个code,但它节省了一样。
@IBAction FUNC saveAllData(发件人:的UIBarButtonItem){
VAR指数= tableView.indexPathForSelectedRow()!
VAR行= tableView.indexPathForSelectedRow()!行
的println(指数)
的println(currentArray [行])
VAR currentFile = currentArray [行] VAR playlistEntity = NSEntityDescription.insertNewObjectForEntityForName(播放列表,inManagedObjectContext:managedObjectContext)作为! NSManagedObject
VAR songEntity = NSEntityDescription.insertNewObjectForEntityForName(宋史,inManagedObjectContext:managedObjectContext)作为! NSManagedObject 如果boolForName == TRUE {
playlistEntity.setValue(currentSong,forKey:namePlaylist)
boolForName = FALSE
} 在arraySong {项目
songEntity.setValue(项目,forKey:dataSong)
playlistEntity.setValue(的NSSet(对象:songEntity),forKey:曲)
managedObjectContext.save(无)
的println(songEntity)
}
managedObjectContext.save(无)
的println(播放列表\\(playlistEntity))
的println(宋实体\\(songEntity))
VAR分镜= UIStoryboard(名称:主,捆绑:NSBundle.mainBundle())
变种导航= storyboard.instantiateViewControllerWithIdentifier(导航)作为!的UINavigationController
presentViewController(NAVI,动画:真的,完成:无)
}
更新
VAR arraySong = [字符串]()
// MARK:FUNC IBAction为
@IBAction FUNC saveAllData(发件人:的UIBarButtonItem){
VAR行= tableView.indexPathForSelectedRow()!行
VAR currentFile = currentArray [行] VAR playlistEntity = NSEntityDescription.insertNewObjectForEntityForName(播放列表,inManagedObjectContext:managedObjectContext)作为! NSManagedObject
VAR songEntity = NSEntityDescription.insertNewObjectForEntityForName(宋史,inManagedObjectContext:managedObjectContext)作为! NSManagedObject 如果boolForName == TRUE {
playlistEntity.setValue(currentSong,forKey:namePlaylist)
boolForName = FALSE
} 在arraySong {项目
songEntity.setValue(项目,forKey:dataSong)
playlistEntity.setValue(的NSSet(对象:songEntity),forKey:曲)
managedObjectContext.save(无)
的println(播放列表\\(playlistEntity))
的println(宋实体\\(songEntity))
}
managedObjectContext.save(无) VAR分镜= UIStoryboard(名称:主,捆绑:NSBundle.mainBundle())
变种导航= storyboard.instantiateViewControllerWithIdentifier(导航)作为!的UINavigationController
presentViewController(NAVI,动画:真的,完成:无)
}
我尝试过,但同样我只保存的最后一个对象从阵列中。
对于VAR X = 0; X - LT; arraySong.count; X ++ {
VAR项目= arraySong [X]
的println(项目)
songEntity.setValue(项目,forKey:dataSong)
playlistEntity.setValue(的NSSet(对象:songEntity),forKey:曲)
managedObjectContext.save(无)
//的println(播放列表\\(playlistEntity))
//的println(宋实体\\(songEntity))
的println(songEntity)
}
所以我加入歌曲的UITableViewController入阵。
覆盖FUNC的tableView(的tableView:UITableView的,didSelectRowAtIndexPath方法indexPath:NSIndexPath){
tableView.selectRowAtIndexPath(indexPath,动画:真,则scrollPosition:UITableViewScrollPosition.None)
VAR行= tableView.indexPathForSelectedRow()!行
VAR歌= currentArray [行]
arraySong.append(歌曲)
VAR细胞= tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
更新2
我试了一下
对于VAR X = 0; X - LT; arraySong.count; X ++ {
VAR项目= arraySong [X]
的println(项目)
VAR songEntity = NSEntityDescription.insertNewObjectForEntityForName(宋史,inManagedObjectContext:managedObjectContext)作为! NSManagedObject
songEntity.setValue(项目,forKey:dataSong)
playlistEntity.setValue(的NSSet(对象:songEntity),forKey:曲)
managedObjectContext.save(无)
//的println(播放列表\\(playlistEntity))
//的println(宋实体\\(songEntity))
的println(songEntity)
}
更新3
还是这个方法我试过,但我救,我只选择第一个对象。是否有任何方法呢?我如何保存播放列表的特定(另一个实体)?
覆盖FUNC的tableView(的tableView:UITableView的,didSelectRowAtIndexPath方法indexPath:NSIndexPath){
tableView.selectRowAtIndexPath(indexPath,动画:真,则scrollPosition:UITableViewScrollPosition.None)
VAR indexPath = tableView.indexPathForSelectedRow()!
VAR行= tableView.indexPathForSelectedRow()!行
VAR歌= currentArray [行]
// arraySong.append(歌曲)
VAR细胞= tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.Checkmark VAR playlistEntity = NSEntityDescription.insertNewObjectForEntityForName(播放列表,inManagedObjectContext:managedObjectContext)作为! NSManagedObject
VAR songEntity = NSEntityDescription.insertNewObjectForEntityForName(宋史,inManagedObjectContext:managedObjectContext)作为! NSManagedObject 如果boolForName == TRUE {
playlistEntity.setValue(currentSong,forKey:namePlaylist)
boolForName = FALSE
} songEntity.setValue(歌曲,forKey:dataSong)
playlistEntity.setValue(的NSSet(对象:songEntity),forKey:曲)
managedObjectContext.save(无) 的println(songEntity)
}
不过我试了一下。
覆盖FUNC的tableView(的tableView:UITableView的,didSelectRowAtIndexPath方法indexPath:NSIndexPath){
tableView.selectRowAtIndexPath(indexPath,动画:真,则scrollPosition:UITableViewScrollPosition.None)
VAR indexPath = tableView.indexPathForSelectedRow()!
VAR行= tableView.indexPathForSelectedRow()!行
VAR歌= currentArray [行]
arraySong.append(歌曲)
VAR细胞= tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.Checkmark 让playlistEntity = NSEntityDescription.insertNewObjectForEntityForName(播放列表,inManagedObjectContext:managedObjectContext)作为! NSManagedObject
让songEntity = NSEntityDescription.insertNewObjectForEntityForName(宋史,inManagedObjectContext:managedObjectContext)作为! NSManagedObject 如果boolForName == TRUE {
playlistEntity.setValue(currentSong,forKey:namePlaylist)
boolForName = FALSE
} 对于VAR位数= 0;数字< arraySong.count;数字+ {
VAR savedSong = arraySong [数字]
songEntity.setValue(savedSong,forKey:dataSong)
playlistEntity.setValue(套装(arrayLiteral:songEntity),forKey:曲)
managedObjectContext.save(无) }
的entites
进口基金会
进口CoreData@objc(播放列表)
一流的播放列表:NSManagedObject { @NSManaged VAR namePlaylist:字符串
@NSManaged VAR曲:的NSSet
}进口基金会
进口CoreData@objc(宋)
宋级:NSManagedObject { @NSManaged VAR dataSong:字符串
@NSManaged VAR播放列表:播放列表}
更新4
我试着像不过这种方法节省了几个新的播放列表。我就知道。谁能告诉我编程吗?
覆盖FUNC的tableView(的tableView:UITableView的,didSelectRowAtIndexPath方法indexPath:NSIndexPath){
tableView.selectRowAtIndexPath(indexPath,动画:真,则scrollPosition:UITableViewScrollPosition.None)
//
让indexP = tableView.indexPathForSelectedRow()!
让行= tableView.indexPathForSelectedRow()!行
VAR歌= currentArray [行] VAR细胞= tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.Checkmark 让playlistEntity = NSEntityDescription.insertNewObjectForEntityForName(播放列表,inManagedObjectContext:managedObjectContext)作为! NSManagedObject
让songEntity = NSEntityDescription.insertNewObjectForEntityForName(宋史,inManagedObjectContext:managedObjectContext)作为! NSManagedObject songEntity.setValue(歌曲,forKey:dataSong)
playlistEntity.setValue(套装(arrayLiteral:songEntity),forKey:曲)
的println(songEntity)
的println(playlistEntity)
managedObjectContext.save(无)
}
每个选择您要制作一个新的播放列表和一个新的歌曲一排的时间。选择歌曲之前创建播放列表,只有当你添加新的歌曲更新的关系。
I have data in array. I loaded them into UITableViewController, and when I select a cell, data from cell adds to Array. Then I want to save data from Array in Core Data, but I cannot make it. I can to save only one file. How can I make it?
@IBAction func saveAllData(sender: UIBarButtonItem) {
var index = tableView.indexPathForSelectedRow()!
var row = tableView.indexPathForSelectedRow()!.row
var playlistEntity = NSEntityDescription.insertNewObjectForEntityForName("Playlist", inManagedObjectContext: managedObjectContext) as! NSManagedObject
var songEntity = NSEntityDescription.insertNewObjectForEntityForName("Song", inManagedObjectContext: managedObjectContext) as! NSManagedObject
if boolForName == true {
playlistEntity.setValue(currentSong, forKey: "namePlaylist")
boolForName = false
}
for item in arraySong {
println(item)
songEntity.setValue(item, forKey: "dataSong")
}
playlistEntity.setValue(NSSet(object: songEntity), forKey: "song")
managedObjectContext.save(nil)
println("play list \(playlistEntity)")
println("song entity \(songEntity)")
var storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
var navi = storyboard.instantiateViewControllerWithIdentifier("navi") as! UINavigationController
presentViewController(navi, animated: true, completion: nil)
}
Updated I remake this code, but it saves the same.
@IBAction func saveAllData(sender: UIBarButtonItem) {
var index = tableView.indexPathForSelectedRow()!
var row = tableView.indexPathForSelectedRow()!.row
println(index)
println(currentArray[row])
var currentFile = currentArray[row]
var playlistEntity = NSEntityDescription.insertNewObjectForEntityForName("Playlist", inManagedObjectContext: managedObjectContext) as! NSManagedObject
var songEntity = NSEntityDescription.insertNewObjectForEntityForName("Song", inManagedObjectContext: managedObjectContext) as! NSManagedObject
if boolForName == true {
playlistEntity.setValue(currentSong, forKey: "namePlaylist")
boolForName = false
}
for item in arraySong {
songEntity.setValue(item, forKey: "dataSong")
playlistEntity.setValue(NSSet(object: songEntity), forKey: "song")
managedObjectContext.save(nil)
println(songEntity)
}
managedObjectContext.save(nil)
println("play list \(playlistEntity)")
println("song entity \(songEntity)")
var storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
var navi = storyboard.instantiateViewControllerWithIdentifier("navi") as! UINavigationController
presentViewController(navi, animated: true, completion: nil)
}
UPDATE
var arraySong = [String]()
// MARK: IBAction func
@IBAction func saveAllData(sender: UIBarButtonItem) {
var row = tableView.indexPathForSelectedRow()!.row
var currentFile = currentArray[row]
var playlistEntity = NSEntityDescription.insertNewObjectForEntityForName("Playlist", inManagedObjectContext: managedObjectContext) as! NSManagedObject
var songEntity = NSEntityDescription.insertNewObjectForEntityForName("Song", inManagedObjectContext: managedObjectContext) as! NSManagedObject
if boolForName == true {
playlistEntity.setValue(currentSong, forKey: "namePlaylist")
boolForName = false
}
for item in arraySong {
songEntity.setValue(item, forKey: "dataSong")
playlistEntity.setValue(NSSet(object: songEntity), forKey: "song")
managedObjectContext.save(nil)
println("play list \(playlistEntity)")
println("song entity \(songEntity)")
}
managedObjectContext.save(nil)
var storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
var navi = storyboard.instantiateViewControllerWithIdentifier("navi") as! UINavigationController
presentViewController(navi, animated: true, completion: nil)
}
I tried it but the same I save only the last object from the array.
for var x = 0; x < arraySong.count; x++ {
var item = arraySong[x]
println(item)
songEntity.setValue(item, forKey: "dataSong")
playlistEntity.setValue(NSSet(object: songEntity), forKey: "song")
managedObjectContext.save(nil)
// println("play list \(playlistEntity)")
// println("song entity \(songEntity)")
println(songEntity)
}
So I am adding songs from UITableViewController into the array.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
var row = tableView.indexPathForSelectedRow()!.row
var song = currentArray[row]
arraySong.append(song)
var cell = tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
UPDATE 2
I tried it
for var x = 0; x < arraySong.count; x++ {
var item = arraySong[x]
println(item)
var songEntity = NSEntityDescription.insertNewObjectForEntityForName("Song", inManagedObjectContext: managedObjectContext) as! NSManagedObject
songEntity.setValue(item, forKey: "dataSong")
playlistEntity.setValue(NSSet(object: songEntity), forKey: "song")
managedObjectContext.save(nil)
// println("play list \(playlistEntity)")
// println("song entity \(songEntity)")
println(songEntity)
}
UPDATE 3 Still I tried this method but I save only first object which I selected. Are there any methods for it? How can I save several songs( objects) for specific playlist (another entity)?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
var indexPath = tableView.indexPathForSelectedRow()!
var row = tableView.indexPathForSelectedRow()!.row
var song = currentArray[row]
// arraySong.append(song)
var cell = tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
var playlistEntity = NSEntityDescription.insertNewObjectForEntityForName("Playlist", inManagedObjectContext: managedObjectContext) as! NSManagedObject
var songEntity = NSEntityDescription.insertNewObjectForEntityForName("Song", inManagedObjectContext: managedObjectContext) as! NSManagedObject
if boolForName == true {
playlistEntity.setValue(currentSong, forKey: "namePlaylist")
boolForName = false
}
songEntity.setValue(song, forKey: "dataSong")
playlistEntity.setValue(NSSet(object: songEntity), forKey: "song")
managedObjectContext.save(nil)
println(songEntity)
}
Still I tried it.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
var indexPath = tableView.indexPathForSelectedRow()!
var row = tableView.indexPathForSelectedRow()!.row
var song = currentArray[row]
arraySong.append(song)
var cell = tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
let playlistEntity = NSEntityDescription.insertNewObjectForEntityForName("Playlist", inManagedObjectContext: managedObjectContext) as! NSManagedObject
let songEntity = NSEntityDescription.insertNewObjectForEntityForName("Song", inManagedObjectContext: managedObjectContext) as! NSManagedObject
if boolForName == true {
playlistEntity.setValue(currentSong, forKey: "namePlaylist")
boolForName = false
}
for var digit = 0; digit < arraySong.count; digit++ {
var savedSong = arraySong[digit]
songEntity.setValue(savedSong, forKey: "dataSong")
playlistEntity.setValue(Set(arrayLiteral: songEntity), forKey: "song")
managedObjectContext.save(nil)
}
Entites
import Foundation
import CoreData
@objc(Playlist)
class Playlist: NSManagedObject {
@NSManaged var namePlaylist: String
@NSManaged var song: NSSet
}
import Foundation
import CoreData
@objc(Song)
class Song: NSManagedObject {
@NSManaged var dataSong: String
@NSManaged var playlist: Playlist
}
Relationships entities in Core Data
UPDATE 4 I tried like but this method saves several new playlists. I knew it. Can anyone show me programmatically it?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
//
let indexP = tableView.indexPathForSelectedRow()!
let row = tableView.indexPathForSelectedRow()!.row
var song = currentArray[row]
var cell = tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
let playlistEntity = NSEntityDescription.insertNewObjectForEntityForName("Playlist", inManagedObjectContext: managedObjectContext) as! NSManagedObject
let songEntity = NSEntityDescription.insertNewObjectForEntityForName("Song", inManagedObjectContext: managedObjectContext) as! NSManagedObject
songEntity.setValue(song, forKey: "dataSong")
playlistEntity.setValue(Set(arrayLiteral: songEntity), forKey: "song")
println(songEntity)
println(playlistEntity)
managedObjectContext.save(nil)
}
Each time you select a row you are creating one new playlist and one new song. Create your playlist before selecting songs and only update the relationship when you add a new song.
这篇关于如何保存多个对象到关系的实体。 X $ C $Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!