Muju
Muju

Reputation: 979

How to get NSArray object in NSString and send that object one by one?

I am new in iOS and I am facing a problem regarding to send array object one by one.I save data in core data then I fetch it and now I want to send it one by one to the web service. Hear is my code to fetch the object from core data and get it in array..

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

This is how I convert array to string:

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

Hear is the NSLog of Array8:

Array8 =(
    5,
    5,
    5,
    5
)

In array it is a string value.

I want to send the object of array one by one to the web service. Did I am doing right? When I am converting array to string it convert all the object of array to string.

Upvotes: 0

Views: 323

Answers (1)

CodeChanger
CodeChanger

Reputation: 8351

So based on your comment you can send your data to server like below.

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

Hope this will helps you to send your data to serve one by one.

To Combine all array in One array use below code :

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

[newArray addObjectsFromArray: Array1];

[newArray addObjectsFromArray: Array2];

and so on.

Above logic also can work but there is one more way to get all objects in single array and use same loop and send data to server.

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

Use above code for all object in single array.

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.

Upvotes: 1

Related Questions