-1

I have two entities with relationships in coredata:

Entity "Kategorien" has the relation "To Many" to Entity "Details" that is called "kategorie"

enter image description here

In controllerA with "Kategorien" i pick "Kategorien" and reach it to the controller "DetailViewController" with "Details":

Swift:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "MainDetail" {
        let indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!
        let controller = segue.destinationViewController as DetailViewController
        let kat:Kategorien = self.fetchedResultsController.objectAtIndexPath(indexPath) as Kategorien

        controller.kategorie = kat        }
}

In DetailViewController i receive it:

class DetailViewController: UITableViewController, NSFetchedResultsControllerDelegate {

    var kategoie:Kategorien!

In Objective-C kat is a property in DetailViewController.h

@property (strong, nonatomic) Kategorien *kat;

and in the fetchResulController i just said

#pragma mark - Fetched results controllers delegates
- (NSFetchedResultsController *)fetchedResultsController {
...
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"kategorie == %@", kat];

and the records for the kategorie are shown.

With Swift in the fetchResulController i tried it with

fetchRequest.predicate = NSPredicate(format:"kategorie == %@", kategorie)

and other ways (i.e. kategorie.description) but

no records will appear

Whar did i missunderstand? Where is my mistake? It drives me crazy...

Ulli H
  • 1,748
  • 1
  • 19
  • 32
  • Why is it `kategorie` in the creation of the Swift predicate and `kat` in Objective-C? – (If you provide a *link* to a screenshot of your Core Data model then a friendly user might add it to your question :) – Martin R Jan 15 '15 at 18:52
  • Sorry: kategorie is in Swift and kat in Objective-C. I both means the same. How can i provide a link??? – Ulli H Jan 15 '15 at 19:22
  • You could for example upload the image to http://imgur.com and post the link here. – Martin R Jan 15 '15 at 19:25
  • If the property is called `kategory` in the Swift code, then why do you have `var kat:Kategorien!` and not `var kategory:Kategorien!` ? – Martin R Jan 15 '15 at 19:43
  • shit, there stands: var kategorie:Kategorien! That's right. Copy & Paste Error :-) – Ulli H Jan 15 '15 at 19:58
  • It would really be helpful (and save your and our time) if you copy/paste your *real code*: now you have `var kategoie:Kategorien!` which must be a typo again. – That being said, I cannot see a difference between the ObjC and the Swift predicate, both should work, I assume that the problem is somewhere else. What happens if you don't assign a predicate: Do you get all objects? – Martin R Jan 16 '15 at 20:47
  • @ Martin R: if i don't assign a predicate i also see no records... – Ulli H Jan 21 '15 at 13:31
  • Well, that means that the problem is unrelated to the predicate and there must be something else :) – Martin R Jan 21 '15 at 13:51
  • @ Martin R SORRY! I know it's the sabsolute wrong place here for this question, but i don't know how to get help :-( i've recogniced, that you see it, then i add a comment or answer to a question. How can i do that? I found nothing in the help... – Ulli H Jan 21 '15 at 16:54
  • Sorry, I don't understand what you are asking. If you have new information about the problem then you can edit and update your question. – Martin R Jan 21 '15 at 17:35
  • but u always give quick reply! How do u do that? – Ulli H Jan 21 '15 at 17:53

2 Answers2

2

That constructor is not available in swift, since it uses varargs.

Instead of the Objective C constructor:

+ (NSPredicate *)predicateWithFormat:(NSString *)format

use the swift constructor:

init(format predicateFormat: String, 
     argumentArray arguments: [AnyObject]?) -> NSPredicate

So your code should look like:

fetchRequest.predicate = NSPredicate(format:"kategorie == %@", argumentArray:[kategorie])
Alex Brown
  • 41,819
  • 10
  • 94
  • 108
  • THX Alex, but i get the errormessage "extraneous argument label "arguments" in call". May you help??? – Ulli H Jan 16 '15 at 13:53
  • Try the new version. If in doubt, cmd-click on the symbol NSPredicate in Xcode to see the actual API. !code completion is also your friend. – Alex Brown Jan 16 '15 at 15:04
  • yes, i did it this way, but still get no Child-Records (???) Could it be, that the cause is, that i use a sqlite-file migrated from the Objective-C-Version???? – Ulli H Jan 16 '15 at 18:02
  • @AlexBrown: Your statement is not correct. `NSPredicate(format:"kategorie == %@", kategorie)` works just fine in Swift. You find it as `convenience init?(format predicateFormat: String, _ args: CVarArgType...)` in the API. – Martin R Jan 16 '15 at 19:59
  • (lldb) po fetchRequest.predicate say and this is fine: kategorie == (entity: Kategorien; id: 0x79c2a570 ; data: { columns = ""; details = ""; katid = 1; name = "\U00c4rzte/Therapeuten"; order = 0; }) – Ulli H Jan 16 '15 at 20:51
0

i got it! :-) Took a solution from Ray Wenderlichs "Core Data by Tutorials" to manage the MOC:

import CoreData
public class CoreDataStack {  
  let context:NSManagedObjectContext
  let psc:NSPersistentStoreCoordinator
  let model:NSManagedObjectModel
  let store:NSPersistentStore?
  ...

This class is called by AppDelegate.swift (no MOC...) and the ViewControllers. But this allways creates a new MOC! So "Kategorien" and "Details" have own MOCs. So the fetchRequest

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"kategorie == %@", kategorie]

in Details has another MOC as kategorien got from controllerA.

Now i killed the class CoreDataStack and ported all to AppDelegate.swift see Swift: cannot preload Coredata. Now all use the same MOC and everything is fine... :-)

Community
  • 1
  • 1
Ulli H
  • 1,748
  • 1
  • 19
  • 32