-2

im not able to display the values from this code .plz provide a code to display the mutabledictionay and a

- (void)viewDidLoad{
    [super viewDidLoad];

     mdict =[[NSMutableDictionary alloc]init];
     [mdict setValue:@"abc" forKey:@"def"];

     [mdict release];
}

-(void)display{

     CFShow(mdict);
 }
4

2 回答 2

2

怎么用

-(void)display{

     NSLog(@"%@",mdict);

}

将您的方法更改为:

- (void)viewDidLoad{
    [super viewDidLoad];

     mdict =[[NSMutableDictionary alloc]init];
     [mdict setValue:@"abc" forKey:@"def"];


     //if you want to display use following statement
     [self display];

    // [mdict release];//you should not release it here.
}

注意:在viewDidLoad你使用[mdict release]; mdict 时会被释放!!!

这不应该在viewDidLoad,把那个声明放进去dealloc

于 2013-01-24T09:29:10.763 回答
0

首先,您需要创建NSMutableDictionaryis Public(在 .h 文件中声明),因为您在外部使用它并在dealloc方法中释放它的实例

- (void)viewDidLoad{
    [super viewDidLoad];

     self.mdict =[[NSMutableDictionary alloc]init];
     [self.mdict setValue:@"abc" forKey:@"def"];

     [self display];
}
-(void)display
{
   NSLog(@"%@",[self.mdict objectForKey:@"def"]);
}

-(void)dealloc并在方法中释放字典实例

于 2013-01-24T09:29:46.707 回答