V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
pysama
V2EX  ›  iDev

能否让 define 的变量在工程的所有类中都生效?

  •  
  •   pysama · 2015-05-26 22:04:47 +08:00 · 1834 次点击
    这是一个创建于 3271 天前的主题,其中的信息可能已经有所发展或是发生改变。

    为了简化代码或者调试方便,我们会经常使用define预处理,比如:

    //简化debug语句
    #define SLOG(x) NSLog(@"My test: %@", x)
    
    //快捷定义RGB
    #define  [UIColor ....]
    

    请问大家,有没有什么办法,在一个“公共文件”中一次性define,然后在工程的所有类中都可以使用呢?

    自己想过的方案:写一个公共类,但是在需要用到的地方import。 虽然可行,但还是麻烦,因为还是要import一次。 所以在此请教下大神有没有方法能够实现我这种偷懒的需求?

    thanks~

    7 条回复    2015-05-27 14:02:13 +08:00
    dorentus
        1
    dorentus  
       2015-05-26 22:08:23 +08:00 via iPad   ❤️ 1
    在工程的 prefix header (文件名应该是叫 xxx.pch 吧)里面 import 或者 define 应该就行
    lionyue
        2
    lionyue  
       2015-05-26 23:50:32 +08:00
    @dorentus 正解!
    pysama
        3
    pysama  
    OP
       2015-05-27 10:02:59 +08:00
    @dorentus 非常感谢,晚上再试试
    realityone
        4
    realityone  
       2015-05-27 11:01:16 +08:00
    #define 的不是变量吧..
    angelface
        5
    angelface  
       2015-05-27 11:24:29 +08:00
    @pysama 新的项目模板应该没pch, 要自己手动添加。
    pysama
        6
    pysama  
    OP
       2015-05-27 13:01:18 +08:00
    @realityone 恩,不是变量,是个预处理。我没表达好
    zenghaojim33
        7
    zenghaojim33  
       2015-05-27 14:02:13 +08:00
    xcode6下手动添加prefix header即可
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1217 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 18:21 · PVG 02:21 · LAX 11:21 · JFK 14:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.