我有一个RSS解析器方法,我需要从我提取的html摘要中删除空格和其他无用的东西。我有一个NSMutableString类型'currentSummary‘。当我调用时:
currentSummary = [currentSummary
stringByReplacingOccurrencesOfString:@"\n" withString:@""];Xcode告诉我“警告:来自不同的Objective-C类型的赋值”
这有什么问题吗?
发布于 2009-10-30 08:13:50
如果currentSummary已经是一个NSMutableString,那么就不应该尝试给它分配一个常规的NSString (stringByReplacingOccurrencesOfString:withString:的结果)。
取而代之的是使用可变的等效replaceOccurrencesOfString:withString:options:range:,或者在赋值之前添加对mutableCopy的调用:
// Either
[currentSummary replaceOccurencesOfString:@"\n"
withString:@""
options:NULL
range:NSMakeRange(0, [receiver length])];
// Or
currentSummary = [[currentSummary stringByReplacingOccurrencesOfString:@"\n"
withString:@""]
mutableCopy];发布于 2011-12-21 11:21:28
当然,这也适用于嵌套元素:
*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]];
}下面的代码,如上面的用例所示,可能会有所帮助。
[self.contentTitleArray addObject:[[contentTitle
stringByReplacingOccurrencesOfString:@"&"
withString:@"&"]
mutableCopy]];发布于 2009-10-30 08:22:51
这通常意味着您在(在本例中) currentSummary的定义中省略了星号。
因此,您最有可能拥有:
NSMutableString currentSummary;当您需要时:
NSMutableString *currentSummary;在第一种情况下,由于Objective-C类是在类型结构中定义的,编译器会认为您试图将NSString赋值给结构。
令人痛苦的是,我经常犯这种打字错误。
https://stackoverflow.com/questions/1647292
复制相似问题