Incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *' [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 11:29:37

问题


This is my code that is causing the above warning:

    NSMutableArray *tbValueArray = [NSArray arrayWithObjects:oServices1.text,
                oServices2.text,oServices3.text,oServices4.text,oServices5.text,oServices6.text,
                oServices7.text,oServices8.text,oServices9.text,oServices10.text,oServices11.text,
                oServices12.text,oServices13.text,oServices14.text,oServices15.text,oServices16.text,
                oServices17.text,oServices18.text,oServices19.text,oServices20.text,oServices21.text,
                oServices22.text,oServices23.text,oServices24.text,nil];

How do I change this to remove the warning? (I have looked at Google and SO and found nothing that applies).


回答1:


Are you trying to create an NSMutableArray or an NSArray? You have declared the variable as being of type NSMutableArray*, but the expression on the right creates an NSArray. If you want this array to be mutable, change the receiver of arrayWithObjects: to be NSMutableArray; if not, change the declaration to correctly identify this as an NSArray instead of an NSMutableArray.




回答2:


You can't cast an NSArray as an NSMutableArray. Just initialize it as an NSMutableArray

NSMutableArray *tbValueArray = [NSMutableArray arrayWithObjects: ... ]

Alternatively, if you don't actually need an NSMutableArray, change your variable type to NSArray*

NSArray *tbValueArray = [NSArray arrayWithObjects: ... ]



回答3:


Replace

NSMutableArray *tbValueArray = [NSArray arrayWithObjects:...];

by

NSArray *tbValueArray = [NSArray arrayWithObjects:...];


来源:https://stackoverflow.com/questions/19101395/incompatible-pointer-types-initializing-nsmutablearray-with-an-expression-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!