我试图在打印预览选项中显示字符串值。如果我没有在任何文本字段中输入任何文本,并且在单击打印按钮时应用程序崩溃。错误是[__NSCFString appendString:]:nil参数'。这是我的代码
-(NSMutableString*)pageStringToPrint {
NSMutableString * cnoteString = [[NSMutableString alloc]init];
[cnoteString setString:@"Class: "];
[cnoteString appendString:self.cnote.classname];
[cnoteString appendString:@"\nTeacher: "];
[cnoteString appendString:self.cnote.teacher];
[cnoteString appendString:@"\nSource: "];
[cnoteString appendString:self.cnote.source];
[cnoteString appendString:@"\nTopic: "];
[cnoteString appendString:self.cnote.topic];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM-dd-yyyy"];
NSString *dateString = [dateFormat stringFromDate:self.cnote.date];
[cnoteString appendString:@"\nDate: "];
[cnoteString appendString: dateString];
cnoteString = [NSMutableString stringWithFormat:@"<div id=\"mainContainer\"><div id=\"header\">%@</div>",[cnoteString kv_encodeHTMLCharacterEntities]];
这就是我创建Self.cnoe.classname
的方式- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self saveCNote ];
self.classNameTextField.text = self.cnote.classname;
self.topicTextField.text = self.cnote.topic;
self.sourceTextField.text = self.cnote.source;
self.teacherTextField.text = self.cnote.teacher;
self.essentialQuestionTextView.placeholder = @"Essential Question";
self.essentialQuestionTextView.layer.cornerRadius = 7.0f;
self.essentialQuestionTextView .layer.borderColor = [[UIColor grayColor] CGColor];
self.essentialQuestionTextView.layer.borderWidth= 1.0f;
self.essentialQuestionTextView.delegate = self;
self.essentialQuestionTextView.text = self.cnote.essentialQuestion;
// set the date
self.dateTextField.delegate = self;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM-dd-yyyy"];
self.dateTextField.text = [dateFormat stringFromDate:self.cnote.date];
}
保存Cnote:
- (void)saveCNote {
self.cnote.classname = self.classNameTextField.text;
self.cnote.topic = self.topicTextField.text;
self.cnote.source = self.sourceTextField.text;
self.cnote.teacher = self.teacherTextField.text;
[self.cnote save];
}
答案 0 :(得分:0)
您应该像nil
这样添加判断:
-(NSMutableString*)pageStringToPrint {
NSMutableString * cnoteString = [[NSMutableString alloc]init];
[cnoteString setString:@"Class: "];
[cnoteString appendString:self.note.classname ?: @""];
[cnoteString appendString:@"\nTeacher: "];
[cnoteString appendString:self.note.teacher ?: @""];
[cnoteString appendString:@"\nSource: "];
[cnoteString appendString:self.note.source ?: @""];
[cnoteString appendString:@"\nTopic: "];
[cnoteString appendString:self.note.topic ?: @""];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM-dd-yyyy"];
NSString *dateString = [dateFormat stringFromDate:self.note.date] ?: @"";
[cnoteString appendString:@"\nDate: "];
[cnoteString appendString: dateString];
cnoteString = [NSMutableString stringWithFormat:@"<div id=\"mainContainer\"><div id=\"header\">%@</div>",[cnoteString kv_encodeHTMLCharacterEntities]];