Saturday, January 17, 2015

Using Swift for Mac OS X apps

Yesterday I ran into an issue out of my control with a 3rd party cross platform tool I use. The issue was something that may end my development with it for Mac OS X. Now I not 100% sure if I will not find a work around with that tool. However it is very clear that I need to stop putting all my eggs into one basket and make a real move into using Apple's Xcode to build for Mac OS X in the future.

I already been learning swift for iOS, however when it comes to Mac OSX there very little info or example projects floating around. Even Apple developer site has only 3 swift examples for Mac OSX but have a ton for iOS. Mac and iOS are not the same.

I decided I going to do, if my schedule allows it one or more Mac swift project a day for the next 2 months and really learn how this is done. I have already meet a dozen other developers who want to learn swift for Mac also and are trying to find examples to.

That why I going to share much of what I learn each learning project I do here as, it may help some what.

I decided to skip the Hello world app and start with working with basic text strings and very basic button actions for my first mac demo.

Now first off for a Mac swift project there only one source file the AppDelegate.swift file that all your code goes in.

You still design the interface in Interface builder much like it was done with objective c. Just like with Objective-c each object needs to be assigned an outlet or a action however this is done in the same  AppDelegate.swift file.


This example will simply take the text from each 3 fields and add it together with the process button. And secondly with the clear button remove the text from all 4 fields.

The photo below shows the outlets for all 4 text fields. to create the outlet, first put Xcode into 2 views. the left one the interface view the right one the source code view for AppDelegate.swift. (If that file does not appear in the right side of the subview. At top left corner has 4 boxes like icon, click there to get a sub menu to switch the source code for the view.) To create the outlet simple select a field while holding down the control button and drag the string like graphic to the source code view as like the photo below. This will present you with a pop over window where you name the field (such as fldFieldName) that in turn makes an outlet. do this for each field. (below is an image of the added outlets, Note the window outlet was pre made by Xcode as was the functions for app launching and will terminate) 


Next Adding actions to the buttons. This is done similar to how you create a outlet however you need to add this code after the last function Xcode made but between the last "}" symbol. Also note when the pop over appear there a pop up menu if you select that, you can change the outlet to action. That what you want to do for the button, then assign it a name and hit return.

for my mini app I have the following:
 @IBAction func btnProcessName(sender: AnyObject) {
}

Now between the {} you add your code. Working with strings in Xcode is pretty simple and is similar to many other development languages. we take the object dot property such as:

@IBAction func btnProcessName(sender: AnyObject) {
        fldFullname.stringValue
    }

To assign as value to this fields string value (text) use the = symbol and basically just add the text together. This example does not current check if a field has no text however doing such would not be hard. 

The final action that makes a full name from 3 text fields for first, middle and last name looks like this:

 @IBAction func btnProcessName(sender: AnyObject) {
        fldFullname.stringValue = fldFirstName.stringValue + " " + fldMidName.stringValue + " " + fldLastName.stringValue   
    }
And last to clear all text from the text field as simple action very similar to many programming languages. 

 @IBAction func btnClearAllText(sender: AnyObject) {
        fldFirstName.stringValue = ""
        fldMidName.stringValue = ""
        fldLastName.stringValue = ""
        fldFullname.stringValue = ""
    }
And below are additional images of this very simple swift learning example for Mac OSX.

The full source code (and yes you can make your Xcode editor any color, like mine.)

And last an screen shot of the app test running (like my input;) )


I know I did not do a video, but my upstair neighbors are very loud with there yelling and screaming that a video would not work today. If you think a video would help I could do one in the future when they calm down upstairs.

I hope this helps some as it help me making it to learn myself.

Happy coding ;) thanks for visiting.

2 comments:

  1. Thanks for sharing this. I am living in Italiy and not found material that instructs for Mac OS X. I follow you, and I hope you can continue envi great job.

    ReplyDelete
  2. Thanks for sharing this post. Your post is really very helpful its students. iPhone online course

    ReplyDelete