【问题标题】:Where do you declare a constant in Objective-C?在 Objective-C 中,你在哪里声明一个常量?
【发布时间】:2011-09-05 12:43:55
【问题描述】:

我在头文件const double EARTH_RADIUS=6353; 中声明了一个常量,该文件被导入到其他各种头文件中,但出现链接器错误。

Ld /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator/BadgerNew.app/BadgerNew normal i386
    cd /Users/Teguh/Dropbox/badgers/BadgerNew
    setenv MACOSX_DEPLOYMENT_TARGET 10.6
    setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-g++-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -L/Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator -F/Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator -filelist /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Intermediates/BadgerNew.build/Debug-iphonesimulator/BadgerNew.build/Objects-normal/i386/BadgerNew.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -framework CoreLocation -framework UIKit -framework Foundation -framework CoreGraphics -framework CoreData -o /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator/BadgerNew.app/BadgerNew

ld: duplicate symbol _EARTH_RADIUS in /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Intermediates/BadgerNew.build/Debug-iphonesimulator/BadgerNew.build/Objects-normal/i386/NearbyIsiKota.o and /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Intermediates/BadgerNew.build/Debug-iphonesimulator/BadgerNew.build/Objects-normal/i386/FrontPageofBadger.o for architecture i386
collect2: ld returned 1 exit status

基本上,我希望该常量可用于我项目中的所有类。我应该在哪里申报?

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    您可以在标头中声明,在代码文件中定义它。只需将其声明为

    extern const double EARTH_RADIUS;
    

    然后在某个 .m 文件中(通常是您在其中声明的 .h 的 .m)

    const double EARTH_RADIUS = 6353;
    

    【讨论】:

    • 你真的需要在标题中做extern吗?没有它,它似乎可以工作。
    • 标题中的 extern 允许其他文件了解您的常量。如果您只需要单个文件范围的常量,则不需要extern
    • 在.m中,在@implementation之前还是之后有关系吗?
    • @kraftydevil 从技术上讲这并不重要,但惯例是在 #imports 之后和 @implementation 之前进行。但实际上在文件的全局范围内的任何地方都可以(意思是只要它不在任何花括号内{}
    【解决方案2】:

    有两种方法可以做到这一点:

    第一个选项 - 正如之前的回复所指出的,在 .h 文件中

    myfile.h
    extern const int MY_CONSTANT_VARIABLE;
    

    并在 myfile.m 中定义它们

    myfile.m    
    const int MY_CONSTANT_VARIABLE = 5;
    

    第二个选项 - 我最喜欢的

    myfile.h
    static const int MY_CONSTANT_VARIABLE = 5 ;
    

    【讨论】:

    • 为什么用 static const int MY_CONSTANT_VARIABLE = 5 声明有效?
    • 它有效,但前提是您需要一个非全局常量。静态常量在文件外不可见。否则使用第一个选项。
    • .h 或 .m 文件在哪里?在顶部的花括号内?其他地方?
    • 这似乎是错误的。当我执行static const int MY_CONSTANT_VARIABLE = 5 ; 时,我从 Xcode5 收到错误:“接口类型无法静态分配”
    • 嗨@learner,您是在头文件中的导入下方声明常量吗?
    【解决方案3】:

    源文件中声明它并与它有外部链接(使用extern关键字)以便在所有其他源文件中使用。

    【讨论】:

      【解决方案4】:

      最佳做法是在您的 .h 和 .m 文件中声明它。请参阅Constants in Objective-C 以获取有关同一问题的一组非常详细的答案。

      【讨论】:

        猜你喜欢
        • 2023-03-22
        • 1970-01-01
        • 2011-01-03
        • 1970-01-01
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        • 2016-07-05
        • 2010-12-28
        相关资源
        最近更新 更多