Mohammad Arman
Mohammad Arman

Reputation: 508

how to print array element line by line using objective c

I want to print array element line by line like periodic table , here is my code..
Output should be in vertical

NSArray* myInfo1 = [NSArray arrayWithArray:allhrStartTime];
[myInfo1 componentsJoinedByString:@"\n"];

NSArray* myInfo2 = [NSArray arrayWithArray:allhrStopTime];
[myInfo2 componentsJoinedByString:@"\n"];

NSArray* myInfo3 = [NSArray arrayWithArray:allhrStartLocation];
[myInfo3 componentsJoinedByString:@"\n"];

NSArray* myInfo4 = [NSArray arrayWithArray:allhrStopLocation];
[myInfo4 componentsJoinedByString:@"\n"];


NSArray* allInfo = [NSArray arrayWithObjects:headers, myInfo1,myInfo2 ,myInfo3,myInfo4, nil];'

Upvotes: 1

Views: 356

Answers (1)

CRD
CRD

Reputation: 53000

As @Larme stated in a comment you are calling a method which returns a value but not using that value. You also appear to have some confusion between types.

Try this:

NSString *allhrStartTimeString = [allhrStartTime componentsJoinedByString:@"\n"];
NSLog(allhrStartTimeString);

The first line takes your original array and joins the values in it, separating each by a new line, to produce a string. The second line shows that string on the console. You should see the one element per line you seek.

However your code suggests you want the contents of each array to appear as a column in the output producing a tabular result. To do that you'll need to do a bit more work:

  • Start by creating an empty instance of NSMutableString
  • Using appendFormat: add the first line containing the headers to this mutable string
  • Now loop for ix = 0 to maximum index of your column arrays
    • Using appendFormat: again add the next two to your mutable string using the ix'th element of each of your arrays (allhrStartTime, allhrStopTime, allhrStartLocation, allhrStopLocation)
  • end loop
  • your mutable string now has your data represented in tabular form in it, display it in a suitable text field or dump it to the console, e.g. with NSLog.

If you have difficulty coding this ask a new question, link to this one so people know the history, show the code you have written, and explain where you are having difficulty. Someone will undoubtedly help you along. HTH

Upvotes: 1

Related Questions