1. Apple Recommended method
Locate target and change its other Linker Flags to:-all_load or-force_load
-force_load, followed by a file location, allows you to load the required files more precisely. To put it simply, the dynamic nature of the objective-c makes it necessary to add a label for the linker (set other Linker Flags to-OBJC) to resolve the Category
The problem of adding methods to the class. But this tag-objc a problem in 64-bit and IOS and requires the use of-all_load or-force_load.
Cons: All symbols will be imported, resulting in a large library.
2. My method
Add a class where the category declaration is, and then implement a method at random, and then call the class in the category in target. But if there are a lot of category words to add a bit more trouble, so, I directly define a few macros to simplify the operation.
The macro is defined as follows:
////FixCategoryBug.h//Mainlib////Created by Shaozg on 15/7/31.//Copyright (c) 2015 SHAOZG. All rights reserved.//#ifndef Mainlib_fixcategorybug_h#define mainlib_fixcategorybug_h#Define __kw_to_string_1 (x) #x#Define __kw_to_string (x) __kw_to_string_1 (x)//need to be called in a CATEGORY header file, such as Kw_fix_category_bug_h (nsstring_extented)#define kw_fix_category_bug_h (name) \@InterfaceKw_fix_category_bug_# #name: nsobject \+(void) Print; @end//need to be called in a CATEGORY source file, such as Kw_fix_category_bug_m (nsstring_extented)#define kw_fix_category_bug_m (name) \@implementation Kw_fix_category_bug_# #name \+ (void) Print {NSLog (@ "[Enable category%s]", __kw_to_string (name)); } @end//enable the macro in target to invoke the print method of the class defined in the category below. #define kw_enable_category (name) [kw_fix_category_bug_# #name Print]#endif
Nsstring+extented.h as follows:
//// NSString+Extented.h// MainLib//// Created by shaozg on 15/7/31.// Copyright (c) 2015年 shaozg. All rights reserved.//#import <Foundation/Foundation.h>#import "FixCategoryBug.h"KW_FIX_CATEGORY_BUG_H(NSString_Extented)@interface NSString (Extented)- (NSString *)testCategory;@end
NSSTRING+EXTENTED.M in the following:
//// NSString+Extented.m// MainLib//// Created by shaozg on 15/7/31.// Copyright (c) 2015年 shaozg. All rights reserved.//#import "NSString+Extented.h"KW_FIX_CATEGORY_BUG_M(NSString_Extented)@implementation NSString (Extented)- (NSString *)testCategory { returnself;}@end
The other target parts of the category need to be called as follows:
#import "AppDelegate.h" #import "MainLib.h" #import "Nsstring+extented.h" #import "SubLib.h" @interface appdelegate ()@end @implementation appdelegate - (BOOL) Application: (uiapplication*) Application Didfinishlaunchingwithoptions: (nsdictionary*) Launchoptions {//Override point for customization after application launch.//Enable_category (nsstring);Kw_enable_category (nsstring_extented);NSLog(@"Won't it go wrong?" %@", [@"Of course! "TestCategory]);return YES;}
Pros: Simple to use, no need to change the compilation option at all, and does not increase the volume of the library.
Cons: No drawbacks
If the small partners have an easier way, please share them!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Objective-c Static Library contains category what to do?