1

他のオブジェクトのコレクション(TCQuestion)を維持しているオブジェクト(TCLesson)があります。

アウターの説明の実装は次のようになります。

-(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];
}

次のような内部オブジェクトについてもほぼ同じです。

-(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];
}

しかし、ロギングに関しては、内部オブジェクトの説明が乱暴になり、次のような奇妙なフォーマット文字が出力されます。

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

そこで正しい構文を読むことができれば、とてもうれしく思います。

今何をする?

4

1 に答える 1

1

TCLessonオブジェクトにTCQuestionオブジェクトの配列があるため、TCLessonのdescriptionメソッドが内部的に呼び出されると、NSArrayのdescriptionメソッドが呼び出され、再びTCQuestionのdescriptionメソッドを内部的に呼び出します。この間、NSArrayのdescriptionメソッドは、TCQuestionの説明をフォーマット(「\n」を「\n」に置き換える)します。

したがって、文字列を返す前に、TCLessonのdescriptionメソッドで「\\n」の出現箇所を「\n」に置き換える必要があります。

-(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"];
}
于 2013-01-24T12:31:41.973 に答える