Skip to content

Commit 41f53d6

Browse files
committed
Added README.
README is untested and unproofed but I believe I picked up all the salient points to make things work.
1 parent 83b0c33 commit 41f53d6

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

ObjCApp/ViewController.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ @implementation ViewController
1717

1818
- (void)viewDidLoad {
1919
[super viewDidLoad];
20-
SimpleClass *sc = [[SimpleClass alloc] init:@"Dammit Objective C!!!"];
20+
SimpleClass *sc = [[SimpleClass alloc] init:@"Hello Swift framework from Objective C!!!"];
2121
[sc printMessage];
22-
23-
// Do any additional setup after loading the view, typically from a nib.
2422
}
2523

2624
- (void)didReceiveMemoryWarning {

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+

SimpleFramework.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
D8AA993219E47B0C00CEAFA5 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
107107
D8AA993419E47B0C00CEAFA5 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
108108
D8AA993719E47B0C00CEAFA5 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
109+
D8AA995019E4801700CEAFA5 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
109110
/* End PBXFileReference section */
110111

111112
/* Begin PBXFrameworksBuildPhase section */
@@ -146,6 +147,7 @@
146147
D8AA98C419E4665900CEAFA5 = {
147148
isa = PBXGroup;
148149
children = (
150+
D8AA995019E4801700CEAFA5 /* README.md */,
149151
D8AA98D019E4665900CEAFA5 /* SimpleFramework */,
150152
D8AA98DA19E4665900CEAFA5 /* SimpleFrameworkTests */,
151153
D8AA98FB19E4769200CEAFA5 /* SwiftApp */,

SwiftApp/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ViewController: UIViewController {
1313

1414
override func viewDidLoad() {
1515
super.viewDidLoad()
16-
let sc = SimpleClass("Dammit Swift!!!")
16+
let sc = SimpleClass("Hello Swift framework from Swift!!!")
1717
sc.printMessage()
1818
}
1919

0 commit comments

Comments
 (0)