This file contains hidden or 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
| import Foundation | |
| var iLow: Int = Int(round(1.1)) // -> 1 | |
| var iMid: Int = Int(round(1.5)) // -> 2 | |
| var iHigh: Int = Int(round(1.9)) // -> 2 |
This file contains hidden or 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
| import Foundation | |
| // Int to Double | |
| var i: Int = 1 | |
| var d: Double = Double(i) | |
| // Double to Int | |
| var i1: Int = Int(d) | |
| var i2: Int = Int(1.1) // -> 1 |
This file contains hidden or 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
| import Foundation | |
| let json = """ | |
| { | |
| "key1": "val1", | |
| "key2": "val2", | |
| "arr": [1, 2, 3] | |
| } | |
| """ |
This file contains hidden or 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
| import Foundation | |
| func convertArrayToXML(array: [Any], startElement: String) -> String { | |
| var xml = "" | |
| for value in array { | |
| if let value = value as? [String: Any] { | |
| xml += convertDictionaryToXML(dictionary: value, startElement: startElement, isFirstElement: false) | |
| } | |
| else if let value = value as? [Any] { | |
| xml += convertArrayToXML(array: value, startElement: startElement) |
This file contains hidden or 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
| import Foundation | |
| class ParserDelegate: NSObject, XMLParserDelegate { | |
| func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) { | |
| print("Found element \(elementName) with attributes \(attributeDict)") | |
| } | |
| } | |
| let delegate = ParserDelegate() | |
| let xml = "<parent><child attr=\"attr\" /></parent>" |
This file contains hidden or 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
| import Foundation | |
| let price = 1.5 | |
| let strPrice = String(format: "%.02f", price) |
This file contains hidden or 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
| import Foundation | |
| let num = 1 | |
| let str = "\(num)" | |
| let numAgain = Int(str) | |
| let dNum = 1.5 | |
| let dStr = "\(dNum)" | |
| let dNumAgain = Double(dStr) |
This file contains hidden or 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
| import Foundation | |
| let str = "Just a text string" | |
| let data = str.data(using: .utf8)! | |
| let str2 = String(decoding: data, as: UTF8.self) |
This file contains hidden or 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
| import UIKit | |
| extension UIViewController { | |
| @IBAction func goBack() { | |
| if let nc = navigationController, | |
| nc.viewControllers.count >= 2 { | |
| nc.popViewController(animated: true) | |
| } else { | |
| dismiss(animated: true, completion: nil) | |
| } |
This file contains hidden or 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
| import UIKit | |
| class DateSelectorViewController: UIViewController { | |
| @IBOutlet weak var tfDOB: UITextField! | |
| @IBOutlet weak var tfNextField: UITextField! | |
| var selectedDOB: Date? = nil | |
| let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 216)) |