I've written a custom struct in a separate header file. It looks something like this
typedef struct RequestSpecifics {
BOOL includeMetaData;
BOOL includeVerboseData;
} RequestSpecifics;
Now I want to make a custom 'make' method, similar to the CoreLocation struct CLLocationCoordinate2 CLLocationCoordinate2DMake method.
I've tried two different ways. While both ways give no errors in the .h file, I do get errors when I want to use the make method.
Method 1:
extern RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData);
Throws:
Apple Mach-O Linker
"_RequestSpecificsMake", referenced from:
Error Linker command failed with exit code 1 (use -v to see invocation)
Method 2:
extern RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData) {
RequestSpecifics specifics;
specifics.includeMetaData = includeMetaData;
specifics.includeVerboseData = includeVerboseData;
return specifics;
}
Throws:
Apple Mach-O Linker
Error Linker command failed with exit code 1 (use -v to see invocation)
Usage example:
RequestSpecificsMake(NO, NO)
I've checked all common solutions for the Apple Macho-Linker error but nothing seems to work or the solutions are not relevant.
So how do I correctly implement the 'make' method for a struct?
So apparently method 2 should be the implementation and it should not be in the .h file. Naturally, I need a .m file as well. This should be the correct way to do it:
.h file
RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData);
.m file
RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData) {
RequestSpecifics specifics;
specifics.includeMetaData = includeMetaData;
specifics.includeVerboseData = includeVerboseData;
return specifics;
}
In the end I had to combine both methods! Also, by the looks of it, the extern keyword is not required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With