我有数组中的数据。我将它们加载到 UITableViewController 中,当我选择一个单元格时,单元格中的数据将添加到数组中。然后我想在核心数据中保存数组中的数据,但我做不到。我只能保存一个文件。我怎样才能做到?
@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)
}
已更新 我重新制作了这段代码,但它保存的是一样的。
@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)
}
更新
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)
}
我试过了,但同样我只保存了数组中的最后一个对象。
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)
}
所以我将 UITableViewController 中的歌曲添加到数组中。
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
}
更新 2
我试过了
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)
}
更新 3 我仍然尝试过这种方法,但我只保存了我选择的第一个对象。有什么方法吗?如何为特定播放列表(另一个实体)保存多首歌曲(对象)?
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)
}
我还是试过了。
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)
}
实体
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
}
更新 4 我试过了,但这种方法保存了几个新的播放列表。我就知道。谁能以编程方式向我展示它?
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)
}
最佳答案
每次选择一行时,您都会创建一个新的播放列表和一首新歌曲。在选择歌曲之前创建您的播放列表,并且仅在您添加新歌曲时更新关系。
关于ios - 如何将多个对象保存到关系实体中。 Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32189030/