0

以下に示すコードの目的は、テキストフィールドから文字列を取得し、カンマと左括弧を削除して、内容を float に変換する準備をすることです。たとえば、数値 1,234,567 を 1234567 に変更します。

コード スニペットは機能しますが、「互換性のないポインター タイプが NSMutableString * from String * に割り当てられています」という情報エラーを返します。

NSMutableString の代わりに NSString を使用すると、エラーは発生しませんが、返される値は空の文字列です。

NSMutableString 方法論を使用して、何が問題で、これを変更して「互換性のないポインター型」エラーを解消するにはどうすればよいですか。

NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:datacellR2C2.text];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@"," withString:@""];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@")" withString:@""];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@"(" withString:@"-"];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@" " withString:@""];
4

3 に答える 3

3

stringByReplacingOccurrencesOfStringNSString のメソッドであり、戻り値も NSString です。NSMutableStringしたがって、戻り値を変数に割り当てることはできません。

replaceOccurrencesOfString:withString:options:range:代わりに、次のメソッドを使用できますNSMutableString

NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:@"1,234,567"];
[cleanedDataCellTest replaceOccurrencesOfString:@"," withString:@"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
[cleanedDataCellTest replaceOccurrencesOfString:@")" withString:@"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
[cleanedDataCellTest replaceOccurrencesOfString:@"(" withString:@"-" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];
[cleanedDataCellTest replaceOccurrencesOfString:@" " withString:@"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];

しかし、パフォーマンスを考えると、NSString を使用し、その方法stringByReplacingOccurrencesOfStringがより効率的であると思います。

更新: 申し訳ありませんが、昨日は「パフォーマンス」をテストしませんでした。文字列 (約 26KB) 内の何かを置き換えることで、今テストを行いreplaceOccurrencesOfString:withString:options:range:まし

于 2012-07-29T00:43:55.813 に答える
2
NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:datacellR2C2.text];
[cleanedDataCellTest replaceOccurrencesOfString:@"," withString:@"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];

参照:https ://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsmutablestring_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableString/replaceOccurrencesOfString:withString:options :range

于 2012-07-29T00:06:15.690 に答える
1

NSStringへの切り替えは私のために働いた...:

NSString *cleanedDataCellTest = [[NSString alloc] initWithString:@"1,234,098"];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@"," withString:@""];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@")" withString:@""];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@"(" withString:@"-"];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"cleanedDataCellTest = %@", cleanedDataCellTest);

ショー:

cleanedDataCellTest = 1234098
于 2012-07-29T00:09:44.713 に答える