I have define #define baseUrl [NSString stringWithFormat:@"%@api/v4", MAINURL]
in objective c class and can access anywhere in project. But now i have created swift file in exiting objective c project and want to access baseurl in swift but error received.
我在目标c类中定义了#define baseUrl [NSString stringWithFormat:@“%@ api / v4”,MAINURL],可以访问项目中的任何地方。但是现在我已经在退出的目标c项目中创建了swift文件,并希望在swift中访问baseurl但收到错误。
Use of unresolved identifier 'baseUrl'
how can resolve it?
怎么解决呢?
2 个解决方案
#1
6
Importing Objective-C macros into Swift doesn't always work. According to documentation:
将Objective-C宏导入Swift并不总是有效。根据文件:
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.
Swift自动导入使用#define指令声明的简单,类似常量的宏作为全局常量。当宏使用字符串表示字符串,浮点或整数值时,或者在文字或以前定义的宏之间使用+, - ,>和==等运算符时,将导入宏。
C macros that are more complex than simple constant definitions have no counterpart in Swift.
比简单常量定义更复杂的C宏在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 }
#2
0
Unfortunately, Objective-C #define
could not access by swift.
不幸的是,Objective-C #define无法通过swift访问。
Maybe you change your #defines
to actual constants, which is better than #defines
.
也许您将#defines更改为实际常量,这比#defines更好。
Or create a swift object hold some static variable.
或者创建一个swift对象保存一些静态变量。
For example:
class API: NSObject {
static let apiPrefix = "https://vvv"
static let apiPosts = apiPrefix + "posts"
}
Calling: API.apiPosts
#1
6
Importing Objective-C macros into Swift doesn't always work. According to documentation:
将Objective-C宏导入Swift并不总是有效。根据文件:
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.
Swift自动导入使用#define指令声明的简单,类似常量的宏作为全局常量。当宏使用字符串表示字符串,浮点或整数值时,或者在文字或以前定义的宏之间使用+, - ,>和==等运算符时,将导入宏。
C macros that are more complex than simple constant definitions have no counterpart in Swift.
比简单常量定义更复杂的C宏在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 }
#2
0
Unfortunately, Objective-C #define
could not access by swift.
不幸的是,Objective-C #define无法通过swift访问。
Maybe you change your #defines
to actual constants, which is better than #defines
.
也许您将#defines更改为实际常量,这比#defines更好。
Or create a swift object hold some static variable.
或者创建一个swift对象保存一些静态变量。
For example:
class API: NSObject {
static let apiPrefix = "https://vvv"
static let apiPosts = apiPrefix + "posts"
}
Calling: API.apiPosts