initWithObjectAndkeys 如何使用 iOS 中的变量

How initWithObjectAndkeys using variable in iOS

initWithObjectAndkeys如何使用变量,我想循环遍历类别中的项目,请帮助

如果我这样使用 NSArray

data = [saved_result valueForKey:@"data"];
        NSLog(@"%@",[data valueForKey:@"category"]);

         NSArray *myArray = [[NSArray alloc]initWithObjects:data, nil];
    for (id tempObject in myArray) {
        NSLog(@"Single element: %@", tempObject);

        }

这里是NSArray类型变量保存数据的内容,有助于大家理解代码。 保存的结果 =

(
            {
            data =         {
                amount = 10;
                category = Food;
                dealid = 28;
                enddate = "2016-12-31";
                retailername = mukesh;
                startingdate = "2015-01-10";
                title = gter;
            };
            msg = "deals found";
            status = true;
        },
            {
            data =         {
                amount = 10;
                category = Shopping;
                dealid = 44;
                enddate = "2016-12-31";
                retailername = divya;
                startingdate = "2015-01-10";
                title = welcome;
            };
            msg = "deals found";
            status = true;
        }
            )


2015-02-24 17:09:57.985 Raott[3316:59975] Single element: (
        {
        amount = 10;
        category = Food;
        dealid = 28;
        enddate = "2016-12-31";
        retailername = mukesh;
        startingdate = "2015-01-10";
        title = gter;
    },
        {
        amount = 10;
        category = Shopping;
        dealid = 44;
        enddate = "2016-12-31";
        retailername = divya;
        startingdate = "2015-01-10";
        title = welcome;
    },
        {
        amount = 859;
        category = Shopping;
        dealid = 53;
        enddate = "2016-12-31";
        retailername = mukesh;
        startingdate = "2015-01-10";
        title = ghk;
    },
        {
        amount = 50;
        category = Shopping;
        dealid = 64;
        enddate = "2016-12-31";
        retailername = mukesh;
        startingdate = "2015-01-10";
        title = "new deal";
    },
        {
        amount = 12;
        category = Shopping;
        dealid = 68;
        enddate = "2016-12-31";
        retailername = mukesh;
        startingdate = "2015-01-10";
        title = hello;
    },
        {
        amount = 213;
        category = Food;
        dealid = 69;
        enddate = "2016-12-31";
        retailername = mukesh;
        startingdate = "2015-01-10";
        title = fgfdg;
    }
)

您保存的结果是 NSArrayarrResult ,因此我们将 loop 通过它。

for(NSDictionary *dictObject in arrResult)
{
   //Get Data Dictionary
   NSDictionary *dictData = [dictObject valueForKey:@"data"];
  //Now have data so traverse
  for (NSString*strkey in dictData) {
        id value = [dictData objectForKey:strkey];
        NSLog(@"id = %@",value);
    }
}