我在头文件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

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

推荐答案

可以在头文件中声明,在代码文件中定义.只需将其声明为

extern const double EARTH_RADIUS;

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

const double EARTH_RADIUS = 6353;

Objective-c相关问答推荐

在 Objective C 中将属性标记为已弃用

'if not' 的 Objective-C 预处理器指令

UITableViewCell 突出显示时使标签的背景清晰

NSMutableDictionary 线程安全

块引用作为Objective-C中的实例变量

如何在另一个视图中获取视图的框架?

iOS 7 - viewDidLoad 和 viewDidAppear 之间的区别

CF 对象与 NS 对象

如何在 XCode 中复制文件?

从 UIScrollView 中删除所有子视图?

Cocoa 与 Cocoa Touch - 有什么区别?

如何在 Objective C 中将浮点数转换为 int?

如何从输入 IB 的 UILabel 文本中插入新行 \n?

Objective-c 中 SEL 类型的属性

如何在 iOS 中计算 SHA-2(最好是 SHA 256 或 SHA 512)哈希?

`[super viewDidLoad]` 约定

如何在键盘上添加完成按钮?

将数据传回前一个视图控制器

如何判断 WkWebView 是否在 Objective-C 中完成加载?

UiTextField 中的第一个字母小写