Tuesday, January 27, 2015

Alert Messages with Swift

Here is a quick look at using a modal alert window with swift for Mac OS X



The main window has a simple button action to present a basic modal alert with a text string.

the code to do this in swift is:

@IBAction func btnShowAlert(sender: AnyObject)
        {
            var myAlert:NSAlert = NSAlert()
            myAlert.messageText = "Alert Text Here."
            myAlert.runModal()

    }


Alerts can be more in depth than this. This was just the basics to show how easy they are to add and use in swift. Alerts can also sheet from the main window by using;

beginSheetModalForWindow()

Instead of using the runModal()


Happy Coding;)

Saturday, January 24, 2015

Sliders with Swift

Here a quick one how to display a value of a NSSlider with swift in a field.Here the running example:


To basically only get the value from the slider we only need to make an outlet for the text field and an action for the slider. ( However if you want to set the slider by code you also need to make it an outlet also.)

The Slider action is as follows:

// field Outlet
@IBOutlet weak var fldSliderValue: NSTextField!

// NSSlider Action
@IBAction func mySlider1(sender: AnyObject) {

// x holds the value as a double as the slider changes position
var x: Double = sender.doubleValue

// to get the value to display as text string in the field do this
var mystring = x.description

// display the value in the field
fldSliderValue.stringValue = mystring

 }

Below is the source view:


Swift for Mac OS X is really fun. Will be sharing more soon.  

Friday, January 23, 2015

Make OS X Web Browser with Swift

Make OS X Web Browser with Swift



The last swift example we did was using basic text strings. This next example takes what we learned using basic text string and shows how they can be used to make a very basic web browser. By taking basically the user inputed text we will load a web page. Also we will take a basic user field input and take that string and do a basic google web search. The video still stays in the realm of basic, however you can see how quickly the project could and should start using functions and be broken into separate sources files.




Swift for Mac OS X is really not very hard to pick up and makes in a good choice for those who came from Apple Script, Basic, Ruby, Javascript or a Small Talk language to pick up quickly as it is very similar to what you already know.

Swift makes you start  to see very fast why you should consider using it over a 3rd party tool, as it simple to learn and use. Best of all it has access to everything Mac or iOS and is free to use. (note for iOS the webview is done differently. It is kind of the same, however you need to use UIWebview instead. It is also more limited than the webview for Mac in what it can do.)

This video a little long at 30 minutes but covers a bunch. Will likely revisit this project in the future with more advance topics.

Here the video:

(It supports up to 1080p and may even work with the new 1440p Beta resolution.
It may take a while for youTube to Process higher video resolutions upto a hour from posting, I suggest 1080p or higher to read the sourcecode.)

And if you on a mobile device and want to view on the you-Tube app instead here the video link (video quality is better for mobile on youTube app) http://youtu.be/khhi52p9szQ




Thanks and happy coding ;)



Tuesday, January 20, 2015

Hello Mac From Swift

After my last post about using swift to make cocoa apps on Mac OS X, I got a few questions on how to make an outlet. I guess it is easier to see it happen. Also got questions on how to do window layout constrains with Xcode for Mac also.  As I said it is easier to see this than read about it, So I show you in this video with a very simple basic Cocoa app Hello Mac again with using swift.

There a million swift examples for iOS but not to many for Mac OS X cocoa based apps, so again I keep sharing more swift for Mac stuff in the future. As I am a more a Mac OS developer more than an iOS one.

Here the video; Enjoy.

Note: (If you do not see 1080p resolution then wait for YouTube to process the higher res version. should be not to long)

 

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.

Friday, January 2, 2015

Happy New Year

Happy New Year

When I first started this blog part of posterous.com over 6 years now, (moved it to blogger 3 years ago) we meet some pretty cool people. Between my video collections at Vimeo, Screencast.com and YouTube we made over 100 free videos for just learning something cool to do with programing. Some of these videos are now outdated, as the technology world changes every second, (ok it seems like it does, every 6 to 14 months) and that causes some of this stuff to no longer work :(

This blog went from 583 unique visitors a month from 2013 to 8,704 in 2014. Wow! I almost feel I do not give that much content to deserve those numbers. Thank you.

We always try to make more content, but we do not get paid for this blog. We do it when we are in-between programming and multi- media work and jobs. If its slow we give more content, and if busy it will be less.

We also tell it like it is, no candy coating here, not many fancy edits. Why? because we are real people, only fake people edit out there flaws.

Thanks everyone for watching in 2014 and for supporting my little corner of the internet. Hope to see you here again in 2015.

Your friend,

Obleo ;) Happy Coding.