Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - Custom struct 'make' method similar to CLLocationCoordinate2DMake

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?

like image 937
SolveSoul Avatar asked Feb 28 '26 20:02

SolveSoul


1 Answers

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.

like image 164
SolveSoul Avatar answered Mar 03 '26 09:03

SolveSoul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!