SOLID Swift, Dependency Inversion Principle

23 December 2015

This is he first article in a series about applying the SOLID principles in Swift, mainly for the iOS and Mac platforms. I will not start with the S, but with the D — The Dependency Inversion Principle, DIP

What does the DIP state?

High-level modules should not depend on low-level modules. Both should depend on abstractions.

Abstractions should not depend on details. Details should depend on abstractions.

read more

Contract Based Development in Swift

30 May 2015

Recently I started working an existing Swift project.

One task I was asked was to gain higher decoupling. The project had a high coupling mainly through the excessive use of singletons — they were everywhere:

  • Model layer: User.current()
  • Networking: APIManager.sharedManager()
  • extra methods in the AppDelegate

read more

Implementing Comparator Sorting with QuickSort Algorithm

05 February 2015

This shows how to implement a block based sorting with the quicksort algorithm.

in response to this Stackoverflow question

Example Usage

#import <Foundation/Foundation.h>
#import "NSArray+Comparator"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSArray *array = @[@4, @9, @2, @1, @7];
        array = [array vs_sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
        {
            return  [obj1 compare:obj2];
        }];
    }
    return 0;
}

Results in

( 1, 2, 4, 7, 9 )

read more

Once and For All — Data Source Adapter Pattern

28 January 2015

In iOS and Mac development a plague is spreading around the world.
Many view controller become so heavy and obese that we must expect our devices to suffer from Type-II Diabetes. This disease is super-contangeous and it's ground zero is well known: developer.apple.com

What are the symptoms of this syndrome?

Among others others they are the usual suspects when it come to obisity:

  • hard to move
  • unflexibel
  • hard to breed
  • no sex appeal

Where and how did the current outbreak start?

It is not known by now where the gems that transport this disease came to existence, but once they reached developer.apple.com, it broke loose.
Since than it is spreading through the main transport infrastructure like Apple's sample codes, mailinglists, stackoverflow.com, Books,…

read more

Variadic Lists and Blocks

24 October 2011

Matt Gallagher posted an great article about veriadic lists. In his conclusion he writes

In your own code, variadic methods should be used sparingly — passing variables in an NSArray or NSDictionary is safer (if slightly slower and syntactically more verbose) due to the fact that these classes do offer introspection.
And I think he is right. But now I found a situation, where it makes much more sense to have a veridic list of arguments rather a NSArray or NSDictionary.

I wrote a Category method on NSArray, that allows the user, to filter the array by blocks and create a dictionary with given keys.

- (NSDictionary *) dictionaryByFilteringWithBlocks:(NSArray *)filterBlocks
                                           forKeys:(NSArray *)keys

read more

Beat This, Python!

09 October 2011

I love Python. I love Objective-C.

"Huh?!" I hear you thinking, "they are complete different, how can you like both"?

Well, nothing is perfect, even in a lovely relationship. There are things, that drive me crazy in one — and things that are missing, form my point of view, in the other language

One thing I'd love to see in Python, that Objective-C has, is extending a class — Categories. Every language should have functional tools like pythons List Comprehensions, but Objective-C hasn't.

But since Apple extended Objective-C by introducing Blocks to the underlaying C, it is now possible to have something similar to List Comprehensions — and this article is about how. When people ask me, why I am such a big fan of Python, I usually refer to a sample Quicksort algorithm using List Comprehensions. here we go:

def quicksort(alist):
  if len(alist) <= 1: return alist
    pivotelement = alist.pop()
    left  = [element for element in alist if element < pivotelement]
    right = [element for element in alist if element >= pivotelement]
  return quicksort(left) + [pivotelement] + quicksort(right)

read more