Skip to content

Instantly share code, notes, and snippets.

@rnagella
Last active March 8, 2017 05:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnagella/a71f7c7f320edcd28e51 to your computer and use it in GitHub Desktop.
Save rnagella/a71f7c7f320edcd28e51 to your computer and use it in GitHub Desktop.
Example of how to convert swift String to Double
//
// ViewController.swift
// Area Calculator
//
// Created by Ranjith Nagella on 10/7/14.
// Copyright (c) 2014 Ranjith Nagella. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
// Input
@IBOutlet weak var widthTextField: UITextField!
@IBOutlet weak var heightTextField: UITextField!
//Output
@IBOutlet weak var areaOutputLabel: UILabel!
@IBOutlet weak var permiterOutputLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Actions
@IBAction func buttonPressed(sender: AnyObject) {
println("Button pressed.");
// String to integer conversion
// let width = widthTextField.text.toInt()!,
// height = heightTextField.text.toInt()!
// String to double conversion
let width = NSString(string: widthTextField.text).doubleValue
let height = NSString(string: heightTextField.text).doubleValue
// area
let area = width * height
println("Area for W \(width) and H \(height) is \(area)")
areaOutputLabel.text = "\(area)"
// perimter
let perimter = 2 * (height + width)
println("Perimter of W \(width) and H \(height) is \(perimter)")
permiterOutputLabel.text = "\(perimter)"
}
}
// Int
/* Integers are number ranges from -Ve to +ve i.e, -2,147,483,648 to 2,147,483,647
*/
// let thisYear: Int = 2014
// String
/* String is a combination of characters 1 or more.
*/
// let myNextBirthdayIs: String = "April 17th 1990"
@Guvargam
Copy link

Guvargam commented May 2, 2015

Thats helpfull , thanks.

@pcora
Copy link

pcora commented Feb 25, 2016

Thanks for sharing! It's really helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment