0

I am new in iOS and I am facing problem regarding to get more the one array in this loop

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

This loop only convert one array in string and I am not able to access the string outside this loop. I have 9 array

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"];

and I need to convert all the array into 9 different string using this loop is it possible?? How can I convert 9 different array in 9 different string.

Note - devices is NSMutableArray from core data and it type is NSString.

Thanks in Advance!

6
  • What type is devices and what type(s) contains it? Commented Dec 16, 2016 at 9:15
  • @vadian It is NSMutableArray.Its data is from core data. Commented Dec 16, 2016 at 9:16
  • 1
    Please add a concrete example what one of the lines returns and what the expected result is. Commented Dec 16, 2016 at 9:19
  • @vadian I want to send array element one by one in string format. Commented Dec 16, 2016 at 9:21
  • Why don't you try nested loops? It doesn't seem so hard. Try once and then ask for help. Commented Dec 16, 2016 at 9:21

2 Answers 2

1

If devices comes from Core Data it cannot contain NSString. Either it's NSManagedObject or NSDictionary. I guess it's NSManagedObject.

In this case it's more suitable to use the NSManagedObject as object in the loop and get the values for the nine keys respectively:

for (NSManagedObject *object in devices) {

    NSSString *string1 = [object valueForKey:@"key1"];
    NSSString *string2 = [object valueForKey:@"key2"];
    NSSString *string3 = [object valueForKey:@"key3"];
    NSSString *string4 = [object valueForKey:@"key4"];
    NSSString *string5 = [object valueForKey:@"key5"];
    NSSString *string6 = [object valueForKey:@"key6"];
    NSSString *string7 = [object valueForKey:@"key7"];
    NSSString *string8 = [object valueForKey:@"key8"];
    NSSString *string9 = [object valueForKey:@"key9"];
    // use the strings
}

An alternative is to use an array of NSDictionary from Core Data rather than NSManagedObject

Sign up to request clarification or add additional context in comments.

Comments

0

I guess you need this(atleast from your question)-

// Preparation
NSArray *array1 = @[@"one", @"two"];
NSArray *array2 = @[@"three", @"four"];
NSArray *devices = @[array1,array2];

// Real work
for (NSArray *array in devices) {
    for (NSString *strDevice in array) {
        NSLog(@"Print Your Data = %@",strDevice);
        //Send to ur server or do anything you want
    }
}

1 Comment

Tell me if you wanted this way only or not. If you explain your problem more elaborately then we can help you more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.