0

I am getting stuck, and this makes me crazy. I have a function that returns a NSManagedObject:

let getaccount = AccountModel.getFirst(globals.managedObjectContext)

and I have to cast it into its generated representation Account

I'll do this:

dump("--------------------")
dump(getaccount)
dump("--------------------")
if let acc = getaccount as? Account {
    print("OK")
} else {
    print("not convertable ")
}
dump("*********************")

And the output FROM MY UNITTEST looks like this:

- --------------------
▿ Ticketstream.Account
  ▿ Some: Coredataexampleapp.Account #0
    ▿ super: <Coredataexampleapp.Account: 0x138d85f90> (entity: Account; id: 0x1398a9fe0 <x-coredata://06F96EF3-96A6-4C32-A6A2-F16BCB8318EA/Account/p1> ; data: {
    active = 0;
    avatar =     (
    );
    config =     (
    );
    createdAt = "2015-07-31 23:34:58 +0000";
    displayName = "My Account (1)";
    host = localhost;
    lastUpdatedDate = "2015-07-31 23:34:58 +0000";
    password = nil;
    port = 8080;
    schema = HTTPS;
    username = "";
})
      - NSObject: <Coredataexampleapp.Account: 0x138d85f90> (entity: Account; id: 0x1398a9fe0 <x-coredata://06F96EF3-96A6-4C32-A6A2-F16BCB8318EA/Account/p1> ; data: {
    active = 0;
    avatar =     (
    );
    config =     (
    );
    createdAt = "2015-07-31 23:34:58 +0000";
    displayName = "My Account (1)";
    host = localhost;
    lastUpdatedDate = "2015-07-31 23:34:58 +0000";
    password = nil;
    port = 8080;
    schema = HTTPS;
    username = "";
})
- --------------------
not convertable 
- *********************

How to cast a NSManagedObject into Account?

I need this, because I can't get the first Account otherwise than:

class func getFirst(context: NSManagedObjectContext) -> NSManagedObject? {
    let request = NSFetchRequest(entityName: accountEntityName)
    request.fetchLimit = 1
    let list: NSArray = try! context.executeFetchRequest(request)
    return list.firstObject as? NSManagedObject
}

I've tried this with NSManagedObject, AnyObject and Account... How do i get the first Account the right way?

UPDATE: In normal running mode: all is fine! I'll get:

- --------------------
OK
- *********************

But not from my tests :-(

Thanks for your help, I am lost.

Peter Shaw
  • 1,867
  • 1
  • 19
  • 32
  • Isn't the problem that the managed object model belongs to the app target, not to the test target? – matt Jul 31 '15 at 23:57
  • I checked this a couple of times. Sadly no. – Peter Shaw Aug 01 '15 at 00:16
  • @PeterShaw: This thread http://stackoverflow.com/questions/25076276/unable-to-find-specific-subclass-of-nsmanagedobject has some answers related to unit testing (e.g. mark the managed object subclass with `@objc(EntityName)`. – Martin R Aug 01 '15 at 07:21
  • After i add @obj(Account) to class Account: NSMangagedObject, i get a CoreData warning: Unable to load class named 'Coredataexampleapp .Account' for entity 'Account'. – Peter Shaw Aug 01 '15 at 11:31

2 Answers2

0

Select all your project files in turn and look at the File Inspector at right. Make sure all files have the 'Test' project checked in their Target Memberships.

BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • All files belongs to the main target and the test target! Even all Entities belongs to the Test target, too – Peter Shaw Aug 01 '15 at 00:03
  • Hm! I'd be curious if any differences could be gleaned from lldb at a breakpoint in both cases. Maybe include the `po` on `getobject` in your question. – BaseZen Aug 01 '15 at 00:10
0

There is no need to include all the normal class files in the test target. You just have to import the main module, and everything works as expected.

@testable import AppName

This is inserted automatically in Xcode 7 beta 4. For the casting issue see this answer.

Community
  • 1
  • 1
Mundi
  • 79,884
  • 17
  • 117
  • 140