Monday, January 5, 2015

!2Swift:Of lumpy carpets & loose wires...

Welcome to the newly branded !2Swift (not-too-swift) branch of my blog.

Since I have been talking about just about nothing else besides Swift for days on end it seems fitting that I make a *thing* out of it...

But let's be real now for a moment: I am neither a zealot/fanboi nor am I a hater/assassin.  

I am a Skeptical Software Developer.

It is difficult to keep your politics out of your code these days as we have to endure endless wrangling over licenses.  To be politically neutral is to be intentionally clueless.

I am therefore going out of my way to behave in the way that I have seen many old people use to good effect:  I am filtering out things-I-don't-want-to-hear and only focusing on the interesting parts.


"Adapt what is useful, reject what is useless, and add what is specifically your own" - Bruce Lee

But that also means that I sometimes, just out of pragmatism, I will have to point to things & say "WTF?"

One of those things is the Coccoa framework.  There was a serious WTF moment that I experienced when switching gears from trying to learn Swift to finding the state of affairs that is the Core Libraries we need to depend upon in order to write *Everything*.

 I will provide further examples in coming posts, but looking below to my cargo cult posting about accessing files - you can see some hints there to the wires hanging out of the back and they are all of the ObjectiveC variety. (Anything with the 'NS' prefix for example)

You will come up against this every time you cross the language barrier in terms of features.  For example, in Using Swift with Coccoa and Objective-C in the section on Working with nil it points out that ObjC works with raw pointers that could be NULL.  Swift however guarantees all values are non-nil.  Thus Swift wraps the class-types in imported ObjC API's in an implicitly unwrapped optional type.  So before you use an ObjC object you need to check that it is not missing.

In their example when importing this ObjectiveC method:

- (NSDate *)dueDateForProject:(Project *)project;

You have to do this in Swift:

func dueDateForProject(project: Project!) -> NSDate! 

So you might be thinking either:
  1. WTF? ::facepalm::
  2. What's the big deal?
And to you in the second group I will point out this simple truth:
Everything that you want to do requires that you import ObjectiveC code.


[EDIT: It was pointed out that I should explain my point a little further if I am not going to force people to Do The Work to get it...]


The Big Deal is the '!' at the end.  Whenever you use them you are essentially bypassing the safety built into the language.  This is a crash waiting to happen every time that you do it & these frameworks require that we do it all over the place.

No comments:

Post a Comment