1

I'm trying to read a file into array, by using mutable array in Objective C like this:

NSCharacterSet *newlineCharSet = [NSCharacterSet newlineCharacterSet];
NSMutableArray *myStr = [data componentSeparatedByCharactersInSet: newlineCharSet];

And this is the warning that I've got :

Incompatible pointer type initializing "NSMutableArray" with an expression of type "NSArray"

The reason why I used NSMutableArray is because later I'm going to convert all the elements inside the array to lowercase. Would it be possible to fix the warning and still satisfy the condition to use mutable array ? Thank you.

2 Answers 2

4

componentsSeparatedByCharactersInSet: returns an NSArray, not a mutable array. To satisfy the compiler (and actually provide you with a mutable array), simple create a mutable copy:

NSMutableArray *myStr = [[data componentsSeparatedByCharactersInSet:newlineCharSet] mutableCopy];
Sign up to request clarification or add additional context in comments.

Comments

3

componentSeparatedByCharactersInSet returns an NSArray object.. If you want an NSMutableArray Object instead than you need to create an NSMutableArray from it like this:

NSMutableArray *myStr = [NSMutableArray arrayWithArray:[data componentSeparatedByCharactersInSet: newlineCharSet]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.