Swift Programming – Simple Currency Converter

Programming in Swift

What is Swift?

  • New programming language for Mac OSX App and iOS Apps
  • Is nicely integrated with Cocoa and Cocoa Touch
  • Uses ARC for memory management
  • Has the power of Objective-C’s dynamic object model
  • Extensively uses constants to make code safer and cleaner.
  • Has datatypes which were not present in Objective C (e.g tuples that allows to create and pass around grouping of values)
  • One strikingly different thing about swift is, it does not use semicolon (;) after each statement. Semicolons are however required if you are writing multiple separate statements in a single line
  • Has easy learning curve and is considered to be modern , powerful and easy to use.

Necessary Setup for Swift Programming

  1. Mac OSX upgrade to 10.9.3
  2. Xcode 6.0 Beta Version

Thats it, we are all setup to get going

For this sample application we are going to do the following.

  1. Create a new Xcode project using Xcode 6.0 and select Cocoa Application and Programming language as Swift.
  2. When the project is created the following files are automatically created for you
    1. AppDelegate.swift
    2. MainMenu.xib
  3. We are going to modify the main menu by adding a couple of text fields(a from currency field, a conversion rate field, a result text field and a NSButton for calculate.



Sample Code 1 (A simple currency converter)


//  AppDelegate.swift
//  SwiftCurrencyConverter
//  Created by Debasis Das on 6/2/14.
//  Copyright (c) 2014 KnowStack. All rights reserved.

import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
//Note: The way outlets are defined in Swift is different than Objective C Programming
    @IBOutlet var window: NSWindow
    @IBOutlet var fromCurrencyTextField: NSTextField
    @IBOutlet var conversionRateTextField: NSTextField
    @IBOutlet var resultTextField: NSTextField
    @IBOutlet var calculateButton: NSButton

    func applicationDidFinishLaunching(aNotification: NSNotification?) {
        // Insert code here to initialize your application
        // Logs can be printed using println or NSLog
          //This method is same as the  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
          //When the programming language is Cocoa
        println("Hello, world")
        println("This is the second log using println")
        NSLog("%@", "Hello World using NSLog");
        var aIntValue = 90
        var aFloatValue = 90.87
        NSLog("%d", aIntValue);
        NSLog("%f", aFloatValue);

    func applicationWillTerminate(aNotification: NSNotification?) {
        // Insert code here to tear down your application
        //The method is automatically added when the project is created.

   //Note the difference in method definition for an IBAction in Swift
    @IBAction func calculate(AnyObject) {
        println("button tapped!")
        var value = fromCurrencyTextField.intValue * conversionRateTextField.intValue
        resultTextField.stringValue = value.description;

We will be continuously updating this site with new code snippets for programming in Swift. Visit Back for learning more.

Posted in Cocoa, Objective C, Swift Tagged with: , , ,
4 comments on “Swift Programming – Simple Currency Converter
  1. Timothy says:

    Did you try storyboarding in XCode6? I cannot find how to make an outlet when using a storyboard.

    It creates a Window and a separate ViewController, where I can place controls. But where to link them to? Linking to ViewControl.swift will give errors.

    Thanks for your quick response to Swift introduction.

    • Debasis Das says:

      Try creating a simple Master Detail Application for Xcode 6 template
      and then in the the DetailViewController create a new label @IBOutlet var secondLabel: UILabel

      Then in the Main.storyboard you can drag from the DetailViewController to the label, I tried that and it works as expected.

  2. Angelo says:

    Hello, I tried to create a simple calculation, but I can not handle numbers with commas.

    @IBAction func ccdSelect(sender: AnyObject) {

    if ccdController.selectedSegmentIndex == 0 {
    textCCD.text = “4.8”

    if ccdController.selectedSegmentIndex == 1 {
    textCCD.text = “3.6”

    @IBAction func buttonCalcola(sender: AnyObject) {

    var a = (textDistanza.text as NSString).floatValue
    var b = (textCCD.text as NSString).floatValue

    var molt = a * b

    labelRisultato.text = String(molt)


    • Debasis Das says:

      The following code works for me, Please try this and let me know if it works for you
      class ViewController: UIViewController

      @IBOutlet var fromCurrencyTextField: UITextField
      @IBOutlet var conversionRateTextField: UITextField
      @IBOutlet var resultTextField: UITextField

      override func viewDidLoad() {
      self.fromCurrencyTextField.text = “300.6”
      self.conversionRateTextField.text = “54.63”

      // Do any additional setup after loading the view, typically from a nib.

      override func didReceiveMemoryWarning() {
      // Dispose of any resources that can be recreated.

      @IBAction func calculate(AnyObject) {
      println(“button tapped!”)
      var value = (fromCurrencyTextField.text as NSString).floatValue * (conversionRateTextField.text as NSString).floatValue
      resultTextField.text = value.description;

1 Pings/Trackbacks for "Swift Programming – Simple Currency Converter"
  1. […] (Vanila – Sample Cocoa Application using Swift) […]

Leave a Reply to Angelo Cancel reply

Your email address will not be published. Required fields are marked *