如何将多个对象保存到关系实体中。 Xcode中

时间:2015-08-24 18:17:55

标签: ios arrays xcode swift

我有数组中的数据。我将它们加载到UITableViewController中,当我选择一个单元格时,来自单元格的数据会添加到Array中。然后我想在Core Data中保存数据,但是我无法做到。我只能保存一个文件。我该怎么做?

@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)

    }

的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

}

核心数据中的关系实体 enter image description here

enter image description here enter image description here

更新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)
}

1 个答案:

答案 0 :(得分:1)

每次选择一行时,您都会创建一个新播放列表和一首新歌曲。在选择歌曲之前创建播放列表,仅在添加新歌曲时更新关系。