|
| 1 | +# SimpleFramework |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The purpose of this project is to demonstrate (and remember) how to create a Swift framework for iOS that in turn uses Objective-C code. Then, this framework is used by two different iOS apps -- one Swift and one Objective-C. |
| 6 | + |
| 7 | +## Creating the Framework |
| 8 | + |
| 9 | +1. Create a new Xcode project. |
| 10 | +1. Create a new Cocoa Touch framework target using Swift. |
| 11 | +2. Create a new Objective C Cocoa Touch class named `SFClass` as a subclass of `NSObject`. |
| 12 | +3. Select `SFClass.h` and make sure its Target Membership is `SimpleFramework` _and_ "Public". Turns out this is _critical_. |
| 13 | +3. Add the following code to `SFClass.h`: |
| 14 | + |
| 15 | + ``` |
| 16 | + -(void) printMessage:(NSString *) message; |
| 17 | +
|
| 18 | + ``` |
| 19 | + |
| 20 | +1. Add the following code to `SFClass.m`: |
| 21 | + |
| 22 | + ``` |
| 23 | +-(void) printMessage:(NSString *) message { |
| 24 | + NSLog(@"The message is %@", message); |
| 25 | +} |
| 26 | +
|
| 27 | + ``` |
| 28 | + |
| 29 | +1. Create a new Swift class named `SimpleClass` as a subclass of `NSObject` and add the following code to `SimpleClass.swift`. Note the usage of `public` in front of the class, the initializer, and the `printMessage` method. This makes the class and methods available to those who consume the framework. |
| 30 | + |
| 31 | + ``` |
| 32 | +public class SimpleClass: NSObject { |
| 33 | + |
| 34 | + var message: String |
| 35 | + var object: SFClass = SFClass() |
| 36 | + |
| 37 | + public init(_ newMessage: String) { |
| 38 | + self.message = newMessage |
| 39 | + } |
| 40 | + |
| 41 | + public func printMessage() { |
| 42 | + object.printMessage(self.message) |
| 43 | + } |
| 44 | +} |
| 45 | + ``` |
| 46 | +1. When you created the framework target, Xcode automatically created `SimpleFramework.h` for you. Add the following to that file: |
| 47 | + |
| 48 | + ``` |
| 49 | +#import "SFClass.h" |
| 50 | + ``` |
| 51 | + |
| 52 | +## Consuming the Framework from a Swift app |
| 53 | + |
| 54 | +1. Create a new single-view iOS app target in the project and name it `SwiftApp` and use Swift. |
| 55 | +2. Select the new target and go to the `General` settings. Under `Embedded Binaries`, click the `+` and add `SimpleFramework.framework`. |
| 56 | +3. In the `ViewController.swift` file that was created, add the following line at the top: |
| 57 | + |
| 58 | + ``` |
| 59 | + import SimpleFramework |
| 60 | + ``` |
| 61 | + |
| 62 | + and the following method should replace the existing `viewDidLoad`: |
| 63 | + |
| 64 | + ``` |
| 65 | + override func viewDidLoad() { |
| 66 | + super.viewDidLoad() |
| 67 | + let sc = SimpleClass("Hello Swift framework from Swift!!!") |
| 68 | + sc.printMessage() |
| 69 | + } |
| 70 | +``` |
| 71 | + |
| 72 | +## Consuming the Framework from an Objective-C app |
| 73 | +1. Create a new single-view iOS app target in the project and name it `ObjCApp` and use Objective C. |
| 74 | +2. Select the new target and go to the `General` settings. Under `Embedded Binaries`, click the `+` and add `SimpleFramework.framework`. |
| 75 | +3. Under the `Build Settings` for the target, find the setting for `Embedded Content Contains Swift Code` and set it to `Yes`. |
| 76 | +4. In the ViewController.m file that was created, add the following line at the top: |
| 77 | + |
| 78 | + ``` |
| 79 | + @import SimpleFramework; |
| 80 | + ``` |
| 81 | + |
| 82 | + and the following method should replace the existing `viewDidLoad`: |
| 83 | + |
| 84 | + ``` |
| 85 | +- (void)viewDidLoad { |
| 86 | + [super viewDidLoad]; |
| 87 | + SimpleClass *sc = [[SimpleClass alloc] init:@"Hello Swift framework from Objective C!!!"]; |
| 88 | + [sc printMessage]; |
| 89 | +} |
| 90 | +``` |
| 91 | + |
0 commit comments