Geri Borbás
Geri Borbás

Reputation: 16628

Why NSLog prints out linebreaks explicitly?

I have an object (TCLesson), maintaining collection of other objects (TCQuestion).

Description impolementation for the outer is like:

-(NSString*)description
{
    return [NSString stringWithFormat:
            @"TCLesson \n"
            "welcomeMessages: \n%@ \n"
            "questionPhrases: \n%@ \n"
            "goodAnswerAcknowledgements: \n%@ \n"
            "wrongAnswerAcknowledgements: \n%@ \n"
            "questions: \n%@ \n",
            self.welcomeMessages,
            self.questionPhrases,
            self.goodAnswerAcknowledgements,
            self.wrongAnswerAcknowledgements,
            self.questions];
}

Pretty the same for the inner objects, like:

-(NSString*)description
{
    return [NSString stringWithFormat:
            @"TCQuestions \n"
            "question: \n%@ \n"
            "pictureFileName: \n%@ \n"
            "answers: \n%@ \n"
            "hints: \n%@ \n",
            self.question,
            self.pictureFileName,
            self.answers,
            self.hints];
}

But when it comes to logging, the inner object's description goes wild, and prints out some weird formatting characters, like:

TCLesson 
welcomeMessages: 
(
    "Hi <USERNAME>, let's learn the colors!",
    "Hi <USERNAME>, let's get into this lesson about the colors!",
    "Yay! We gonna learning about the colors, <USERNAME>!"
) 
questionPhrases: 
(
    "What color is this?",
    "What is the name of this color?",
    "What color do you see on the picture?"
) 
goodAnswerAcknowledgements: 
(
    "Yay! Exactly, <USERNAME>. This color is <ANSWER>.",
    "As you said, this color is <ANSWER>.",
    "Woot! Yes, you can see <ANSWER> on the picture."
) 
wrongAnswerAcknowledgements: 
(
    "No, but I can help you.",
    "Wrong answer, but I'm here to help.",
    "Apperantly not, here is a little help."
) 
questions: 
(
    "TCQuestions \nquestion: \n<DEFAULT> \npictureFileName: \norange.jpg \nanswers: \n(\n    ORANGE,\n    GOLD\n) \nhints: \n(\n    \"It is the color of the pumpkin.\",\n    \"It is the color of the orange.\",\n    \"It is the color of the carrot.\"\n) \n",
    "TCQuestions \nquestion: \n<DEFAULT> \npictureFileName: \nyellow.jpg \nanswers: \n(\n    YELLOW,\n    AMBER,\n    LEMON\n) \nhints: \n(\n    \"It is the color of the sun.\",\n    \"It is the color of the banana.\",\n    \"It is the color of the corn.\"\n) \n"
) 

I'd be very pleased, if I could read the correct syntax there.

What to do now?

Upvotes: 1

Views: 458

Answers (1)

Rahul Wakade
Rahul Wakade

Reputation: 4805

As you have array of TCQuestion object in TCLesson object, when description method of TCLesson is called internally description method of NSArray will get called for questions property which again internally calls description method of TCQuestion. During this, NSArray's description method is formatting(replacing "\n" with "\n") description of TCQuestion.

So you need to replace occurrence of "\\n" with "\n" in description method of TCLesson before returning string.

-(NSString*)description
{
    return [[NSString stringWithFormat:
            @"TCLesson \n"
            "welcomeMessages: \n%@ \n"
            "questionPhrases: \n%@ \n"
            "goodAnswerAcknowledgements: \n%@ \n"
            "wrongAnswerAcknowledgements: \n%@ \n"
            "questions: \n%@ \n",
            self.welcomeMessages,
            self.questionPhrases,
            self.goodAnswerAcknowledgements,
            self.wrongAnswerAcknowledgements,
            self.questions] stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
}

Upvotes: 1

Related Questions