Namespace issues with downcasting NSManagedObjects in Swift modules

When I try to flush the selected NSManagedObject into the appropriate subclass StoryPhotoTrackin my (Swift) unit tests, I get a runtime error:Could not cast value of type 'StoryPhotoTrack_StoryPhotoTrack_' (0x7fbda4734490) to 'StoryPhotoTrack' (0x11edd1b08).

Here is my failure / failure:

func testFetchingStoryWithCastingInvolved() {
    let story : StoryPhotoTrack? = storyFetcher.fetchAnyStoryPhotoTrack()
    XCTAssertNotNil(story, "should be a story") // works
    let definitlyAStory : StoryPhotoTrack = story! // works
    let object : NSManagedObject = story as NSManagedObject! // works
    println(NSStringFromClass(definitlyAStory.dynamicType)) // prints 'StoryPhotoTrack_StoryPhotoTrack_'
    let downCastedStory : StoryPhotoTrack = object as! StoryPhotoTrack // -> error: "Could not cast..."
    XCTAssertNotNil(downCastedStory, "should still be a story")
}

// StoryFetcher.swift
    func fetchAnyStoryPhotoTrackVia_ObjectiveC_Fetcher() -> StoryPhotoTrack? {
        let object = StoryPhotoTrack.fetchAnyStoryPhotoTrackInContext(moc)
        return object as StoryPhotoTrack?
    }

Actual fetching is done in Objective-C:

// StoryPhotoTrack+Fetching.m
+ (StoryPhotoTrack *)fetchAnyStoryPhotoTrackInContext:(NSManagedObjectContext *)moc
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"StoryPhotoTrack"];
    NSArray *storyTracks = [moc executeFetchRequest:request error:nil];
    return (StoryPhotoTrack *)[storyTracks lastObject];
}

I guess this has something to do with Swift space spaces. It seems my StoryPhotoTrack class is actually called StoryPhotoTrack_StoryPhotoTrack_.

I tried to rename the class names of my main data entities using the projectName prefixes (as suggested here ), but that didn't help. I tried both my main target module name and my test target module name, none of them worked. setting class prefix in core data modelmy testbundle module name

What am I doing wrong?

edit: , , Swift, , , , . ?

+4
1

, - NSManagedObjects unit test, NSManagedObject Array , , to ProductModel, ProductModel, .

ProductModel MyAppName.ProductModel.

     func testProductModel() {
            let resultFetchRequest = NSFetchRequest(entityName: ProductModel.Name)
            let productModels: [NSManagedObject] = context.executeFetchRequest(resultFetchRequest, error: &error) as!
                    [NSManagedObject]

            if let recipes = productModels[0].valueForKey("recipes") as? NSSet {
                let recipesCount = recipes.count;
                XCTAssertTrue(recipesCount == 0)
            } else {
                XCTFail("FAIL - no recipes nsset")
            }
        }
0

All Articles