1

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];'
2
  • 2
    componentsJoinedByString:returns a NSString, you don't use the returned value. Commented Jun 26, 2020 at 12:14
  • @Larme Can you please post alternate solution , If i don't use returned value.. Commented Jun 26, 2020 at 18:25

1 Answer 1

1

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

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

1 Comment

Please do not try to paste long code into comments it is unreadable. Ask a new question as covered in the last paragraph of the above answer. (When you do this you may if you wish post a comment here with a link to it [press "help" t the right when typing a comment to see how to add a link]) That said it looks like you are using appendString, rather than appendFormat, and are not appending any spacing or line breaks.

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.