2012-02-13 30 views
0

我想在我的课堂上创建一个NSMutableDictionary。我已经阅读了很多帖子在stackoverflow来了解差异。但现在我完全困惑。所以任何一个人都纠正我,哪一个是在我的类中初始化一个NSMutableDictionary的正确方法。我有我的许多应用领域。所以建议我使用的变量初始化的好方法访问此dictiionary ...哪一个是初始化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 

请任何一个帮助我获得使用变量的正确方式....

回答

2
- (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]; 
} 
+0

所以你试图指出哪个接口。如果我像这样初始化,我将如何在' - (void)setObjectAtKey'和' - (void)dealloc'中调用..... – 2012-02-13 15:18:18

+0

与自己或直接.... – 2012-02-13 15:18:44

+0

[instance setObjectsAtKey:] or don'不了解你 – NeverBe 2012-02-13 15:30:46

相关问题