Last active
July 6, 2017 22:09
-
-
Save murphman300/d7312e32b28e489c74ad79da8f85269d to your computer and use it in GitHub Desktop.
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
// | |
// MultiVar.swift | |
// | |
// | |
// Created by Jean-Louis Murphy a.k.a Murphman300(github.com, stackoverflow.com, twitter.com) on 2017-02-19. | |
// Copyright © 2017 Jean-Louis Murphy. All rights reserved. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
class MultiVar { | |
private var intValue : Int? | |
private var doubleValue : Double? | |
private var cgFloatValue : CGFloat? | |
private var stringValue : String? | |
private var alphabetical = "abcdefghijklmnopqrstuvwxyzABCDEFGHIKLMNOPQRSTUVWXYZ!@#$%^&*()_+}{|:'/<>?()[]" | |
var integer : Int { | |
get { | |
guard let int = intValue else { | |
return 0 | |
} | |
return int | |
} set { | |
set(newValue) | |
} | |
} | |
var double : Double { | |
get { | |
guard let dble = doubleValue else { | |
return 0.0 | |
} | |
return dble | |
} set { | |
set(newValue) | |
} | |
} | |
var cgFloat : CGFloat { | |
get { | |
guard let flt = cgFloatValue else { | |
return 0 | |
} | |
return flt | |
} set { | |
set(newValue) | |
} | |
} | |
var string : String { | |
get { | |
guard let stg = stringValue else { | |
return "" | |
} | |
return stg | |
} set { | |
set(newValue) | |
} | |
} | |
init(_ value: Any?) { | |
guard let val = value else { return } | |
set(val) | |
} | |
public func set(_ i: Any) { | |
if Mirror(reflecting: i).subjectType == Mirror(reflecting: Int()).subjectType { | |
guard let it = i as? Int else { return } | |
intValue = it | |
cgFloatValue = CGFloat(it) | |
doubleValue = Double(it) | |
if it < 10 && it > 0 { | |
stringValue = "0\(String(describing: it))" | |
} else { | |
stringValue = String(describing: it) | |
} | |
} | |
if Mirror(reflecting: i).subjectType == Mirror(reflecting: String()).subjectType { | |
let s = String(describing: i) | |
stringValue = s | |
if isNonAlpha() { | |
let int = Int(s) | |
intValue = Int(s) | |
if let t = int { | |
let c = CGFloat(t) | |
cgFloatValue = c | |
if t < 10 && t > 0 { | |
stringValue = "0\(s)" | |
} | |
} | |
if let s = stringValue, let int = intValue, s.characters.count == 3 && int > 0 || s.characters.count == 1 { | |
guard let first = s.characters.first, first == "0" else { return } | |
stringValue = String(describing: i) | |
} | |
} | |
} | |
if Mirror(reflecting: i).subjectType == Mirror(reflecting: CGFloat()).subjectType { | |
guard let cg = i as? CGFloat else { | |
guard let cg = i as? Double else { | |
return | |
} | |
let int = Int(cg) | |
cgFloatValue = CGFloat(cg) | |
intValue = int | |
doubleValue = cg | |
stringValue = String(describing: cg) | |
return | |
} | |
let int = Int(cg) | |
cgFloatValue = cg | |
intValue = int | |
let s = String(describing: cg) | |
stringValue = s | |
if int < 10 && int > 0 { | |
stringValue = "0\(s)" | |
} | |
} | |
if Mirror(reflecting: i).subjectType == Mirror(reflecting: Double()).subjectType { | |
guard let db = i as? Double else { return } | |
cgFloatValue = CGFloat(db) | |
intValue = Int(db) | |
let s = String(describing: db) | |
stringValue = s | |
stringValue = "\(s)" | |
} | |
} | |
private func isNonAlpha() -> Bool { | |
var b = Bool() | |
if let st = stringValue { | |
b = !alphabetical.contains(st) | |
} | |
return b | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment