Foundation Export [duplicate] - ios

This question already has an answer here:
"FOUNDATION_EXPORT" vs "extern"
(1 answer)
Closed 9 years ago.
What is the use of FOUNDATION EXPORT in Objective-c?
So I have:
KError.h
FOUNDATION_EXPORT NSString *const KAPPErrorDomain;
enum {
KPUnexpectedError = -1,
};
KError.m:
#import "KError.h"
NSString *const KAPPErrorDomain = #"com.kexample.myapp";
I assume that when you use Foundation_export in this case, it is to be able to use a variable in another file?
So that in KService.m, I cam reference KAppErrorDomain without any problem?

Yes. FOUNDATION_EXPORT is a macro that expands to extern
(or extern "C" in a C++ file), and that is the
keyword to declare a variable that is shared across source files (better: shared
across "translation units").
See How do I use extern to share variables between source files?
for many good answers why extern is necessary and how it works.

Related

iOS: How to get the #define from a Constant.h ObjC in a Swift File? [duplicate]

This question already has answers here:
use #define in objective C and access in swift class
(2 answers)
Closed 4 years ago.
When I write this in the constant.h file:
#define WS_PLANNING_INFORMATION "registrations/"
It works when I call it from the Swift file.
But if I write this in the constant.h file:
#define WS_PLANNING_INFORMATION "registrations/" CRYPTOKEY
It doesn't work. The swift file doesn't see the WS_PLANNING_INFORMATION anymore.
Is there an easy way to concatenate two strings in the constant.h file to be well-retrieved by the swift file?
(The validate answer to this question is very useful and more simple than in that ticket, that doesn't suit me)
Use constants instead of macros.
in constant.h
extern const char *WS_PLANNING_INFORMATION;
in constant.m
const char *WS_PLANNING_INFORMATION = "registrations/" CRYPTOKEY;
If you mean to use NSString * instead of char *
in constant.h
extern const NSString *WS_PLANNING_INFORMATION;
in constant.m
const NSString *WS_PLANNING_INFORMATION = #"registrations/" CRYPTOKEY;

Linker error when referencing static NSString const in Swift [duplicate]

I created Objective C Header file. and added some properties in it.
i declared
static NSString* const kColor005C98 = #"005C98"; in Constants.h file
I defined this file in Bridging-Header file as #import "Constants.h"
Now when i want to use this property kColor005C98 in some swift file it failed the build and i am getting
Undefined symbols for architecture armv7: "_kColor005C98", referenced from:
i don't know what else i need to do so i don't get this error? (i have used this property in other objective C file successfully and no issue in that case)
Update:
As of Swift 2/Xcode 7 and later, a static constant definition like
static NSString* const kColor005C98 = #"005C98"; // in Constants.h file
is imported to Swift and can be used without problems.
(Old answer for Swift 1.x) When the code
static NSString* const kColor005C98 = #"005C98"; // in Constants.h file
is processed by an Objective-C compiler, it is treated as two things
combined into one statement:
A variable declaration which introduces an identifier and describes its type, and
a variable definition which actually instantiates/implements this identifier.
See for example
What is the difference between a definition and a declaration?
for a good explanation of the difference between declaration and
definition.
The Swift compiler treats the statement only as a declaration.
Therefore the variable is not defined anywhere, causing the linker error.
To solve the problem, you have to move the definition to an Objective-C
file:
// Constants.m:
#import "Constants.h"
NSString * const kColor005C98 = #"005C98";
and change the declaration to an extern declaration:
// Constants.h:
extern NSString * const kColor005C98;
Alternatively, you can just remove the static modifier:
NSString * const kColor005C98 = #"005C98";
to make it work with Swift. The disadvantage is that when
this line is included by multiple Objective-C files, all of them
will define a globally visible symbol kColor005C98, causing
"duplicate symbol" linker errors.
Another alternative is to use a macro definition instead:
#define kColor005C98 #"005C98"

header file in iOS 8 embedded frameworks

I'm trying to create an embedded framework for use with iOS8. After creating one called SampleKit (BTW; is there any convention here, should I used a prefix?), it contains a header file that is puzzling me:
//! Project version number for SampleKit.
FOUNDATION_EXPORT double SampleKitVersionNumber;
//! Project version string for SampleKit.
FOUNDATION_EXPORT const unsigned char SampleKitVersionString[];
I know that FOUNDATION_EXPORT is a macro for extern or extern "C", but I'm not sure about the two constants. Where am I supposed to set the value for them?
Project > Build Settings > Versioning > Current Project Version :

Declaring NSString constants [duplicate]

This question already has answers here:
static const Vs extern const
(7 answers)
Closed 8 years ago.
Right now I am using the following way declare constants in header file:
static NSString *const RSMaxNumberOfIndustiresKey = #"MaxNumberOfIndustries";
Is it correct? I read Constants in Objective-C question but I really don't know if I really need to declare constant in 2 different places using FOUNDATION_EXPORT.
No. You should use the following in Constants.h:
extern NSString *const RSMaxNumberOfIndustiresKey;
and this in Constants.m:
NSString *const RSMaxNumberOfIndustiresKey = #"MaxNumberOfIndustries";
(i.e. add an implementation file simply to hold the single instance of the string constants).
Using your current method means there is a copy of each string within every file that includes that header.

Global variables in my Constants file

Until now if I needed access to a global variable across my app i just added
#define PATH [NSString stringWithFormat:#"www.url.com"]
To my Constants.h file.
I need to fetch the PATH value from my server.
How can i assign the value I'm getting from a server to a macro \string like the above and still be able to use just the variable PATH anywhere in my app? (Without naming the class like a property use such as class.PATH
This works:
#import <Foundation/Foundation.h>
NSString* PATH;
#interface Constants : NSObject
+(void)getPathFromServer;
#end
And PATH is accessible from anywhere in my app but I'm not sure if that should be the way to go.
As I understand so far, you need to define a macro which dynamic change it's url content.
If I'm right , you may need a Vararg Macros which takes a variable.
#define PATH(...) [NSString stringWithFormat:#"%#",__VA_ARGS__]
You can use extern keyword
Example :
//Header file
extern NSString * const path;
// .m file under implementation
NSString * const Ppath = [NSString stringWithFormat:#"www.url.com"];
Have a look at these
Constants in Objective C
#define vs const in Objective-C

Resources