不能用stringreplacestring

我有UITableViewCelldetailTextLabel其中包含string,我想用空间replace(换句话说,删除)。它看起来像这样:

 cell.detailTextLabel?.text = newsModel.pubDate 

现在,问题是,当我写cell.detailTextLabel?.text?.stringByReplacingOccurrencesOfString("+0000", withString: " ")

它不工作,编译器说:

“调用结果”stringByReplacingOccurrencesOfString(_:withString:options:range :)'未使用“

任何人都可以告诉我解决scheme? 谢谢

stringByReplacingOccurencesOfString:withString:方法返回一个string,它是replacesearchstring的结果 。 警告意味着您正在调用一个非空的返回值的方法,您不使用。

从文档 (斜体为我强调)

返回一个string,其中接收方中所有出现的目标string被另一个给定的stringreplace。

你可以使用这个:

 cell.detailTextLabel?.text = newsModel.pubDate.stringByReplacingOccurrencesOfString("+0000", withString: " ") 

您得到此警告的原因是因为该方法不会修改原始string并返回您不使用的string。 如果你要使用

 cell.detailTextLabel?.text? = cell.detailTextLabel?.text?.stringByReplacingOccurrencesOfString("+0000", withString: " ") 

您不会得到警告,因为您将返回值分配给单元格文本,因此“使用”调用的结果。

这两种方法是完全一样的,除了一个更短。