BroSimple BroSimple - 1 month ago 10
Objective-C Question

What is #define in swift

In Objective-c it's

#define getDataURL @"http://serverip.org/test.php"


but in Swift it's something else and I can't figure it out

It's probably simple but thank you for helping out.

Answer

#define is a preprocessor directive that Objective-C inherited from C (since it's a superset of C).

Swift doesn't have a preprocessor at all so you can't have the same exact behavior of #define textual replacement but you can do something different, eg:

class MyConstants {
  static let DATA_URL = "http://serverip.org/test.php";
}

That's semantically different since preprocessor directives doesn't even reach semantic phase of the compiler but you can use it in a quite similar way.