3

I just started playing with frameworks in xcode trying to create my own module. When making an iOS app its relatively straightforward where the entry point is ( AppDelegate )

However the framework I've created has nothing like a "main.swift" or similar method.

Do i have to create/specify the entry point on my own? Thanks

EDIT: The reason i'd like the framework to be runnable is so that i can actually print some output while I'm making it to make sure everything works properly.

Return-1
  • 2,329
  • 3
  • 21
  • 56

3 Answers3

1

A framework doesn't have a traditional entry point like this - it won't ever be run by itself, so it doesn't need one.

To use your framework you would create an app which linked with your framework - the entry point for the app would then call methods from inside your framework.

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • Would it not be useful to somehow run the code of my framework as im developing it though? This sort of forces me to make my framework in a CLI tool enivorment and when im done, take all the files from there and then package them in a framework. How would you go about creating a framework normally if i may ask? Thank you. – Return-1 Jun 26 '17 at 13:57
  • George - wouldn't you simply, then have a call (perhaps "test" or similar) which, if you happen to want to, you can call in that setting? – Fattie Jun 26 '17 at 14:04
  • 1
    @GeorgeAvgoustis - for an example of how to code things, look at my answer. For an example of how to develop both together, look at my answer here: https://stackoverflow.com/questions/41207622/how-to-make-same-ios-swift-app-with-only-some-differences-in-the-code-and-assets/41209283#41209283 Basically you want your Xcode app project to also contain your framework project's `.xcodeproj` file. –  Jun 26 '17 at 14:10
  • @dfd ok thanks. This is what i needed, someone to verify that you need an app while developing a framework and there is no way to execute it on its own while you re making it. Cheers – Return-1 Jun 26 '17 at 14:13
  • Be careful, I prefer using the term *target* over *app* here, as there are other things - a second framework, a macOS app, extensions - that can also "consume" a framework. Just trying to be as accurate as possible! –  Jun 26 '17 at 14:15
  • @GeorgeAvgoustis You could write some unit tests for it ;) – deanWombourne Jun 26 '17 at 21:32
  • @deanWombourne you know what, thats the best idea so far. May i go ahead and post it as an answer crediting you? – Return-1 Jun 27 '17 at 06:44
  • @GeorgeAvgoustis sure – deanWombourne Jun 27 '17 at 12:47
1

(My TL;DR is at the bottom.)

As already stated, there is no entry point like you are thinking. Instead, you should do this:

In your Framework target (I'll assume the framework is named MyFramework):

Add files, classes, properties, subclassed controls, etc. and mark things as public, private, internal, and fileprivate. (See the access level section in the Apple documentation.)

For instance:

public class MyClass1 {
    public var property1 = ""
    private var property2 = ""
    public func myFunc() -> String {
        print("Hello World!")
    }
}
private class MyClass2 {
    var property1 = ""
    var property2 = ""
    func myFunc() -> String {
        return "Hello World!"
    }
}

In your app target (again, assuming your framework is named myFramework):

include MyFramework

class ViewController: UIViewController {
    func tryThis() {
        let myClass1 = myClass1()
        print(myClass1.myFunc())  // prints "Hello World!"
        // the line below will generate a build error 
        // as myClass2 is marks private
        let myClass2 = myClass2()
    }
}

TL;DR

Learn your Access Levels, add code into your Framework target, and import the framework into your app.

  • Thanks for the answer. Doesn't that mean however that in order to test a framework while Im making it, it needs to be included in some app as it cannot be run on its own? – Return-1 Jun 26 '17 at 14:07
  • I posted a comment in the earlier answer with this link: https://stackoverflow.com/questions/41207622/how-to-make-same-ios-swift-app-with-only-some-differences-in-the-code-and-assets/41209283#41209283. I use this setup, and develop both targets - app and framework - at the same time in on Xcode window. In fact, if you edit the app scheme to build both targets, you can always pull in the framework changes every tie you build the app. –  Jun 26 '17 at 14:13
1

An answer I got from @deanWombourne points out that if anyone wants to only use the framework project for development as is and develop the framework without integrating it in an app for execution, they can just use the tests provided by the framework for an entry point.

For someone that might be new, all you need to is include unit tests to your project, press on the play button which you normally press on for running and select the wrench icon that writes "test" next to it to run the tests.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Return-1
  • 2,329
  • 3
  • 21
  • 56