Using Swift variables in Objective-C : Swift 4 Edition
Today I ran into weird problem which prompted me to write this blog post on Objective-C and Swift interconnectedness.
Before Swift 4 came along, the sure way to access Swift variables in Objective-C was to inherit class from NSObject (Swift structs cannot be used in the Objective-C) and prefix it with @objc keyword.
To summarize, below are the steps to import Swift classes in the Objective-C class
- Create a Swift class with class name prefixed with
@objcannotation and inheriting fromNSObject - Xcode will autogenerate
YourProjectName-Swift.hfile with details of your Swift class intended to use in Objective-C - You can import this header file in your Objective-C class to start using Swift class and variable
- Here's where it gets different in Swift 4. Steps 1-3 will work fine with pre-Swift 4 versions. However, if you want to start using Swift class and instance variables in
Objective-CSwift 4 onwards, you will have to prefix them withobjckeyword. - Alternatively, if you have so many variables in Swift class and you want to make all of them available to
Objective-C, you can add@objcMembersat the top of class declaration. This is equivalent to prefixing all the variables with@objcprefix - Since rule has changed in
Swift 4, you NO LONGER need to prefix Swift class withobjcannotation
Let's see an example.
Assume below is the Swift class in project named MyProject you want to use in the Objective-C file. Let's begin by applying some of the
class Vehicle: NSObject {
@objc let name: String
@objc let numberOfWheels: Int
let manufacturer: String
}
Observations:
- Class
vehiclehas 3 variables viz.name,numberOfWheelsandmanufacturer - We derive
Vehicleclass fromNSObjectclass Vehicleis a class and not astruct- Instance variables
nameandnumberOfWheelsare prefixed withobjcannotations to make them available toObjective-Cclasses - Alternatively, if you want to make all the instance variables of
Vehicleaccessible toObjective-C, you can add@objcMembersbefore class declaration - Let's assume the name of Xcode auto generated header file used to import
SwiftintoObjective-CisMyProject-Swift.h
How to use:
- Compile your project. This will add necessary
Objective-Cusable and compatible classes in the auto-generated header fileMyProject-Swift.h - Import
MyProject-Swift.hfile in the Objective-C class. This is the sameObjective-Cclass from which you want to access Swift class and related instance variables - Once this is done, you're good to use Swift class and related variables which are made accessible
Vehicle *vehicle = [[Vehicle alloc] init];
vehicle.name = @"RAV 4";
vehicle.numberOfWheels = 4;
// Please note that following line won't compile
// unless you have used @objcMembers as explained above.
// In normal case without @objcMembers keyword,
// the Swift variable manufacturer is not visible to
// Objective-C classes
// Do not use this.
// vehicle.manufacturer = @"Toyota";
This is all you need to know to start using Swift classes in Objective-C beginning Swift 4. Feel free to message me on @jayeshkawli on Twitter is you run into any other problems
References

