Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a Objective-C #define from Swift

I am migrating a UIViewController class to train a bit with Swift. I am successfully using Objective-C code via the bridging header but I have the need of importing a constants file that contains #define directives.

I have seen in Using Swift with Cocoa and Objective-C (Simple macros) the following:

Simple Macros

Where you typically used the #define directive to define a primitive constant in C and Objective-C, in Swift you use a global constant instead. For example, the constant definition #define FADE_ANIMATION_DURATION 0.35 can be better expressed in Swift with let FADE_ANIMATION_DURATION = 0.35. Because simple constant-like macros map directly to Swift global variables, the compiler automatically imports simple macros defined in C and Objective-C source files.

So, it seems it's possible. I have imported the file containing my constants into the bridging header, but I have no visibility from my .swift file, cannot be resolved.

What should I do to make my constants visible to Swift?

UPDATE:

It seems working with NSString constants, but not with booleans:

#define kSTRING_CONSTANT @"a_string_constant" // resolved from swift #define kBOOL_CONSTANT YES // unresolved from swift 
like image 393
atxe Avatar asked Jun 20 '14 10:06

atxe


People also ask

How do I write in Objective-C?

Objective-C uses the same phraseology as the C language. Like in C, each line of Objective-C code must end with a semicolon. Blocks of code are written within a set of curly brackets. All basic types are the same, such as int, float, double, long and more.

Can you still use Objective-C?

Objective-C is so pervasive in iOS that it's impossible to completely remove it. Apple continues to maintain libraries written in Objective-C, so we should expect Objective-C to be treated as a (mostly) first class language in iOS. At other companies, legacy code remains.

Is Objective-C difficult to learn?

Aside from its funny-looking syntax, Objective-C is an easier language for beginner developers to learn.


1 Answers

At the moment, some #defines are converted and some aren't. More specifically:

#define A 1 

...becomes:

var A: CInt { get } 

Or:

#define B @"b" 

...becomes:

var B: String { get } 

Unfortunately, YES and NO aren't recognized and converted on the fly by the Swift compiler.

I suggest you convert your #defines to actual constants, which is better than #defines anyway.

.h:

extern NSString* const kSTRING_CONSTANT; extern const BOOL kBOOL_CONSTANT; 

.m

NSString* const kSTRING_CONSTANT = @"a_string_constant"; const BOOL kBOOL_CONSTANT = YES; 

And then Swift will see:

var kSTRING_CONSTANT: NSString! var kBOOL_CONSTANT: ObjCBool 

Another option would be to change your BOOL defines to

#define kBOOL_CONSTANT 1 

Faster. But not as good as actual constants.

like image 51
fabrice truillot de chambrier Avatar answered Sep 23 '22 19:09

fabrice truillot de chambrier