Scroll down for latest blog entries.

Exploring Swift: Variables and Constants

LinkedPermalink

In this series of post I'll try to cover Swift in detailed manner to help myself, and hopefully you too, learn Swift in detail. Swift does has good language guide published by Apple which is available on iBooks Store, but given the nature of being such expressive language, there are tons of things which are not apparent from the first glance and really obscure in when things get complicated.

I will not focus on Cocoa and Cocoa Touch in few upcoming posts. This is not "Getting Started with iOS Development" kind of posts. I will focus on language itself. I'll try cover interoperability of existing frameworks, Objective-C code and the Swift later.

So, without further ado, let's get started:

Variables and constants

Variables and constants seem to be very straight forward in Swift. Both have to declared before use using var and let keywords respectively. Constants cannot be changed in future execution of program and must me initialized with its value immediately.

let numberOfMaximumCarsInParking = 1000 // set or get compile error

Variables can change during program and could be declared without initialization, albeit using them in expressions will throw compile error. Uninitialized variables must have type annotation. This is only case (I couldn't find another yet) where type annotation is required throwout the language. If you provide initial value to variable, there is no need to annotation it.

var numberOfFreeSpacesInParking = 123

var carParkingName:String; // this is fine, but type has to be annotated

carParkingName = "Underground on Some Street"

// Comment line above and you'll get compile error in if statement
// because you can not use uninitialised variable
if carParkingName == "Underground on Some Street" {
    "You are here"
}

For compactness you can declare multiple variables and multiple constants in a single line, separated by comas.

var x = 1, y = 2, z = "This is string" // Works for constants too

Redeclaring of variable and constants with same name is prohibited, even if you change store value.

Swift seems to be proud to allow you using any Unicode characters as a variable name. So proud in fact that guide even shows how to use Emoji as an example of variable name. Please don't do this, or you'll end up with something that looks like this.

let 🍆 = "fisteggplant"

Value Types

There are two types of containing values in Swift: value types and reference types. A value type is a type that is copied when it is assigned to a variable or constant, or when it is passed to a function. Structures and enumerations (enums) are value types. As you might already guessed, classes are reference types.

Here is an example of how different value types behave when passed to array: (Same will apply to any expression.)

// For Value Types
let numbers = [x,y]         // [1,2] as constant
x = 5
numbers                     // again [1,2] and no error
numbers[0] = 5              // this will work
numbers                     // [5,2]

// For reference types
class MyNumber {
    init (value:Int = 0){
        self.valueNumber = value
    }
    var valueNumber:Int;
    var value:Int {
        get {
            return valueNumber
        }
        set {
            valueNumber = newValue
        }
    }
}

let a = MyNumber(value:4)
var b = MyNumber(value:7)

let myNumbres = [a,b]       // [4,7] as constant
a.value = 7                 // a is instance, thus reference type
myNumbres                   // [7,7] 

What about primitives like Int or Bool? In Swift, there are no primitives! That's whole beauty and point of Swift. What looks like a primitive for you, is actually a stuct, thus are value types.

Note that String is also struct, and passed by value.

Mutability

There are no mutable and immutable breeds of same class in Swift as in Objective-C. You specify mutability by declaration. Everything declared with var is considered mutable and by let—immutable.

But, if you paid close attention to example code above, you might notice odd behaviour from Array which was declared as constant (read: immutable), but was clearly mutable.

let numbers = [x,y]         // [1,2] as constant
numbers[0] = 5              // this works fine, but why?

let is overloaded to dictate immutability for value types, not reference types. So changing mutable instance property of class which is referenced by immutable array seems obvious now. Problem is that, array in Swift is declared as a structure.

From my observation, reference you hold on Swift array, is reference to the buffer in memory and by using let you make that reference immutable. Because arrays in Swift buffer look fairly simple [length,capacity,item1,item2,...] you can exchange items with same type/capacity but cannot change length, thus making it impossible to append, pop and so on. Declaring array as a variable will "unlock" those functionalities.

Apple strongly recommends to use constants in cases where variables appear to not change its lifecycle.

I'll cover more on arrays and dictionary in future posts.

Printing Values

println(value:String) is new NSLog(). It will print out in console, both when working with Playgrounds (located in Assistant Editor) or in Xcode project.

Swift support String interpolation, thus println() can use it as its advantage and printout any constant, variable or another long string as a part of its string like so:

println("Currently \(carParkingName) has \(numberOfFreeSpacesInParking) free parking places")

Comments

Few word on comments. They are as you'd expect them too be with one neat addition. In swift you can nest /* ... */ multiline comments. Yay!

/* This is multiline comment

    /* So this is, but interesting part is one line below */

    This line, would be illegal in some languages,
    but in Swift also belongs to comment scope above.
*/

let isSwiftMultilineCommentAwesome = true

That's it for now. I will update this post if I find more details on obscure behaviours or find more relevant information about constants and variables.

You can download playground with sample code above from GitHub.

Reading Quickly

LinkedPermalink

Since I remember myself I had a dream to read quickly. I wanted to know everything in 300 page book by hitting it to my head. Of course that didn't work.

Today, being grown-up, I still have that very same dream to transfer information from the book to brain in relatively short period of time.

Problem I face is my reading pace. No matter how much I try and read, I do it in a very moderate rate. Thus, as one of my "2014 Todo List" goals I decided to learn how to read quickly.


In late October I was listening to one of the episodes of Iterate podcast where Eric Mayville of Wondersauce was talking about challenges in making of Read Quick app. Which as you might already guessed—is an app that "teaches" you how to read quickly. So I bought it. In fact I bought almost every app that claimed to make me read quicker. There are quite a few apps of this type, but only two remained on my phone—Velocity and Read Quick.

Both of them are simple, which is crucial for me, and both of them can read from Instapaper, which is a must. Conceptually both are identical: They display single word at a time with a predefined rate (measured in words per minute). At the fist time choosing from two seemed to be easy. Velocity has nicer and simpler UI, but devil is in details. Read Quick does everything with more of a detail oriented fashion. For example, Read Quick will pause for a fraction of second if displayed word is too long and repeat last five word if you swipe right in case you lost your attention for a second. This seems to be nothing special but makes reading very comfortable. On the other hand Velocity UI is cleaner, simpler and has icon I like being on my home screen. Plus, it can archive Instapaper article right from the app itself. It seems like Velocity was made by people who care about UI and Read Quick—who care about reading. Thus, my choice will got to Read Quick but ideally, I would prefer those two to merge somehow in an app of my dream.

That's all good and fine, but do I read faster? Yes. I started at 250 words per minute (which was not hard) and now read at 305 WPM. As a result I can read 1500 word essay in under 5 minutes... only if a keep my mouth shut.

The 2014 List

LinkedPermalink

2014 promises to be very interesting year in my life. For several reasons I would call it a life-changing for both, me and my family. This November my wife got government grant to continue education in Japan. News pushed me to “go indie” even further and I made decision to quit Sixty Three Bits by the end of the year and thus, start working independently. This should make me much stronger as a professional and person. Not being “chained” to one workplace is also bonus I’ve been looking for.

I’ll write about my Japanese adventures later in 2014. Meanwhile here is small to-do list for upcoming year.

Personal Life

  • Successfully move to Japan.
  • Do not stop eating healthy food.
  • Finish job of getting myself proper body.
  • Take sports-climbing seriously.
  • Start Automotive podcast I always wanted to record.
  • Learn reading very, extremely fast.
  • Learn electronics and embedded programing.

Carrier & Skills

  • Learn OpenCL and DSP/image processing in-depth.
  • Learn about UI animation in-depth.
  • Launch my camera iPhone app.
  • Start working on health/fitness app.
  • Assemble standing desk workplace.

This is my to-do list for 2014. I will update this post during this year and write about new, finished or “work-in-progress” subjects regarding subjects above.

I won’t share some personal plans here right now. Just to spice everything up for myself.

Have a good, healthy and lucky year!

JayBird BlueBuds X

LinkedPermalink

Bluetooth headphones have been something I never enjoyed. Most of them are too big or have too many wires, which spoils the whole concept of being free and wireless.

BlueBuds X was purely an impulse purchase. They look unusually small for wireless headphones and price seems to be very good as well. So I got one.

JayBird BlueBuds X

BlueBuds are my first in-ear headphones in many years. Last time I used in-ear plugs was in my teenage, and as a teenager I loved deep, ballsy sound which apparently sacrificed clarity in most cheap in-ear headphones. BlueBuds don’t do that. They are clear and flat. Now, being a bit older, I appreciate flatness of frequency response. This means you are listening to whatever band and/or sound-editor wanted to.

Being a Bluetooth headphones, BlueBuds require power and thus battery, which is magically hidden in left or right plug without being bulky. It’s that discreet. Right plug also has some sort of mini-USB hidden under a cap for battery charging. Battery last whole day without a problem. Bad thing is you have to charge one more thing at night, which will always annoy me until someone will invent ever-lasting batteries.

Fit is good, but I personally, still feel more comfortable with regular headphones rather than in-ears. Maybe it’s just matter of time.

Rubber “wings” really do help when you are jogging or training in gym. BlueBuds come with three size of those “wings” and three rubber plugs for in-ear part as well. I use medium size on both.

BlueBuds X also have three button controller with built-in microphone under right ear-plug. Microphone is loud and a bit noisy in streets. Some controls do not match Apple EarPods, which for me, is a bit irritating. For example: double tap on center button redials last number, rather than skips to next song.

Overall, JayBird BlueBuds X, are great bluetooth headphones for sport or indeed any situation. They last long, sound good, fit well and do not look like Robocop’s appliance on your head.

If only they could call it Jaybird Bluebuds X, not that camel-case nonsense.

“As Little Design as Possible”

LinkedPermalink

This post contains strong language.

Design solves problems, in this case delivers information to the reader. No extras are necessary.

What I'm saying is that all the problems we have with websites are ones we create ourselves.

True. But we often overcomplicate things to make ourselves stand-out in a crowd. Including me.

That’s why I simplified design of this blog even more. Hope you like it, if not I don’t care because:

Shit’s legible and gets your fucking point across.

Check-out the web-site, it’s hilarious.

iPhone 5s

LinkedPermalink

It's that year when Apple releases its S version of iPhone. Cosmetically nearly identical but internally pumped-up, iPhone 5s. As in my iOS 7 review I will not cover this phone as many already did month before. What I will do is share my personal feelings and thoughts about 5s.

Touch ID is new toy for mobile industry. It’s fast and reliable. Or is it?

Things is that, previous generations of fingerprint sensors we had in notebooks were quite literally unusable. Touch ID, on the other hand, seems to work like magic. It's much, much faster and actually does the job of identifying your finger. Having said that, there are turtles that are much, much faster that those stupid fingerprint sensors in old laptops. I disabled Touch ID the day I got the device. Swiping home screen away is still faster and easier (I almost always unlock phone using sleep/wake button). However, I will use it on conferences, in classroom and on travel trips, which I never did before.

Highlight in new camera system for me is lack of shiny basel and nearly equal margins from the edge of the phone. Five’s camera alignment always made me feel that cliche “Steve wouldn’t approve this” feeling (which apparently he did).

Dual LED panels is what videographers have been using for years to balance white in complex lighting situations. It’s hard to believe nobody had thought using similar as a flash before Apple. Flash photos from 5s are way better than from 4S or 5 but still look bad for my liking. My beloved Fuji X100s is only camera I trust to fire built-in flash.

I always believed in Apple products being better in white. I was right before original iPhone came out. I loved it and since than there was no iPhone to match original one (iPhone 4 and 4S were very close though). 5s in “Space Grey” reminds me of a classic iPhone. Especially from the back side. It’s amazing how settle change in color and finish can transform square hokey puck into beautiful device. iPhone 5s is first “dark” version of iPhone I like.

Camera and overall device performance is over-the-top. Yes, camera sensors always can get better and bigger, but A7 chip is something to behold of. For the first time it really is desktop class system on a chip with all the performance you’ll ever need in your pocket. iPhone 5s will be huge pain for Apple in few years simply because it won’t slow down as previous generation phones did over the time. A7 is that good. Apple admits it by putting A7s in iPad Air and retina iPad mini without a single change. It seems A7 even could power Macbook Air if it made any sense.

If you are really interested in new A7 I’d highly recommend reading following:

iPhone still remains best phone money can buy; even with iOS 7 which in my opinion is great step forward, but clearly not finished. Knowing Apple, I’m sure they will polish everything even before iOS 8. Device itself is flawless in terms of fit and finish, but you already knew that.

Tim Cook, Apple October 22 iPad Event:

We have a very clear direction and very ambitious goal.

What makes me really glad is that Apple is not slowing down as some could think. Every new product is clearly step forward. And, that’s good.

Geyikbayiri, Turkey

LinkedPermalink

Turkey is a neighbor country of my homeland and getting there is very easy since there is no ridiculously overcomplicated visa system. You can cross border anytime for free.

Turkey hosts millions and millions of tourists per year, especially popular among Russians and Ukrainians who like bright sun and amazingly transparent mediterranean sea. But that’s not what me and my wife are interested in. What we really wanted to do is sports-climbing.

There is small village in Turkey called Geyikbayiri. Located 40km from Antalya airport, it’s almost abandoned place with no tourists attractions at all. What Geyikbayiri is famous for is its wast rocks with almost 600 routes for sports climbing grading from 5A to 8C+ (that’s 5.7 to 5.14c in YDS).

Finding accommodation is easy. Half of dozen camping and bungalow equipped places are all over the place. I found Climbers Garden to be best located. Besides good location, place has lemon and pomegranate tree garden which looks almost as eden garden.

Some great mountains and fellow climber Englishman indicate that traditional climbing seems to be also popular in Geyikbayiri.

There is not much to do in Geyikbayiri besides climbing though. You can hike, but only places I enjoyed are tracks leading to climbing sectors.

Climbing routes are mostly overhang, which was an issue for me as I’m not nearly as good as Tamuna. Even fives seemed to be way harder than in Olympos. Tamuna confirmed that for six and seven grades.

To sum up, Geyikbayiri is a great place to visit if you are even slightly into climbing. We’ve climbed 33 routes in total from 5C to 7B+ in eleven days. Rest days can be spent in Antalya with ease if you are comfortable with hitchhiking.

Thanks Turkey for second great summer-fall vacation.

NSHipster Book

LinkedPermalink

You’ve read the site. Now buy the book.

Like resources like this? Support them.

Jonathan Penn, Cocoa Manifest

Mattt Thompson is one of the most fascinating person in our industry. His projects, AFNetworking, Helios and NSHipster are priceless for somebody who’s into iOS and/or Mac development.

I belive everybody who is reading NSHisper regullary should go and pre-order Mattt’s new book with “over a dozen new essays” combined with ones from nshipster.com.

Serously, pre-order one now. It’s $19. Such things deserve support.

Archive of Posts and Links