无法用字符串替换字符串 [英] Can't replacing string with string

查看:212
本文介绍了无法用字符串替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 UITableViewCell 带有 detailTextLabel ,其中包含我想用空格替换的字符串(换句话说就是删除)。它看起来像这样:

I have UITableViewCell with detailTextLabel which contains string that i want to replace with empty space(in other words to delete).It looks like this:

cell.detailTextLabel?.text = newsModel.pubDate

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

Now, the problem is that when i write cell.detailTextLabel?.text?.stringByReplacingOccurrencesOfString("+0000", withString: " ")

它无效,编译器说:


调用结果
'stringByReplacingOccurrencesOfString(_:withString:options:range :)'是
unused

"Result of call 'stringByReplacingOccurrencesOfString(_:withString:options:range:)' is unused"

任何人都可以告诉我解决方案吗?
谢谢

Anyone can tell me the solution? Thanks

推荐答案

stringByReplacingOccurencesOfString:withString: method 返回一个字符串,它是用替换替换搜索字符串的结果。警告意味着您正在调用一个具有非空返回值的方法,而您没有使用该方法。

The stringByReplacingOccurencesOfString:withString: method returns a string that is the result of replacing your search string with the replacement. The warning means that you are calling a method with a non-void return value that you aren't using.

来自 documentation (斜体)由我添加以强调)

From the documentation (italics added by me for emphasis)


返回 new 字符串,其中所有出现的目标字符串都在接收器中被另一个给定的字符串替换。

Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.

你可以使用这个:

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

您收到此警告的原因是因为该方法不修改原始字符串而返回 字符串,不要用。如果您要使用

The reason you get this warning is because the method doesn't modify the original string and returns a new string, which you don't use. If you were to use

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

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

You wouldn't get the warning because you are assigning the return value to the cell text and therefore "using" the result of the call.

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

The two methods are exactly the same, except one is shorter.

这篇关于无法用字符串替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆