0

I have a NSMutableArray and I need to sort its elements into separate C char.

How to accomplish that? Or is it better to put the elements into NSStrings?

I've tried this, and it crashes when I try to log the result:

NSMutableArray *array = [[NSMutableArray alloc] init];

... do something to fill the array ...

NSString *string;

string = [array objectAtIndex:0];

NSLog(@"String: %@", string);

*I really prefer putting the elements of the array into C char, because I already have some woking code using char instead of NSStrin*g.

Thanks!

1
  • Please explain, so is array full of NSStrings and you want an array of const char * to sort, or do you actually want a single C char from the strings in your array or what? Commented Nov 2, 2011 at 20:19

1 Answer 1

1

Dont see any specific reason to convert NSString to C chars. To sort an array full of NSStrings try this method -

NSMutableArray *array = [[NSMutableArray alloc] init];
sortedArray = [array sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *string = [sortedArray objectAtIndex:0];
NSLog(@"String: %@", string);
Sign up to request clarification or add additional context in comments.

3 Comments

I receive the warning "Semantic Issue: Incompatible pointer types assigning to 'NSMutableArray *' from 'NSArray *'"
@Winston both arrays sortedArray & array have to be of the same type NSMutableArray or NSArray. Change that & i dont see a reason for this to not work...
I verified that and I only have NSMutableArray. When I "Jump to Definition" on xCode for sortedArrayUsingSelector I get: "- (NSArray *)sortedArrayUsingSelector:(SEL)comparator;"

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.