gpt4 book ai didi

ios - 哪一个是初始化 NSMutableDictionary 的正确方法。请任何人在这里纠正我

转载 作者:行者123 更新时间:2023-11-28 23:06:17 24 4
gpt4 key购买 nike

我正在尝试在我的类(class)中创建一个 NSMutableDictionary。我已经阅读了 stackoverflow 中的许多帖子以了解其中的区别。但现在我完全糊涂了。所以有人纠正我,哪一个是在我的类中初始化 NSMutableDictionary 的正确方法。我必须在我的应用程序的许多区域访问这个字典。所以建议我使用变量初始化的好方法......

/// .h file
@interface ActiveFeeds : NSObject {

}
@property (nonatomic, copy) NSMutableDictionary *invDictionary;
@property (nonatomic, retain) NSString *filePath;
@end

@implementation ActiveFeeds

@synthesize filePath;
@synthesize invDictionary;

- (id)init{

self = [super init];
if (self != nil){
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:self.filePath];
self.invDictionary = [dictionary mutableCopy];
dictionary release];
}
return self;
}

/* And use self.invDictionary all in the application */
- (void)setObjectAtKey:(NSMutableDictionary *)objectDic atKey:(NSString *)setKey{
[self.invDictionary setObject:objectDic forKey:setKey];
}

- (void)dealloc {
[self.invDictionary release];
[self.filePath release];
[super dealloc];
}

@end

或者像这样....

@interface ActiveFeeds : NSObject {
NSMutableDictionary *invDictionary;
NSString *filePath;
}
@end


@implementation ActiveFeeds

- (id)init{

self = [super init];
if (self != nil){
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
invDictionary = [dictionary mutableCopy];
[dictionary release];
}
}
return self;
}

/* And use invDictionary all in the application */
- (void)setObjectAtKey:(NSMutableDictionary *)objectDic atKey:(NSString *)setKey{
[invDictionary setObject:objectDic forKey:setKey];
}

- (void)dealloc {
[invDictionary release];
[filePath release];
[super dealloc];
}

@end

请任何人帮助我获得使用变量的正确方法....

最佳答案

- (id)initWithFilePath:(NSString *)path{

self = [super init];
if (self != nil){
self.filePath = path;
self.invDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
return self;
}

还有

- (void)dealloc {
[invDictionary release];
[filePath release];
[super dealloc];
}

关于ios - 哪一个是初始化 NSMutableDictionary 的正确方法。请任何人在这里纠正我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9262772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com