gpt4 book ai didi

ios - 如何在 NSString 中获取 NSArray 对象并将该对象一一发送?

转载 作者:行者123 更新时间:2023-11-29 00:33:26 25 4
gpt4 key购买 nike

我是 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);

听到的是Array8NSLog:

Array8 =(
5,
5,
5,
5
)

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

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

最佳答案

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

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.
}

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

Note: As this is not preferable way to call service for these much time although there is only one value you need to send on server so make one string with comma separated and send it with single call.

关于ios - 如何在 NSString 中获取 NSArray 对象并将该对象一一发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41178325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com