Swift programming – Huh, No need to import .Swift Extension file

[wce_code id=”1″]

Many of you may have started to use the new Programming language Swift which Apple introduced in the WWDC 2014 Keynote.

It took many of us by surprise and for me a “oh here we go again, I now have to try and get my heard around another language”.

Apple explains that it should be easier to use and easy to pick up for new  comers.

 I suspect that it will  be as Apple says for many people, me included.

One thing so far that I like and there are many..

Is while trying out an Extension  (similar to an Objective-C category)  for the String class ( Original Extension code by drewag ).

I initially Declared the Extension in the original AppDelegate file that was created in my “Hello World” Project.

 But thought what if I want to  have many more Extensions and the like, this could get messy.

 


 

//  AppDelegate.swift

import Cocoa

extension String {

    func characterAtIndex(index: Int) -> Character? {

        var cur = 0

        for char in self {

            

            if cur == index {

                return char

            }

            cur++

        }

        return nil

    }

}

class AppDelegate: NSObject, NSApplicationDelegate {

                            

    @IBOutlet var window: NSWindow

    @IBOutlet var label: NSTextField

      @IBOutlet var label2: NSTextField

  @IBOutlet var label3: NSTextField

    

    func applicationDidFinishLaunching(aNotification: NSNotification?) {

 

         let string = “Dog!”

        

        let singleChar  = string.characterAtIndex(3)!  //– Get the nth character in string

  

        

    }

    

    

  …..

   

 

}


 

So I decided to move the Extension code to a new .swift file. And thinking I would get an error back in AppDelegate.swift file on the line –  let singleChar  = string.characterAtIndex(3)!   – because I had not imported the new file yet.

 


//  Extension.swift

//  Hello World

//

//  Created by Mark Hunte on 07/06/2014.

//  Copyright (c) 2014 mark hunte. All rights reserved.

//

extension String {

    func characterAtIndex(index: Int) -> Character? {

        var cur = 0

        for char in self {

            

            if cur == index {

                return char

            }

            cur++

        }

        return nil

    }

}


But nope, nada.

So the great thing is in this case at least I do not need to import the swift file. The complier does it all for you. Very nice.

 

This does not mean no importing is not done just it seems with swift files like this.

Mixing and matching between Objective-c and swift will require imports as well as importing frameworks and APIs

Added bonus

Originally I had misnamed the Extensions.swift file Extension.swift. If you have ever changed file names in a project you know you normally have to also change any reference to them. Not  hard but can be a pain sometimes. In this case you do not need to go looking for references to change. It is taken care of for you.

 

Please feel free to correct me on any of this as I as many am trying to gather my understanding. 🙂