objective-c NSMutableString stringByReplacingOccurrencesOfString 警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1647292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSMutableString stringByReplacingOccurrencesOfString Warning
提问by quantum
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:
我有一个 RSS 解析器方法,我需要从我提取的 html 摘要中删除空格和其他废话。我有一个 NSMutableString 类型“currentSummary”。当我打电话时:
currentSummary = [currentSummary
stringByReplacingOccurrencesOfString:@"\n" withString:@""];
Xcode tells me "warning: assignment from distinct Objective-C type"
Xcode 告诉我“警告:来自不同 Objective-C 类型的赋值”
What's wrong with this?
这有什么问题?
回答by Tim
If currentSummaryis already a NSMutableString you shouldn't attempt to assign a regular NSString (the result of stringByReplacingOccurrencesOfString:withString:) to it.
如果currentSummary已经是 NSMutableString,则不应尝试为其分配常规 NSString( 的结果stringByReplacingOccurrencesOfString:withString:)。
Instead use the mutable equivalent replaceOccurrencesOfString:withString:options:range:, or add a call to mutableCopybefore the assignment:
而是使用可变的等价物replaceOccurrencesOfString:withString:options:range:,或者mutableCopy在赋值之前添加一个调用:
// Either
[currentSummary replaceOccurencesOfString:@"\n"
withString:@""
options:NULL
range:NSMakeRange(0, [receiver length])];
// Or
currentSummary = [[currentSummary stringByReplacingOccurrencesOfString:@"\n"
withString:@""]
mutableCopy];
回答by Pat
This works great for nested elements as well of course:
这当然也适用于嵌套元素:
*Edited*
*编辑*
// Get the JSON feed from site
myRawJson = [[NSString alloc] initWithContentsOfURL:[NSURL
URLWithString:@"http://yoursite.com/mobile_list.json"]
encoding:NSUTF8StringEncoding error:nil];
// Make the content something we can use in fast enumeration
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary * myParsedJson = [parser objectWithString:myRawJson error:NULL];
[myRawJson release];
allLetterContents = [myParsedJson objectForKey:@"nodes"];
// Create arrays just for the title and Nid items
self.contentTitleArray = [[NSMutableArray alloc]init];
for (NSMutableDictionary * key in myArr) {
NSDictionary *node = [key objectForKey:@"node"];
NSMutableString *savedContentTitle = [node objectForKey:@"title"];
// Add each Title and Nid to specific arrays
//[self.contentTitleArray addObject:contentTitle];
//change each item with & to &
[self.contentTitleArray addObject:[[savedContentTitle
stringByReplacingOccurrencesOfString:@"&"
withString:@"&"]
mutableCopy]];
}
The code below, as shown in the use-case above might be helpful.
下面的代码,如上面的用例所示,可能会有所帮助。
[self.contentTitleArray addObject:[[contentTitle
stringByReplacingOccurrencesOfString:@"&"
withString:@"&"]
mutableCopy]];
回答by TechZen
That usually means you dropped the asterisks in the definition of (in this case) currentSummary.
这通常意味着您在(在本例中)currentSummary 的定义中删除了星号。
So you most likely have:
所以你很可能有:
NSMutableString currentSummary;
when you need:
当您需要时:
NSMutableString *currentSummary;
In the first case, since Objective-C classes are defined in type structures, the complier thinks your trying to assign a NSString to a struct.
在第一种情况下,由于 Objective-C 类是在类型结构中定义的,编译器认为您尝试将 NSString 分配给结构。
I make this typo on a distressingly regular basis.
我经常犯这样的错字。

