我尝试在一个类中定义一个 const NSString。
我做了以下事情:
在.h中:
#import <Foundation/Foundation.h>
@interface GlobalDataModel : NSObject
@property (nonatomic, strong) NSMutableDictionary *infoDictionary;
+ (id)sharedDataModel;
extern NSString * const WS_URL;
@end
以 .m 为单位:
#import "GlobalDataModel.h"
@implementation GlobalDataModel
static GlobalDataModel *sharedInstance = nil;
NSString * const WS_URL = @"http://localhost:57435/IosService.asmx";
- (id)init{
self = [super init];
if (self) {
self.infoDictionary = [NSMutableDictionary dictionary];
}
return self;
}
+ (id )sharedDataModel {
if (nil != sharedInstance) {
return sharedInstance;
}
static dispatch_once_t pred; // Lock
dispatch_once(&pred, ^{ // This code is called at most once per app
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
@end
用法:
#import "GlobalDataModel.h"
GlobalDataModel *model = [GlobalDataModel sharedDataModel];
现在,我可以做到
NSString *temp = model.infoDictionary[@"xxx"];
但不是
NSString *temp = model.WS_URL
因为智能感知中没有 WS_URL。