Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use #define in objective C and access in swift class

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.

Use of unresolved identifier 'baseUrl'

how can resolve it?

like image 304
Shahbaz Akram Avatar asked Jul 04 '18 10:07

Shahbaz Akram


People also ask

Whats a better word for use?

The words employ and utilize are common synonyms of use. While all three words mean "to put into service especially to attain an end," use implies availing oneself of something as a means or instrument to an end.

Is use or used correct?

Used to refers to something familiar or routine, as in "I'm used to getting up early for work," or to say that something repeatedly happened in the past like "we used to go out more." Use to typically occurs with did; "did you use to work there?" or "it didn't use to be like that," describing something in the past that ...

What type of word is use?

As detailed above, 'use' can be a noun or a verb. Noun usage: The use of torture has been condemned by the United Nations. Noun usage: There is no use for your invention.

What is the meaning of use and usage?

Use refers to the act of using or state of being used for a purpose. Usage refers to an accepted and habitual practice or the customary manner in which a language or a form of language is spoken or written.


1 Answers

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 }
like image 161
mag_zbc Avatar answered Oct 16 '22 17:10

mag_zbc