2009-10-30 29 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

+0

素敵!どうもありがとうございました! +1 –

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 is ..."を説明する代わりに、いくつかのサンプルコードを書くこともできます。ご存知のように、あなたとその質問をしている人の両方が、あなたのそれぞれが何を意味するのかを理解するための共通の基礎を持つように、最低限必要です。答えを書く時間をとってくれてありがとう、スタックオーバーフローで幸運! – scraimer

+1

さて、私は前に進み、私のユースケースを追加しました。うまくいけば、これは通行人のために役立つだろう。 – Pat