Some notes on swift .framework
- Xcode -> New project -> Cocoa framework
- Add .swift files to your framework
- cmd + b (aka build)
If you want to build your framework along side your normal XCode project:
- Save as target -> pick Cocoa framework
- Select .swift files and set their target membership to the .framework
- cmd + b (aka build)
- Right click on the ‘.framework’ and select ‘Show in Finder’
- copy the .framework file in the same folder where your .swift files are. Then drag it into Xcode.
- In your new project: Project settings -> General -> Embedded binaries add the .framework via the + button🔑
Adding target membership to files in sub folders
If you have your source code in subfolder, as you should, then simply dragging the root folder into xcode will make the files inside the folders not have a target. There is trick to retarget all the files. 🔑
- Go to Project Settings > Build Phases > Compile Sources.
- You can click the + button, filter .swift files, and you can CMD-A to select all
- Click ok. This is the same thing as setting the target in the left side Utility pan.
Swift has three levels of access control. Use the following rules of thumb when creating your own frameworks:
- Public: for code called by the app or other frameworks, e.g., a custom view.
- Internal: for code used between functions and classes within the framework, e.g., custom layers in that view.
- Fileprivate: for code used within a single file, e.g., a helper function that computes layout heights.
- Private: for code used within an enclosing declaration, such as a single class block. Private code will not be visible to other blocks, such as extensions of that class, even in the same file, e.g., private variables, setters, or helper sub-functions.
- @testable if you prefix your import with
@testablethen you can work with framework classes even if they are not marked public or open 🔑
- Make a framework of your swift files in a xcode project. And also add playground that can import the framework: here
- Use the a .framework file in other projects by copying it: here
- Export and Drag and drop .framework to different projects: here
- Nice video here
- Apple claims that it’s best to use less than 6 .frameworks in your app. Or else compile time may suffer (I doubt this statement is still valid in new versions of XCode)
- Dynamic frameworks supported after ios 8 on iOS. Dynamic frameworks are linked form your app, static frameworks are included in the app.