从NSArray分配给NSMutableString的不兼容的指针类型

时间:2013-12-02 14:28:23

标签: ios objective-c

我是iOS的新手。

我有以下警告,我不知道如何解决:

NSString *getNos=[textField.text stringByReplacingCharactersInRange:range withString:string];

NSMutableArray *allNos = [[getNos stringByReplacingOccurrencesOfString:@" " withString:@""]componentsSeparatedByString:@","];

NSString *matchNo = [[allNos lastObject] stringByReplacingOccurrencesOfString:@" " withString:@""];

在第二行和第三行,我收到以下警告信息:

Incompatible pointer types assigning to 'NSMutableArray * from NSArray *
and
Incompatible pointer types sending NSString * to parameter of type NSMutableString*

2 个答案:

答案 0 :(得分:7)

stringByReplacingOccurrencesOfString:返回NSArray而不是NSMutableArray

如果您确实需要NSMutableArray,请将NSArray更改为mutableCopy或添加NSMutableArray

NSMutableArray *allNos = [[[getNos stringByReplacingOccurrencesOfString:@" " withString:@""]componentsSeparatedByString:@","] mutableCopy];

还有一个拼写错误:缺少“*”。

注意:通过使用中间体,代码更容易查看,更容易查明错误:

NSString *getNos = @"a b, c d";
NSString *cleanNos = [getNos stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableArray *allNos = [[cleanNos componentsSeparatedByString:@","] mutableCopy];
NSLog(@"allNos: %@", allNos);

NSLog输出:

allNos: (
    ab,
    cd
)

答案 1 :(得分:0)

您正在尝试将NSArray分配给NSMutableArray,但您也错过了指针*,请替换此

 NSMutableArray  allNos = ...

用这个

NSMutableArray  *allNos = [NSMutableArray arrayWithArray://YOUR stuff here]