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

I wrote my own structure in a separate header file. It looks something like this.

typedef struct RequestSpecifics {
    BOOL includeMetaData;
    BOOL includeVerboseData;
} RequestSpecifics;

      

Now I want to create my own "make" method similar to the CoreLocation method of struct CLLocationCoordinate2 CLLocationCoordinate2DMake

.

I have tried two different ways. Although both methods give no errors in the .h file, I 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 by:

Linker command failed with completion code 1 (use -v to view the call)

Method 2:

extern RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData) {
    RequestSpecifics specifics;
    specifics.includeMetaData = includeMetaData;
    specifics.includeVerboseData = includeVerboseData;
    return specifics;
}

      

Throws:

Apple Mach-O Linker

Linker command failed with completion code 1 (use -v to view the call)

Usage example:

RequestSpecificsMake(NO, NO)

      

I have checked all common solutions for Apple Macho-Linker error but nothing seems to work or the solutions are irrelevant.

So how do you properly implement the make method for a struct?

+3


source to share


2 answers


Why don't you try

static inline

instead extern



static inline RequestSpecifics RequestSpecificsMake(BOOL includeMetaData, BOOL includeVerboseData) {
    RequestSpecifics specifics;
    specifics.includeMetaData = includeMetaData;
    specifics.includeVerboseData = includeVerboseData;
    return specifics;
}

      

or if you want to use extern

then you need to write it to a .m file.

+1


source


So it is obvious that method 2 should be implementation and it should not be in the .h file. Naturally, I need a file .m

. 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;
    }

      

I ended up having to combine both methods! Also, in appearance, no keyword extern

is required.

+2


source







All Articles