Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Realm (installed with Carthage) with a framework in a Swift app?

I'm working on an iOS App and a Watchkit App.

I read a few things regarding best practices and I decided to create a custom framework, as NathashaTheRobot advise here: https://realm.io/news/architecting-app-apple-watch-natashatherobot/

So I'm trying to use Realm in my framework.

I followed the installation instructions for Carthage:

  1. Add github "realm/realm-cocoa" to your Cartfile.

  2. Run carthage update.

  3. Drag RealmSwift.framework and Realm.framework from the Carthage/Build/iOS/ directory to the “Linked Frameworks and Libraries” section of your Xcode project’s “General” settings.

  4. On your application targets’ “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase”. Create a Run Script with the following contents:

    /usr/local/bin/carthage copy-frameworks

and add the paths to the frameworks you want to use under “Input Files”, e.g.:

$(SRCROOT)/Carthage/Build/iOS/Realm.framework 
$(SRCROOT)/Carthage/Build/iOS/RealmSwift.framework

Then I added my framework to the Target Membership of both Realm.framework and RealmSwift.framework.

But when I try to build the project, I get this error:

ld: framework not found Realm for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Then I gave a shot to lipo:

$ lipo -info Carthage/Build/iOS/Realm.framework/Realm                                                                                                       
Architectures in the fat file: Carthage/Build/iOS/Realm.framework/Realm are: i386 x86_64 armv7 arm64

Do you have any ideas of what I might be doing wrong here? Thank you.


EDIT:

OK so I found the problem and it was totally unrelated to Realm...

It looks like I deleted the Headers and Resources sections of my framework Build Phases somehow (which were and are still empty). I just put them back and everything compiles/works like it should.

Headers and Resources sections

Don't be tempted to delete those two

like image 390
Pym Avatar asked Jul 06 '15 15:07

Pym


People also ask

How do you install Carthage in a project?

Double-click Carthage. pkg to run the installer. Click Continue, select a location to install to, click Continue again and finally click Install. Note: When you attempt to run the installer, you may see a message stating: “Carthage.


1 Answers

Is it possible that it's your test target that can't find the frameworks? You'll have to add the parent location of the frameworks to the "Frameworks Search Path" section of your unit tests (likely $(SRCROOT)/Carthage/Build/iOS).

Here's a sample project of a Swift framework bundling RealmSwift as a dependency which you might find useful to compare your build settings against: https://static.realm.io/debug/ParentFramework.tgz

like image 178
jpsim Avatar answered Sep 26 '22 03:09

jpsim