【问题标题】:Not able resolve a NULL cString crash无法解决 NULL cString 崩溃
【发布时间】:2014-06-26 12:06:10
【问题描述】:

您好,我有以下代码导致应用程序崩溃:

cell.textLabel.text = [NSString stringWithCString:[[[self.arrayForTableSelLang objectAtIndex:indexPath.row] objectForKey:kSupport_question_title] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];

错误是:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSString stringWithCString:encoding:]: NULL cString

NSLog 给我:

Wie lange dauert es, bis meine Guthaben-Überweisung auf meiner Karte gebucht ist?

我该如何解决这个问题?

提前致谢。

【问题讨论】:

  • 如我所见,信息非常清楚。试着把那行代码分成几行,这样你就可以看到原因了。祝你好运。
  • @ArtemStepanenko 你能告诉我如何划分代码吗?或者我的代码有什么问题?
  • @user1954352,我不知道,你的代码有什么问题。因为问题出在提供的线路之外。这与 obj-c 的 ios 无关。这是关于逻辑的。
  • 你搞乱了字符串的编码。您不能在 ISO Latin 1 中导出然后再导入 UTF-8,因为这会导致字母混乱。

标签: ios iphone objective-c


【解决方案1】:

我的意思是:

NSDictionary *langDict = self.arrayForTableSelLang[indexPath.row];
NSString *string = langDict[kSupport_question_title];
char *cString = [string cStringUsingEncoding:NSISOLatin1StringEncoding];
cell.textLabel.text = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];

更新

以这种方式更新代码:

NSDictionary *langDict = self.arrayForTableSelLang[indexPath.row];
cell.textLabel.text = langDict[kSupport_question_title];

【讨论】:

  • 我在实现上述代码后得到了相同的结果。
  • 没错。但是现在你可以调试代码,看看在哪一行返回了 nil。
  • 我做了那个任务,在我的问题中我还提到当我得到“Wie lange dauert es, bis meine Guthaben-Überweisung auf meiner Karte gebucht ist?”它返回 nil。
  • stackoverflow 上的任何人怎么会知道在您的日志中打印该行的原因是什么?
  • @user1954352,当你说 NSLog 给了你一些东西时,你的意思是它是 stringWithCString: 方法的 input c-string 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
  • 2020-06-15
  • 2018-10-23
  • 2011-03-27
  • 2016-08-27
  • 2015-06-08
  • 2015-07-08
相关资源
最近更新 更多