Reputation: 754
Create a static library in objective c and c and trying to import in swift project. followed this link
Getting this error while building application
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_XYZ", referenced from: objc-class-ref in ViewController.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Xcode10.2
Swift 4.2
Upvotes: 0
Views: 1078
Reputation: 257709
Ok, let's do it from scratch step-by-step just to recall that it is possible (used Xcode 11.2, sorry - no 10.2, but I did this even before, so sure nothing changed since then)
1) Create new project form template
iOS > Single View App > Sample (Swift/Storyboard) > Next > Create > Build > Ok
2) Create static lib from template
Project Navigator > Sample > Add Target > iOS > Static Library > Calculator (Objective-C) > Finish
3) Add something meaningful to Calculator
Update Calculator.h & Calculator.m with following ...
@interface Calculator : NSObject
+ (NSString *)calculatedValue;
@end
@implementation Calculator
+ (NSString *)calculatedValue {
return @"1 + 1 = 5";
}
@end
Switch active schema to Calculator > Build > Ok
4) (!!! Very important step) Prepare Swift-ObjC bridge in main target
I prefer that Xcode do this, so... switch back active schema to Sample app
File > New > Cocoa Touch Class > Stub (Objective-C)
Next > (Make sure target is Sample checked) > Create
Create Bridging Header (This is it! - actually internally it does more) > Remove Stub.h/m (as they don't need for us)
5) Open Sample-Briding-Header.h and into it #import "Calculator.h"
6) Add library to main app target
Project Navigator > Sample > Target Sample > General > Section "Frameworks, Libraries... > + > select libCalculator.a
Actually, that's it - now it possible to use Objective-C classes from static library in main Swift-based application
7) Simple demo manipulations... create UILabel
in View Controller Scene
of Main.storyboard
and create outlet to it in ViewController.swift
, set string generated in library to UILabel.text
property
class ViewController: UIViewController {
@IBOutlet weak var results: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
results.text = Calculator.calculatedValue()
}
}
8) Build & Run > OK (observe result of integration)
Upvotes: 0