Last active
March 8, 2017 05:04
-
-
Save rnagella/a71f7c7f320edcd28e51 to your computer and use it in GitHub Desktop.
Example of how to convert swift String to Double
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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" |
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
Thats helpfull , thanks.