我在NSArray和NSMutableArray之间的兼容性方面遇到了麻烦?

时间:2011-07-15 17:55:34

标签: objective-c cocoa-touch

我在NSArray和NSMutableArray之间的兼容性方面遇到了麻烦? - >不兼容的Objective-C类型分配“struct NSArray *”,期望“struct NSMutableArray”

NSMutableArray *nsmarrRow;

NSString *nsstrFilePath = [[NSBundle mainBundle] pathForResource: nsstrFilename ofType: nsstrExtension];
NSString *nsstrFileContents = [NSString stringWithContentsOfFile: nsstrFilePath encoding:NSUTF8StringEncoding error: NULL];

//break up file into rows
nsmarrRow = [nsstrFileContents componentsSeparatedByString: nsstrRowParse];//<--Incompatible Objective-C types assigning "struct NSArray *", expected "struct NSMutableArray"

我已经尝试将“NSString声明”设为“NSMutableString”......制造了更多问题。

感谢

2 个答案:

答案 0 :(得分:5)

您无法通过

获取可变数组
nsmarrRow = [nsstrFileContents componentsSeparatedByString: nsstrRowParse];

你需要,

nsmarrRow = [NSMutableArray arrayWithArray:[nsstrFileContents componentsSeparatedByString: nsstrRowParse]];

NSString更改为NSMutableString不会为您提供NSMutableArray个对象。您将获得NSArray对象,就像NSString一样。您必须使用它来使用上述方法获得NSMutableArray

答案 1 :(得分:2)

componentsSeparatedByString方法返回NSArray。请尝试以下方法:

[NSMutableArray arrayWithArray:[nsstrFileContents componentsSeparatedByString: nsstrRowParse]];