Skip to content

Instantly share code, notes, and snippets.

@foxicode
foxicode / MathNumeric.swift
Created October 15, 2020 19:56
Number rounding in Swift
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
@foxicode
foxicode / SimpleNumeric.swift
Created October 15, 2020 19:52
Simple numeric conversions in Swift
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
@foxicode
foxicode / JSONAndBack.swift
Created October 15, 2020 19:38
Swift Dictionary to JSON and back
import Foundation
let json = """
{
"key1": "val1",
"key2": "val2",
"arr": [1, 2, 3]
}
"""
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)
@foxicode
foxicode / SwiftXML.swift
Created October 15, 2020 18:45
Parsing XML in Swift
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>"
@foxicode
foxicode / FormatPriceString.swift
Created October 15, 2020 18:24
Formatting price in Swift
import Foundation
let price = 1.5
let strPrice = String(format: "%.02f", price)
@foxicode
foxicode / StringNumber.swift
Created October 15, 2020 18:21
Conversion between numbers and String in Swift
import Foundation
let num = 1
let str = "\(num)"
let numAgain = Int(str)
let dNum = 1.5
let dStr = "\(dNum)"
let dNumAgain = Double(dStr)
@foxicode
foxicode / DataStringConversions.swift
Created October 15, 2020 18:15
Conversions between Data and String in Swift
import Foundation
let str = "Just a text string"
let data = str.data(using: .utf8)!
let str2 = String(decoding: data, as: UTF8.self)
@foxicode
foxicode / UIViewController+goBack.swift
Created October 13, 2020 17:30
Universal go-back function
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)
}
@foxicode
foxicode / DateSelectorViewController.swift
Created September 15, 2020 07:37
UIViewController with date selector
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))