Wednesday, December 31, 2014

Swift File Manipulation

When you start writing in a new language it is always an adventure.

When your code starts to work it can be thrilling and when you can't seem to get something to work it can be very frustrating.

But as you dig in deeper there are occasions when you try to do something and it works, but you're not sure why.

This can happen when you start following advice but use Cargo Cult logic to find the "right" syntax.

This is exactly what I have done so far with Swift.

I found that it was difficult to get the answer to the question: how do I read & write to a file in the UNIX file system?

It actually took a considerable amount of digging and experimentation to accomplish what I take for granted as a very common thing that I do with perl, Java or Python on a daily basis.

That all being said I actually got it working on a rudimentary level.  I can read and write to a specific file in a predetermined location.

But that's all that I understand so far.  I very much feel like a neolithic man poking an obelisk from space with a bone since there is a LOT going on here that I do not understand.

You will note for example the use of dirs and dirz (distinct var names) which I resorted to while debugging example code from someone else.

So I figured that I would share my source code since this is just something I have running in a Playground file:


// Playground - noun: a place where people can play

import Cocoa

let file = "test.txt"

let dirs: [String]? = 
    NSSearchPathForDirectoriesInDomains(
        NSSearchPathDirectory.DocumentDirectory, 
        NSSearchPathDomainMask.AllDomainsMask, 
        true) as? [String]

println(dirs)

if (dirs != nil) {
    
    let directories:[String] = dirs!

    let dirz = directories[0]
    
    let path = dirz.stringByAppendingPathComponent(file)
    
    println(path)
    
    println("I usually fiddle with this string to force a page recompile")
    
    //reading
    var error:NSError?
    
    //reading
    let readText = String(contentsOfFile: path, 
                          encoding:NSUTF8StringEncoding, 
                          error: &error)
    
    if let theError = error {
        print("\(theError.localizedDescription)")
    }
 
    println(readText)
    
    let text = readText! + 
               "this is added to the file every time we run\n"
    
    //writing
    text.writeToFile(path, 
                     atomically: false, 
                     encoding: NSUTF8StringEncoding, 
                     error: nil);
}

Tuesday, December 30, 2014

Swift Kick

I have been "tirekicking" Swift.

I have been using a loaner mac from work which somehow allowed me to download & install XCode without a Developer License.

So I have already written Swift code & created an OS/X application.  Whee!

That being said, I have begun to plumb the documentation & learn some syntax.

I hate to admit it but I'm starting to like it.

They have successfully lifted many of the best features of Python, Java and C#.  I actually find the -> notation used to denote a function's output somewhat elegant.

It tickles that literature-minded-Unix-geek nerve just the right way.

So I have XCode, which is a utlititarian bear, to build my apps for me & I have a REPL interface now, in the form of the "playground".

Once thing about playgrounds that it actually very nice is that you can save them as-is & share them with each other as documentation with the added ability to play with the examples right there in front of you.

I still have a LOT to learn - not the least of which is my way around the supporting libraries.

But one thing at a time...

Monday, December 29, 2014

Not Too Swift

I am an idiot.

After programming for >32 years I have become rigid in my pattern of thinking.

I had reservations about Objective C and I stupidly assumed that everyone else would too.

Now that's over.  So the dusty books I bought planning to learn that language will now be a Bridge Too Far and a bitter reminder of what might have been.

But here's the thing:

This Swift nonsense really rubs me the wrong way.  Apple is going out of their way to behave badly.  Just as .NET was only ever intended to run on the Windows platform Swift, by design, only runs on iOS and OS/X.  (And of those, only on the Post-Steve flavors)

Being a parent, I digest news at the Speed of Dark.  So it took me - what has it been, 6 months? - to even notice Swift.  By then all of the hype-cycle analysis should have boiled down to a more reasonable level... "should have"..

Just like everything else there is no online debate.  You are either a zealot or a hater.  No one has time for wishy-washy-fence-sitters.  In the public space of opinions you are All-In or you have no peer group.  Any sort of "debate" is really just the two main camps lobbing petrol-bomb-grade-hyperbole at each other.  There's nothing worthwhile to be learned.

So after having an even stronger visceral reaction than last time I asked myself if I was making the same mistake.  The honest answer I gave myself was "Probably".

So this presents a problem.  I don't have a new mac.  I don't have a new iPhone nor iPad.  All of my gear is roughly 4 years old & thus has fallen off of the supported platform.  The OS/X version that I run at home is four major revisions out of date.  (Because I got sick of chasing the upgrade dragon & froze everything in a working state)

So even if I wanted  to program in Swift - WTF am I going to use?  I would have to do what Apple wants you to do - go out & buy all new stuff...

So I'm learning Swift now - with nothing to run it on. 

And I'm looking into buying a new laptop...

Like I said, I am an idiot.