Sunday, 7 September 2014

Protocols and Functions

Protocols, Delegates and Functions.

I've been browsing through the Apple Swift resources and stumbled upon a pretty good example on Document Management (some good cloud usage samples as well). I'm working quite a bit with 'Document' files both on iOS and OS X and wondered whether the samples provided could be extended into an application framework shared across a suite of products (you should only write code once). You'll find the Apple resources here, well worth a browse around.

Lister Example

I'll just put the playground code up here showing results of my research - I think it's fairly self explanatory.

I think it leads to more concise code personally - I'd appreciate your thoughts.

Tuesday, 17 June 2014

Slight Detour into Custom Sequences

Still trying to work my way around the Swift collection hierarchy with a view to working out mapping functions which work across all collection types (examples so far are Array only) - must say not the easiest thing to get your head around.

In doing my investigations however made a slight detour into understanding how it's possible to extend the collection (Sequence being the root) hierarchy and have your custom collections available to the for value in mysequence enumeration operations.

A very simple, but working, example is shown below which simply takes and array of integers and enumerates over them returning a tuple of the value and the value * 2.

I do have to say the string interpolation feature of Swift is really nice - even type checks (correctly in the main!)

Saturday, 14 June 2014

Pipe and Chains!

In the post written so far I've touched upon implementing the (|>) pipe forward operator and various of the traditional functional (in so far as Swift allows) mapping and folding options but haven't really touched upon why these can be useful in the software development process.

The code sample below shows how, when combined, they provide a powerful mechanism which can greatly reduce the amount of code a developer needs to write. As a side effect you should also note a distinct lack of more traditional control structures (condition,loops etc) and variable definitions.

The code is type checked by the compiler and handles, by using Swift option chaining, the optional elements within the model hierarchy.

Another helper function has been introduced here (getOrElse) which lets us provide default values for optional variables - in the cases here simply ensuring an empty array is returned as part of the search if any part of the model chaining is optional.