EmployeesArray = [[NSMutableArray alloc] initWithCapacity:100];
    Employees *Emp1 = [[Employees alloc] init];
    Emp1.Number = 1;
    Emp1.Name = @"Jamal";
    Emp1.Age = 16;
    Emp1.Gender = @"Male";
  [EmployeesArray addObject:Emp1];
 [Dic1 setValue:Emp1 forKey:@"A"];

- (IBAction)Retrieve:(id)sender {

  Employees *objEmp2 = [Dic1 valueForKey:@"A"];
    printf(objEmp2.Name);

}


为什么此代码未检索名称?和错误出现了??

最佳答案

您是否已初始化Dic1词典?

// try doing this
Dic1 = [[NSMutableDictionary alloc] init];

EmployeesArray = [[NSMutableArray alloc] initWithCapacity:100];
    Employees *Emp1 = [[Employees alloc] init];
    Emp1.Number = 1;
    Emp1.Name = @"Jamal";
    Emp1.Age = 16;
    Emp1.Gender = @"Male";
  [EmployeesArray addObject:Emp1];
 [Dic1 setObject:Emp1 forKey:@"A"];

- (IBAction)Retrieve:(id)sender {

  Employees *objEmp2 = [Dic1 objectForKey:@"A"];
    printf(objEmp2.Name);

}

关于objective-c - NSMutableDictionary无法检索对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9214704/

10-09 09:14