【问题标题】:Add compiler flag dependency for Framework project into main project将 Framework 项目的编译器标志依赖项添加到主项目中
【发布时间】:2017-03-24 04:07:53
【问题描述】:

我正在做一个有一些设置的框架项目。基于这些设置,一些行为将会改变。但这将由框架用户决定。比如说我在如下框架中有一个方法:

+ (void)printCompilerFlag {

if (OC_LOG_ENABLED == 0)
    NSLog(@"OC_LOG_ENABLED disabled");

else
    NSLog(@"OC_LOG_ENABLED enabled");

}

当用户将此框架添加到他们的项目中时,他们应该能够通过添加标志 OC_LOG_ENABLED=0 或 1 来处理他们的项目。

任何建议或手头教程都非常值得赞赏。

谢谢。

n.b:对不起,代码 sn-p,宏条件在这里不起作用。

【问题讨论】:

    标签: ios objective-c xcode compiler-flags


    【解决方案1】:

    我可以根据我的 RD 给你一些建议:

    我。为 .Framework 项目创建一个 Singelton 类:

    // ProjectManager.m file:
          + (instancetype)sharedManager
        {
            static ProjectManager *manager = nil;
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                manager = [ProjectManager new];
    
            });
            return manager;
        }
    
    
        //Singleton function setter:
        - (void)setOs_log_enabled_status:(BOOL)os_log_enabled_status
        {
            _os_log_enabled_status = os_log_enabled_status;
            if (_os_log_enabled_status == false)
            {
                NSLog(@"OC_LOG_ENABLED disabled");
            }
            else
            {
                NSLog(@"OC_LOG_ENABLED enabled");
            }
    
        }
    
    //Or
    
        - (void)printCompilerFlag 
        {
           if (_os_log_enabled_status == 0)
           NSLog(@"OC_LOG_ENABLED disabled");
           else
           NSLog(@"OC_LOG_ENABLED enabled");
        }
    

    项目管理器头文件只有在您生成为框架后才可见。

    //ProjectManager.h file
    @interface ProjectManager : NSObject
    
    + (instancetype)sharedManager;
    
    //os_log bool value to handle your requirement
    @property (nonatomic,assign) BOOL os_log_enabled_status;
    //function
    - (void)printCompilerFlag;
    
    @end
    

    二。 确保您已选择此头文件作为公共文件:

    Build Phases->Headers 将 ProjectManager.h 从 Project 拖到 Public。

    三。演示用法:

      //Appdelegate.m file
        #import <YourFramework/ProjectManager.h>
    
        @implementation AppDelegate
    
        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        //Depends on user status false or true
         [ProjectManager sharedManager].os_log_enabled_status = false;
        //or
         [ProjectManager sharedManager].os_log_enabled_status = true;
    
        //After setting BOOL value call this function
         [ProjectManager sharedManager] printCompilerFlag];
        return true;
        }
    
        @end
    

    希望这会给您一些处理框架类的想法。

    【讨论】:

    • 感谢您的宝贵时间。其实我不想用任何参数来做到这一点。我想通过编译器标志来处理它,显然我会导入框架。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    相关资源
    最近更新 更多