首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSMutableString stringByReplacingOccurrencesOfString警告

NSMutableString stringByReplacingOccurrencesOfString警告
EN

Stack Overflow用户
提问于 2009-10-30 08:10:49
回答 3查看 22.8K关注 0票数 8

我有一个RSS解析器方法,我需要从我提取的html摘要中删除空格和其他无用的东西。我有一个NSMutableString类型'currentSummary‘。当我调用时:

代码语言:javascript
复制
currentSummary = [currentSummary 
        stringByReplacingOccurrencesOfString:@"\n" withString:@""];

Xcode告诉我“警告:来自不同的Objective-C类型的赋值”

这有什么问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-10-30 08:13:50

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

取而代之的是使用可变的等效replaceOccurrencesOfString:withString:options:range:,或者在赋值之前添加对mutableCopy的调用:

代码语言:javascript
复制
// Either
[currentSummary replaceOccurencesOfString:@"\n" 
                               withString:@"" 
                                  options:NULL
                                    range:NSMakeRange(0, [receiver length])];

// Or
currentSummary = [[currentSummary stringByReplacingOccurrencesOfString:@"\n"
                                                            withString:@""]
                  mutableCopy];
票数 38
EN

Stack Overflow用户

发布于 2011-12-21 11:21:28

当然,这也适用于嵌套元素:

*Edited*

代码语言:javascript
复制
// 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]];

    }

下面的代码,如上面的用例所示,可能会有所帮助。

代码语言:javascript
复制
[self.contentTitleArray addObject:[[contentTitle 
                                    stringByReplacingOccurrencesOfString:@"&" 
                                    withString:@"&"] 
                                    mutableCopy]];
票数 3
EN

Stack Overflow用户

发布于 2009-10-30 08:22:51

这通常意味着您在(在本例中) currentSummary的定义中省略了星号。

因此,您最有可能拥有:

代码语言:javascript
复制
NSMutableString currentSummary;

当您需要时:

代码语言:javascript
复制
NSMutableString *currentSummary;

在第一种情况下,由于Objective-C类是在类型结构中定义的,编译器会认为您试图将NSString赋值给结构。

令人痛苦的是,我经常犯这种打字错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1647292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档