Is swift let equivalent to objective-c const?

Viewed 5048

Is this in Swift:

let someString:String = "blah"

Effectively equivalent to this in Objective-C:

NSString * const someString = @"blah";

I've been assuming that the use of the const keyword in this way in Objective-C has been making it effectively equivalent to let in Swift, under the hood, but it would be nice to hear that confirmed. It's rare that anyone uses const in this way when defining local variables in Objective-C, but it seems equivalent to let. Given the benefits of let, ensuring no mutation of the pointer can occur later, I'm wondering if my existing Objective-C projects wouldn't benefit by using this convention all the time. My question is not about the difference between Swift String and NSString.

2 Answers

You can define let and var in Objective-C. Simply save the header (swifty.h) and use it (#import "swifty.h" where you need or add to Prefix Header file pch) :

#ifndef let
#define let __auto_type const 
#endif

#ifndef var
#define var __auto_type 
#endif
Related