IOS: other linker flags – objc

Time:2022-5-25

Application engineering use classification

Create a nsstring classification nsstring (test) in the application project, and create a static method + (void) testmethod in the classification;

NSString+test.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSString (test)

+ (void)testMethod;

@end

NS_ASSUME_NONNULL_END

NSString+test.m

#import "NSString+test.h"

@implementation NSString (test)

+ (void)testMethod{
    NSLog(@"testMethod");
}

@end

ViewController.m

#import "ViewController.h"

#import "NSString+test.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [NSString testMethod];
}


@end
IOS: other linker flags - objc

– objc not set
IOS: other linker flags - objc

normal operation

The demo project can be used normally without adding – objc.

Use the nm command to view the binary file of demo and find that the classification method is linked to:

IOS: other linker flags - objc

Classification normal link

Static library usage classification

Create a category nsbundle (nicesdkbundle) of nsbundle in the static library for reading resource files in the static library.

NSBundle+NiceSDKBundle.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSBundle (NiceSDKBundle)

+ (UIImage*)getNiceBundleImageWithName:(NSString*)name;

@end

NS_ASSUME_NONNULL_END

NSBundle+NiceSDKBundle.m

#import "NSBundle+NiceSDKBundle.h"

@implementation NSBundle (NiceSDKBundle)

+ (NSString*)sdkBundlePath{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"NiceBundle"
                                                           ofType:@"bundle"];
    return path;
}

+ (UIImage*)getNiceBundleImageWithName:(NSString*)name{
    NSString *path = [[self sdkBundlePath] stringByAppendingPathComponent:name];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    return image;
}

@end

At this time, – objc is not set for the static library project

IOS: other linker flags - objc

Static library project not set – objc

Package and compile this static library. Use the nm command to view that the classification symbol exists in the binary file

IOS: other linker flags - objc

Classification symbol exists

Application engineering uses static libraries that contain classifications

Static library project:

NiceSDKAPI.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NiceSDKAPI : NSObject

+ (instancetype)shareInstance;

- (void)test;
- (void)getImage;

@end

NS_ASSUME_NONNULL_END

NiceSDKAPI.m

#import "NiceSDKAPI.h"
#import "NSBundle+NiceSDKBundle.h"

@implementation NiceSDKAPI

+ (instancetype)shareInstance{
    static NiceSDKAPI *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[NiceSDKAPI alloc]init];
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)test{
    NSLog(@"NiceSDK test interface");
}

- (void)getImage{
    //Get bundle image
    UIImage *image = [NSBundle getNiceBundleImageWithName:@"joy_btn_apple_login.png"];
    NSLog(@"%@",image);
}

@end

Application Engineering:

ViewController.m

#import "ViewController.h"

//#import "NSString+test.h"
//Import nicesdk
#import <NiceSDK/NiceSDK.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [NSString testMethod];
    
    //Call SDK getimage interface
    [[NiceSDKAPI shareInstance]getImage];
    
}


@end
IOS: other linker flags - objc

unrecognized selector sent to class

Use the nm command to view:

IOS: other linker flags - objc

Only the classification added by the application project itself

At this time, add – objc to the other linker flags of the application project to run normally:

IOS: other linker flags - objc

Application Engineering plus – objc normal operation
IOS: other linker flags - objc

The categories used by the static library are also linked!

summary

1. Application engineering, using the classification added by its own engineering, without adding – objc;
2. Static library project, using the classification added by its own project, there is no need to add – objc;
3. Application engineering uses the classification added by the static library. Application engineering needs to add – objc.

reference resources

Official description

If you understand, please praise it, friends!

Recommended Today

Common third-party components in IOS

1. Install using cocoapods Afnetworking network request component Fmdb local database component Sdwebimage multiple thumbnail caching components Uickeychainstore component for storing user account and password Reachability monitors network status Datetools friendly time Mbprogresshud is a third-party library with prompt box Mwphotobrowser is a simple IOS photo browsing control Ctassetspickercontroller is a selector component that supports […]