【发布时间】:2014-03-20 17:02:58
【问题描述】:
您好,我有这个头文件:
#import <Foundation/Foundation.h>
@interface PCConstants : NSObject
extern NSString *const kPCUserProfileKey;
extern NSString *const kPCUserProfileNameKey;
extern NSString *const kPCUserProfileFirstNameKey;
extern NSString *const kPCUserProfileLocationKey;
extern NSString *const kPCUserProfileGenderKey;
extern NSString *const kPCUserProfileBirthDayKey;
extern NSString *const kPCUserProfileInterestedInKey;
@end
实现:
#import "PCConstants.h"
@implementation PCConstants
NSString *const kPCUserProfileKey = @"profile";
NSString *const kPCUserProfileNameKey = @"name";
NSString *const kPCUserProfileFirstNameKey = @"firstname";
NSString *const kPCUserProfileLocationKey = @"location";
NSString *const kPCUserProfileGenderKey = @"gender";
NSString *const kPCUserProfileBirthDayKey = @"birthday";
NSString *const kPCUserProfileInterestedInKey = @"interestedIn";
@end
当我在 .pch 文件中导入头文件时,我可以在任何地方访问常量。但我试图了解发生了什么。
我从不分配初始化这个对象,所以它们不能是实例常量。所以常量必须是“类对象常量对吧?但我认为类对象不能包含数据。
谁能解释一下?
【问题讨论】:
-
AFAIK Objective C 不支持实例级常量。因此,您的常量可能被视为具有文件范围。这与您看到的行为一致。
标签: ios iphone objective-c nsstring extern