I have had modules switched off in Xcode for Trail Wallet since its introduction. I have a Core Data entity called “Category” and, when I tried to enable the modules feature (introduced in Xcode 5), I was hit with Redefinition of 'Category' as a different kind of symbol
error.
Recently, however, things came to a head when I tried to import a mixed-source framework into Trail Wallet and it refused to see the Swift files.
The solution to getting Swift files in external frameworks to work in Objective-C projects, according to Apple’s Using Swift with Cocoa and Objective-C, is to import it as a module within Objective-C (e.g. @import ExternalFramework;
).
So I assumed I was finally going to have to tackle entity renaming, which is handled by Core Data’s lightweight migration but only if it is set up correctly.
I generally dislike database migrations and try to avoid doing them if I can. I’ve never had an issue with a production version and migration (probably because I’m so paranoid about it), but it’s always nerve-wracking when a new version with a migration goes out into the world.
Previous searches had yielded no good news but I thought I should search one last time, which revealed this answer at Stack Overflow—I could avoid a migration after all.
The SO answer is a little light on details, so here are the exact steps I took:
1. Select the .xcdatamodeld
file, then select the entity that’s causing the conflict (Category
, in my case)
2. Select the Data Model inspector in the Utilities pane.
3. Rename the Class property:
4. Re-generate the NSManagedObject subclasses by going to Editor -> Create NSManagedObject Subclass...
.
Xcode 7’s NSManagedObject subclasses work a little differently than they used to—it generates its own category that it is then free to overwrite should you change the entity in future versions, and leaves the core class for you to add additional methods and properties to.
5. If you have added additional functionality to the NSManagedObject subclasses, you’ll need to copy it across.
I had a category on my Category entity (of course I did) called Category+GetCategories.[h/m]
, so I had to copy out all of the API and implementation from these files and put it all in the newly generated CDCategory.[h/m]
files.
6. Delete the old classes.
7. Update all references to the old class and replace them with the new class, in my case I had to replace Category
and with CDCategory
throughout the project.
It all worked just fine, no database migration or entity renaming required.
The only other gotcha was that, after attempting to run with the new framework, it was crashing out with a dyld: Library not loaded: @rpath/libswiftCore.dylib
error.
This is caused by the build setting Embedded Content Contains Swift Code
being set to No
. Switching this to Yes
allowed the project to run and use the Swift code in the framework.