4

I have a test app with two entities, folder with a relationship of To Many with another object called List.

In my storyboard I have a tableview controller with the list of created folders. When tapping on a folder I segue to another TableView passing on the selectedFolder which should display the List ordered set saved to the selectedFolder. I have a modal that appears to add a item to the List.

Unfortunately I have not been able to save a List to the selectedFolder ordered set. I receive an error when executing the save function unrecognized selector sent to instance this error is because of the following line:

selectedFolder.list = list.copy() as! NSOrderedSet

I am not sure what I am doing wrong with the save function and was wondering if anyone could help, it would be much appreciated.

Folder Subclass:

class Folder: NSManagedObject {

@NSManaged var title: String
@NSManaged var details: String
@NSManaged var date: NSDate
@NSManaged var list: NSOrderedSet

}

List Subclass

class List: NSManagedObject {

@NSManaged var item: String
@NSManaged var folder: Event

}

Modal View to add List to selected Folder ordered set.

class PopoverViewController: UIViewController {

//selectedFolder passed from segue. Works fine displays title of folder
var selectedFolder: Folder!

@IBOutlet weak var popoverTextField: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.popoverTextField.becomeFirstResponder()
    // Do any additional setup after loading the view.
}


@IBAction func addListItem(sender: AnyObject) {

    //Get the context
    let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

    //get entity details
    let entity = NSEntityDescription.entityForName("List", inManagedObjectContext: moc!)

    //Create the managed object to be inserted
    let list = List(entity: entity!, insertIntoManagedObjectContext: moc!)

    // Add Text 

    list.item = popoverTextField.text

    //Insert the new checklist into the folder set

    var folder = selectedFolder.list.mutableCopy() as! NSMutableOrderedSet
    folder.addObject(list)

    selectedFolder.list = list.copy() as! NSOrderedSet

    //Error check & Save

    var error: NSError?
    if moc!.save(&error){
      println("Could not save: \(error)")
    }
}
JUSDEV
  • 731
  • 1
  • 7
  • 22
  • Does your code crash ? How have you used copy, is it possible to use copy with NSManagedObject ? You should either use generated core data methods or use kvc to get mutableSetValueForKey: method on the target object to get mutable set. – Sandeep Jun 22 '15 at 18:07
  • Is the `Folder` class what you described as `Event`? Is the `list` property of `Folder` what is described as `checklist`?? – Martin R Jun 22 '15 at 18:13
  • Assigning `list.copy()` to `selectedFolder.list` makes no sense, did you mean `selectedFolder.list = folder` ? – Martin R Jun 22 '15 at 18:16
  • @MartinR Sorry about that updated the code. I changed it to `selectedFolder.list = folder` and I am sure that works now. Just fixing another bug to view the Set as an array. – JUSDEV Jun 22 '15 at 18:45
  • @JUSDEV: That is what I provided as an answer, plus alternative solutions :) – Martin R Jun 22 '15 at 18:47

1 Answers1

4
var folder = selectedFolder.list.mutableCopy() as! NSMutableOrderedSet
folder.addObject(list)
selectedFolder.list = list.copy() as! NSOrderedSet

The last assignment makes no sense because list is a List object and not an (ordered) set, and btw. this computes a folder variable which is then ignored.

What you probably meant (and this should work) is

var folder = selectedFolder.list.mutableCopy() as! NSMutableOrderedSet
folder.addObject(list)
selectedFolder.list = folder

As already mentioned in a comment, adding to an ordered relationship is a bit tricky, but can be done with Key-Value coding as

let mutableChecklist = selectedFolder.mutableOrderedSetValueForKey("list")
mutableChecklist.addObject(list)

(Compare Exception thrown in NSOrderedSet generated accessors and Setting an NSManagedObject relationship in Swift.)

In the case of a one-to-many relationship however, the easiest way is to set the other direction of the relationship:

list.folder = selectedFolder
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thank you ! Not many resources for new swift developers working with core data so learning through just trial and error. – JUSDEV Jun 22 '15 at 18:50