@interface WeatherService : NSObject
@property (nonatomic, readonly)
NSMutableDictionary *map;
@end
@implementation WeatherService {
NSMutableDictionary *_map;
}
//lazy 属性:getter 只在首次访问时创建值
- (NSMutableDictionary *)map {
if (_map == nil) {
//2. Creating WeatherMap
_map = [NSMutableDictionary
dictionary];
}
return _map;
}
@end
WeatherService *service =
[WeatherService new];
//1. 访问 map 之前
NSMutableDictionary *map = service.map;
//在此创建,仅一次