What is the process for creating a Framework?

There are literally 1000s of posts on stackexchange and other places with people having problems creating frameworks.


I have no idea where the documentation is for creating a framework. In my case I am trying to create a class full of math functions.


1. I made a new project of "OSX -> Cocoas Framework"

2. I added a file with of type swift

3. I put in the class


public class TF {

public func f1() {

println("works")

}

}

4. I make a new project (in my case an OSX command line tool)

5. I drug the "Products->TFWork.framework" from the framework project into my new project

6. I hit run... and I then I get this:

dyld: Library not loaded: @rpath/TFWork.framework/Versions/A/TFWork

Referenced from: /Users/arh/Library/Developer/Xcode/DerivedData/TFTest-ezwiddsiqvhxwbavwdalvosatvrk/Build/Products/Debug/TFTest

Reason: image not found

(lldb)

I see stuff like this on stack exchange... but none of these posted solutions seem to work.

Ideas?

Thanks,

Alan


The magic here is workspaces (trust me, in Xcode the magic is always workspaces :-).

To test this, I did the following in Xcode 6.4:

  • I created a new framework project, “QFramework”, from the OS X > Framework & Library > Cocoa Framework template, selecting Swift as the language.

  • I created a new file,

    TF.swift
    , and set its contents to:
    public class TF {
        public init() {
        }
        public func f1() {
            println("works")
        }
    }

    IMPORTANT I tweaked your code to add a public initialiser.

  • I created a new app project, “QApp”, from the OS X > Application > Cocoa Application template, also selecting Swift as the language.

    Note The Swift runtime makes command line tools tricky, so I recommend that you start out with the simple case of an app.

  • I closed both projects.

  • I created a new workspace.

  • I added both projects (the

    *.xcodeproj
    files) to the workspace.

    IMPORTANT When adding, make sure Copy items into destination group’s folder is not checked.

  • In the Link Binaries with Libraries section of the Build Phases tab of the app’s target, I clicked the plus button and then added Workspace > QFramework.framework.

  • In the app, in

    AppDelegate.swift
    , I added the following to the top.

    import QFramework

  • In the app, also in

    AppDelegate.swift
    , I added the following to
    -applicationDidFinishLaunching:
    .
    TF().f1()

    [this text is here to make Markdown and DevForums play nicely (-; ]

  • I switched to the QFramework scheme and built that.

  • I switch to the QApp scheme and ran that.

  • It ran, printing:

    works



  • The key here is using a workspace. If you put both the app and the framework target in the same project, they share a build products directory and everything works. If you want separate projects, you have to add them both to the same workspace so that they share a build products directory.


    Share and Enjoy

    Quinn "The Eskimo!"

    Apple Developer Relations, Developer Technical Support, Core OS/Hardware

    let myEmail = "eskimo" + "1" + "@apple.com"

    Great answer. While using Xcode 7.1.1, and using a almost identical app, the app runs correctly in the iOS simulator but when I attempt to run the app on a real device (iPhone 6+ with iOS 9.1) I get the "dyld: Library not loaded: ... blah blah blah .... Reason: image not found" error.

    To answer my own question... highlight the project -> General -> Embedded Binaries

    You've made may day,


    Is it to say that I may have many projects in the same workspace using the same framework?


    And when it is time to archive then, I can do it separatly for each project? As long as I include the framework with it?


    I think I know what I'm going to do tonight.

    Quinn (Eskimo),

    Thanks for this post. I am creating a framework (V2Core) inside my existing app. I moved some of my swift model files over to the framework. I'll need those when I implement my share extension. Everything is working fine with one exception. My problem is that the Flurry.logEvents("This event") stopped working for those files moved into the new framework. I would like to keep that functionality if possible.

    How do I fix this?

    Thanks

    Tom

    What is the process for creating a Framework?
     
     
    Q