ByReplacingOccurrencesOfString警告

ByReplacingOccurrencesOfString警告

本文介绍了NSMutableString stringByReplacingOccurrencesOfString警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RSS解析器方法,我需要从我提取的html摘要中删除空格和其他废话。我有一个NSMutableString类型'currentSummary'。当我打电话时:

I've got an RSS parser method and I need to remove whitespace and other nonsense from my extracted html summary. I've got a NSMutableString type 'currentSummary'. When I call:

currentSummary = [currentSummary
        stringByReplacingOccurrencesOfString:@"\n" withString:@""];

Xcode告诉我警告:从不同的Objective-C类型分配

Xcode tells me "warning: assignment from distinct Objective-C type"

这有什么问题?

推荐答案

如果 currentSummary 已经是一个NSMutableString你不应该尝试为它分配一个常规的NSString( stringByReplacingOccurrencesOfString:withString:的结果)。

If currentSummary is already a NSMutableString you shouldn't attempt to assign a regular NSString (the result of stringByReplacingOccurrencesOfString:withString:) to it.

而是使用可变等效 replaceOccurrencesOfString:withString:options:range:,或者添加对 mutableCopy的调用

Instead use the mutable equivalent replaceOccurrencesOfString:withString:options:range:, or add a call to mutableCopy before the assignment:

// Either
[currentSummary replaceOccurencesOfString:@"\n"
                               withString:@""
                                  options:NULL
                                    range:NSMakeRange(0, [receiver length])];

// Or
currentSummary = [[currentSummary stringByReplacingOccurrencesOfString:@"\n"
                                                            withString:@""]
                  mutableCopy];

这篇关于NSMutableString stringByReplacingOccurrencesOfString警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:31