我如何在一个单独的数组中添加这些dict

时间:2017-05-22 05:30:03

标签: ios objective-c

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"Name": self.txt1.text , @"Roll No.":self.txt2.text,}];

NSMutableDictionary *dict1= [NSMutableDictionary dictionaryWithDictionary:@{@"Name":self.txt3.text , @"Roll No.":self.txt4.text}];

NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:@{@"Name":self.txt5.text , @"Roll No.":self.txt6.text}];

NSMutableDictionary *dict3 = [NSMutableDictionary dictionaryWithDictionary:@{@"Name":self.txt7.text, @"Roll No.":self.txt8.text}];

如何在单个数组中添加这些字典...

3 个答案:

答案 0 :(得分:1)

在数组中添加对象(字典),如下所示: -

NSArray *arrayData = [[NSArray alloc]initWithObjects:dict,dict1,dict2,dict3, nil];

//在其他视图控制器中显示值(根据注释)NSDictionary: -

遍历数组,获取值,显示标签中的值(IBOutlets)

*dicData = [arrayData objectAtIndex:0];
    NSString *name = [dicData objectForKey:@"Name"];
    NSString *rollNo = [dicData objectForKey:@"Roll No."];
    labelName.text = name;
    labelRollNo.text = rollNo;

答案 1 :(得分:0)

attributes = [[NSMutableArray alloc] init];
[attributes addObject: dict.mutableCopy];
[attributes addObject: dict1.mutableCopy];
[attributes addObject: dict2.mutableCopy];
[attributes addObject: dict3.mutableCopy];

就是这样

答案 2 :(得分:0)

试试这个

NSMutableArray *arrDict = [NSMutableArray alloc]initWithObjects:@{@"Name" : self.txt1.text , @"Roll No" : self.txt2.text},@{@"Name" : self.txt7.text, @"Roll No" : self.txt8.text} nil

使用NSArray Literal

NSArray *arrDict =@[@{@"Name" : self.txt1.text , @"Roll No" : self.txt2.text},@{@"Name" : self.txt7.text, @"Roll No" : self.txt8.text} ];

在Next ViewController中,您可以通过循环

获取此词典
for (NSDictionary *dicUser in arrDict) {
        // this dictionary which contained by arrDict
        UILabel *lblName = [dicUser objectForKey:@"Name"];
        UILabel *lblRollNumber = [dicUser objectForKey:@"Roll No"];

    }