What's the Swift Equivalent of Objective-C's "#Ifdef _Iphone_11_0"

What's the Swift equivalent of Objective-C's #ifdef __IPHONE_11_0 ?

The iOS 11 SDK comes with Swift 3.2 (or Swift 4), so you can use a Swift version check to accomplish the same thing:

#if swift(>=3.2)
if #available(iOS 11.0, *) {

}
#endif

use #define in objective C and access in swift class

Importing Objective-C macros into Swift doesn't always work. According to documentation:

Swift automatically imports simple, constant-like macros, declared with the #define directive, as global constants. Macros are imported when they use literals for string, floating-point, or integer values, or use operators like +, -, >, and == between literals or previously defined macros.

C macros that are more complex than simple constant definitions have no counterpart in Swift.

An alternative in your case would be to use a function that returns the value defined by macro

// .h file
#define baseUrl [NSString stringWithFormat:@"%@api/v4", MAINURL]
+ (NSString*) BaseUrl;

// .m file
+ (NSString*) BaseUrl { return baseUrl }

How to use a Objective-C #define from Swift

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.

How to translate the following code snippet from Swift to Objective-C?

I have managed to solve this problem with a workaround discussed here.

#if __clang_major__ >= 12
NSLog(@"My Objective-C language support is what Apple Clang/Xcode 12.x can support.");
#endif

Note that clang_major or clang_minor etc. variables resemble (almost same to) Xcode versions and need careful investigation before the usage.

Is swift let equivalent to objective-c const?

Provided you place the const after the type, as in:

NSString * const someString = @"blah"; // your example

struct { int x; int y; } const someStruct = { 42, 24 }; // const value type

then the semantics of const in (Objective-)C and let in Swift are similar. For value types both prevent the modification of the value. For reference types both prevent the modification of the reference itself and allow modification of the referenced object.

If you place the const elsewhere in the declaration then you enter the labyrinth of C pointers to constants, constant pointers, constant pointers to constants, and longer chains of (non-)constant combinations. Not everyone who enters this labyrinth returns... or at least not with their sanity intact. You have been warned! ;-)



Related Topics



Leave a reply



Submit