gpt4 book ai didi

ios - 为什么 NSLog 显式地打印出换行符?

转载 作者:行者123 更新时间:2023-11-29 13:20:22 25 4
gpt4 key购买 nike

我有一个对象 (TCLesson),维护其他对象 (TCQuestion) 的集合。

外部的描述实现如下:

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

如果我能在那里阅读正确的语法,我将非常高兴。

现在怎么办?

最佳答案

由于您在 TCLesson 对象中有 TCQuestion 对象数组,当内部调用 TCLesson 的描述方法时,NSArray 的描述方法将被调用以获取问题属性,该属性再次在内部调用 TCQuestion 的描述方法。在此期间,NSArray的描述方法是对TCQuestion的格式化(将“\n”替换为“\n”)描述。

因此在返回字符串之前需要将TCLesson的描述方法中出现的“\\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"];
}

关于ios - 为什么 NSLog 显式地打印出换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14499784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com