【问题标题】:[__NSCFString appendString:]: nil argument' error[__NSCFString appendString:]: nil argument' 错误
【发布时间】:2016-01-20 07:36:41
【问题描述】:

我试图在打印预览选项中显示字符串值。如果我没有在任何文本字段中输入任何文本并且在单击打印按钮时应用程序崩溃。错误是 [__NSCFString appendString:]: nil argument '.这是我的代码

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

保存注释:

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

标签: ios printing nsmutablestring


【解决方案1】:

你应该像这样为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]];

【讨论】:

  • @luk2302,如果你的意思是这段代码` [cnoteString appendString: dateString] `?变化 dateString 不是 nil
  • 啊,是的,不是appendString 是问题,而是[dateFormat stringFromDate:self.note.date ?: @""]:您不能将@"" 传递给该函数-抱歉造成混淆。
  • 如果我删除 [cnoteString setString 或 appendString:@"Class:"];它工作正常。但我需要显示类名。
  • @luk2302,谢谢提醒。我不小心忽略了这个变量的类型
  • Vignes 你能添加更多关于如何创建 self.note.classname 变量的代码吗?由于问题是这个字符串,我们可以帮助解决它。
猜你喜欢
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多