2009-10-30 82 views
8

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

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

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

如何处理此问题?

回答

38

如果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];
+0

谢谢!工作很好。 – quantum 2009-10-30 00:19:30

+0

太棒了!非常感谢你! +1 – 2013-01-14 23:56:51

0

这通常意味着你的定义放弃了星号(在这种情况下)currentSummary。

所以,你最有可能有:

NSMutableString currentSummary; 

,当你需要:

NSMutableString *currentSummary; 

在第一种情况下,由于Objective-C类在类型结构定义的,编者认为你的努力将一个NSString分配给一个结构体。

我在痛苦的定期基础上犯这个错字。

3

这个伟大的工程嵌套元素以及课程:

* 编辑 *

// 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]]; 
+0

嗨,帕特!我不确定你的答案是否真的回答了问题。我还建议不要解释“contentTitleArray是...”,你可以写一些示例代码。你知道,所要求的最低限度是让你和问这个问题的人有一个共同的基础来理解你们每个人的含义。感谢您花时间写出答案,并祝Stack Overflow! – scraimer 2011-12-21 06:36:25

+1

好的,我已经提前添加了我的用例。希望这对路人有帮助。 – Pat 2011-12-21 21:17:02