Skip to main content

Development

Modern iOS app development part 2

All .net developers know that LINQ can be used to conveniently extract and process data from arrays, enumerable classes, XML documents, relational databases, and third-party data sources. These concepts are adopted by other programming languages such as Java, Javascript, Ruby etc.

ios-logo










var cityWiseSalary = from comp in ListCompany 
                     select new { 
                            comp.Name,
                            Emp = (from emp in comp.ListEmp 
                            group emp by emp.Address.City into CityWiseEmp
                            select new {
                                   State = CityWiseEmp.Key,
                                   TotalSalary = CityWiseEmp.Sum(emp => emp.salary) 
                            })
                     };

There is no reason that we can’t port it to Objective-C. By using LinqToObjectiveC, we could write Objective-C code like:

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

NSDictionary* dictionary = [input linq_toDictionaryWithKeySelector:^id(id item) {
    return [item substringToIndex:1];
} valueSelector:^id(id item) {
    return [item lowercaseString];
}];

// result:
// (
//    F = frank;
//    J = jim;
//    B = bob;
// )

Comparing to the old school KVC way:

NSArray *payees = [arrayOfTransactionsArrays valueForKeyPath:@"@distinctUnionOfArrays.payee"];

The API is not so intuitive, and not composable, is it?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories
Follow Us