0

I have an array it looks like

NSArray *array=@[@"apple",@"animal",@"ant",@"beat",@"bean".....];

I need to split into multiple arrays alphabetically

I am adding the piece of code what I tried

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH 'a'"];
  NSArray *aElements = [arrLastName filterUsingPredicate:predicate];

NSPredicate works good but at second line am getting an error I dont need to solve this error . only thing is reformation the array into multiple arrays alphabatecilly

 Initializing 'NSArray *__strong' with an expression of incompatible type 'void'
2
  • What error do you get? Commented Feb 24, 2015 at 11:00
  • Initializing 'NSArray *__strong' with an expression of incompatible type 'void' Commented Feb 24, 2015 at 11:01

3 Answers 3

2

You have confused the NSMutableArray method filterUsingPredicate with the NSArray method filteredArrayUsingPredicate. The former modifies the receiver NSMutableArray and returns void. The latter leaves the original array unchanged and returns a new array.

So what you wanted is -

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH 'a'"];
NSArray *aElements = [arrLastName filteredArrayUsingPredicate:predicate];

However this approach would require you to iterate the array multiple times. I would use the following approach -

 NSMutableDictionary *alphaArrays=[NSMutableDictionary new];
 for (NSString *word in arrLastName) {
    NSString *firstletter=[word substringToIndex:1];
    NSMutableArray *wordArray=alphaArrays[firstletter];
    if (wordArray == nil) {
       wordArray=[NSMutableArray new];
       alphaArrays[firstletter]=wordArray;
    }
    [wordArray addObject:word];
 }
Sign up to request clarification or add additional context in comments.

6 Comments

It may be more efficient, however to simply iterate the array once, adding the elements to 1 of 26 mutable arrays as you go
error solved but primary thing is i need to split into another array alphabetically
Yes, using this technique you will need to create 26 predicates and each predicate will iterate the array.
yes but the array elements not fetching into new array , i am getting empty array
its adding all elements into array @paulw
|
1

The issue is with filterUsingPredicate: it filters the NSMutableArray itself and it won't return anything (return type is void) .

You need to use filteredArrayUsingPredicate: (It returns a filtered NSArray)

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH 'a'"];
NSArray *aElements     = [arrLastName filteredArrayUsingPredicate:predicate];

Check the NSMutableArray Class reference, you can see the methods declared like:

- (void)filterUsingPredicate:(NSPredicate *)predicate

- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate

2 Comments

thanks for the answer @midhun , your answer solved the error but still array elements not fetching into new array
@Coolcracker: Check the predicate and also check the arrLastName contains any data. In your code you are populating array and using arrLastName.
1

Using this library , it would be easy to solve LinqToObjectiveC https://github.com/ColinEberhardt/LinqToObjectiveC

This library has the following method groupBy https://github.com/ColinEberhardt/LinqToObjectiveC#groupBy

   NSArray* input = @[@"James", @"Jim", @"Bob"];

   NSDictionary* groupedByFirstLetter = [input linq_groupBy:^id(id name) {
           return [name substringToIndex:1];
    }];

 // the returned dictionary is as follows:
has two keys "J" and "B" and corresponding arrays as values
 // {
   //     J = ("James", "Jim"); - First Array 
   //     B = ("Bob"); - Second Array
 // }

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.