[__NSCFString appendString:]:无参数错误
问题描述:
我试图在打印预览选项中显示字符串值。如果我没有在任何文本字段中输入任何文本,并且点击打印按钮,应用程序会崩溃。并且错误是[__NSCFString appendString:]:无argument'.Here是我的代码[__NSCFString appendString:]:无参数错误
-(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];
}
保存C注:
- (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];
}
答
你应该为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]];
问题出在哪里?你不允许追加'nil' – luk2302