如何在NSString中获取NSArray对象并逐个发送该对象?

时间:2016-12-16 06:16:31

标签: ios objective-c arrays nsstring

我是iOS的新手,我面临着逐个发送数组对象的问题。我将数据保存在核心数据中然后我获取它,现在我想将它逐个发送到Web服务。 听到我的代码是从核心数据中获取对象并将其放入数组中。

NSManagedObject *device2 = [devices objectAtIndex:indexPath.row];

NSLog(@"Devices =%@",devices);
[cell.lbl1 setText:[NSString stringWithFormat:@"%@", [device2 valueForKey:@"key1"]]];
[cell.lbl2 setText:[NSString stringWithFormat:@"%@",[device2 valueForKey:@"key2"]]];
[cell.lbl3 setText:[NSString stringWithFormat:@"%@",[device2 valueForKey:@"key3"]]];
[cell.lbl4 setText:[NSString stringWithFormat:@"%@",[device2 valueForKey:@"key4"]]];
[cell.lbl5 setText:[NSString stringWithFormat:@"%@",[device2 valueForKey:@"key5"]]];
[cell.lbl6 setText:[NSString stringWithFormat:@"%@",[device2 valueForKey:@"key6"]]];
[cell.lbl7 setText:[NSString stringWithFormat:@"%@",[device2 valueForKey:@"key7"]]];
[cell.lbl8 setText:[NSString stringWithFormat:@"%@",[device2 valueForKey:@"key8"]]];
[cell.lbl9 setText:[NSString stringWithFormat:@"%@",[device2 valueForKey:@"key9"]]];

Array1=[devices valueForKey:@"key1"];
Array2 =[devices valueForKey:@"key2"];
Array3 =[devices valueForKey:@"key3"];
Array4=[devices valueForKey:@"key4"];
Array5=[devices valueForKey:@"key5"];
Array6=[devices valueForKey:@"key6"];
Array7=[devices valueForKey:@"key7"];
Array8=[devices valueForKey:@"key8"];
Array9=[devices valueForKey:@"key9"];


NSLog(@" Array =%@",Array1);
NSLog(@" Array =%@",Array2);
NSLog(@" Array =%@",Array3);
NSLog(@"Array =%@",Array4);
NSLog(@" Array =%@",Array5);
NSLog(@"Array =%@",Array6);
NSLog(@"Array =%@",Array7);
NSLog(@"Array =%@",Array8);

这是我将数组转换为字符串的方式:

DevicesString = [NSString stringWithFormat:@"%@",[Array componentsJoinedByString:@""]];
NSLog(@"Device String is  =%@",DevicesString);

听到NSLog的{​​{1}}:

Array8

在数组中,它是一个字符串值。

我想将数组的对象逐个发送到Web服务。我做得对吗?当我将数组转换为字符串时,它将数组的所有对象转换为字符串。

1 个答案:

答案 0 :(得分:1)

因此,根据您的评论,您可以将数据发送到服务器,如下所示。

NSArray *arrayData = //fill your data.

for (NSString *strDevice in arrayData) {
     NSLog(@"Print Your Data = %@",strDevice);
     //Send this strDevice to server in loop one by one.
}

希望这可以帮助您逐个发送数据。

要合并一个数组中的所有数组,请使用以下代码:

NSMutableArray *newArray = [[NSMutableArray alloc] init];

[newArray addObjectsFromArray: Array1];

[newArray addObjectsFromArray: Array2];

and so on.

上面的逻辑也可以工作,但还有一种方法可以将所有对象放在单个数组中并使用相同的循环并将数据发送到服务器。

NSMutableArray *allObjectsArray = [[NSMutableArray alloc] init];

//Add Your all devices array data directly in one array like below 

[allObjectsArray addObjectsFromArray:[devices valueForKey:@"key1"]];
[allObjectsArray addObjectsFromArray:[devices valueForKey:@"key2"]];
[allObjectsArray addObjectsFromArray:[devices valueForKey:@"key3"]];
[allObjectsArray addObjectsFromArray:[devices valueForKey:@"key4"]];
[allObjectsArray addObjectsFromArray:[devices valueForKey:@"key5"]];
[allObjectsArray addObjectsFromArray:[devices valueForKey:@"key6"]];
[allObjectsArray addObjectsFromArray:[devices valueForKey:@"key7"]];
[allObjectsArray addObjectsFromArray:[devices valueForKey:@"key8"]];
[allObjectsArray addObjectsFromArray:[devices valueForKey:@"key9"]];


for (NSString *strDevice in allObjectsArray) {
    NSLog(@"Print Your Data = %@",strDevice);
    //Send this strDevice to server in loop one by one.
}

对单个数组中的所有对象使用上面的代码。

  

注意:因为这不是为这些呼叫服务的优选方式   时间尽管你只需要在服务器上发送一个值   用逗号分隔一个字符串并用单个调用发送它。