Operator Overloading & Custom Operators in Swift

Operator Overloading
Every once in a while we will end up in situations where we would need the functionality of existing operators e.g. +, -, *, /, etc for our own classes or structures. Lets say you were creating a game called “Cube Collector”, wherein, you start with one small cube in a universe filled with cubes of all sizes. You have to look for new cubes and every time you find a cube, your cube size is increased by adding the newly found cube to existing cube.

Our cube definition could be

Continue reading…

Understanding Enums using Swift

Enums are a way to represent a “type” of values which are all related together. Lets say you had an application called MyFamily, which showed you different members in your family and their relationship to you every time you opened the application. Now, the relationship of every member in your family to you could only be one of the few values. This relationship could be represented by a “FamilyRelationshipType” using an enum as follows. FamilyRelationshipType is essentially a new data type.

Continue reading…

Swift – SequenceType & GeneratorType

We are quite used to looping through collections such as arrays / dictionaries using for-in

For e.g.

Now, lets say the details of library are encapsulated. i.e. The UI layer will receive a library object from the data layer and, all we know is that the library object is a collection that supports for-in looping just like an array.

Continue reading…

Objective-C Copy vs Strong

I was recently working on putting together iOS code review guidelines with my colleagues at work. One of the guidelines I had proposed was  –  “Use copy instead of strong when declaring properties of objects, which have mutable variants e.g. NSString, NSArray, etc”. A colleague expressed concerns that using copy would be a hit on the performance.

In my view, one of the main reasons to use copy attribute was to preserve encapsulation. But, I was curious about the performance concerns myself. All I remembered was reading that Apple optimizes the copy attribute for you but had no data. I put together a sample program and here are some of the results:

Continue reading…