【问题标题】:Incompatible pointer type error when using NSMutableString and stringByReplacingOccurrencesOfString使用 NSMutableString 和 stringByReplacingOccurrencesOfString 时出现不兼容的指针类型错误
【发布时间】:2012-07-28 23:46:50
【问题描述】:

下面显示的代码的目的是从文本字段中获取一个字符串并删除逗号和左括号,以准备将内容转换为浮点数。例如,它将数字 1,234,567 更改为 1234567。

代码 sn-p 有效,但返回信息性错误“不兼容的指针类型分配给 NSMutableString * from String *”

当我使用 NSString 而不是 NSMutableString 时,我没有收到任何错误,但返回的值是一个空字符串。

使用 NSMutableString 方法,有什么问题以及如何更改它以消除“不兼容的指针类型”错误。

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

【问题讨论】:

  • 哪一行给出警告?

标签: ios


【解决方案1】:

stringByReplacingOccurrencesOfString是NSString的方法,返回值也是NSString。所以不能将返回值赋给NSMutableString 变量。

相反,您可以使用NSMutableString 中的replaceOccurrencesOfString:withString:options:range: 方法:

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),使用 NSMutableString 和 replaceOccurrencesOfString:withString:options:range: 会有点效率更高。

【讨论】:

    【解决方案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:

    【讨论】:

      【解决方案3】:

      切换到 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
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      相关资源
      最近更新 更多