Skip to content

A library for building for converting primitive values like String, Int Number and so so to another type in Swift

Notifications You must be signed in to change notification settings

FullStack-Swift/swift-extension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift Extension

A description of this package.

String

//convert to Int

let string: String = "10"
let int: Int? = string.toInt() // int == Optional(10)

let string: String = "swift"
let int: Int? = string.toInt() // int == nil
//convert to Double

let string: String = "3.14"
let double: Double? = string.toDouble() // double == Optional(3.14)

let string: String = "swift"
let double: Double? = string.toDouble() // double == nil
//convert to Float

let string: String = "3.14"
let float: Float? = string.toFloat() // float == Optional(3.14)

let string: String = "swift"
let float: Float? = string.toFloat() // float == nil
//convert to Bool

let string: String = "true"
let bool: Bool = string.toBool() // bool == true

let string: String = "1"
let bool: Bool = string.toBool() // bool == true

let string: String = "swift"
let bool: Bool = string.toBool() // bool == false
//convert to URL

let string: String = ""
let url: URL? = string.toURL()
//convert to UUID

let string: String = ""
let uuid: UUID? = string.toUUID()

Double

//convert to Int

let double: Double = 3.14
let int = double.toInt() // int == 3
// convert to Int

let double: Double = 3.14
let string = double.toString() // string == "3.14"
//convert to Float
let double: Double = 3.14
let float: Float = double.toFloat() // float == 3.14

Int

//convert to Double

let int: Int = 100
let double: Double = int.toDouble() // double = 100.0
//convert to Float

let int: Int = 100
let float: Float = int.toFloat() // float == 100.0
//convert to String

let int: Int = 100
let string: String = int.toString() // string == "100"

Float

//convert to String

let float: FLoat = 3.14
let string: String = float.toString() // string == "3.14"
//convert to Double

let float: FLoat = 3.14
let double: Double = float.toDouble() // double == 3.14
//convert to Int

let float: FLoat = 3.14
let int: Int = float.toInt() // int == 3

UUID

//convert to String

let uuid: UUID = UUID()
let uuidString: String = uuid.toString()

URL

//convert to String

let url: URL! // = something
let urlString: String = url.toString()

MIX

let string: String = "3.14"
let value = string.toDouble()?.toInt().toString().....

MIX DATA

struct Model: Codable {

}

//or

class Model: Codable {

}

// or

enum Model: Codable {

}

Dictionary

let dict: [String: Any] = [:] // something

if let model = dict.toModel(Model.self) {

}

if let string = dict.toString() {

}

if let data = dict.toData() {

}

String

let string: String = "" // something

if let dict = string.toDictionary() {

}

if let model = string.toModel(Model.self) {

}

if let data = string.toData() {

}

Data

let data: Data = // something
if let dict = data.toDictionary() {

}

if let string = data.toString() {

}

if let model = data.toModel(Model.self) {

}

if let data = data.toData(keyPath: "result") {

}

Bonous

if let value = string.toDictionary().toData().toData(keyPath: "result")... {
    // to do something
}

// clone Model
let value = model.toData().toModel(Model.self) {

}

//convert model to Data
if let data = model.toData() {

}

// convert Model to String
if let string = model.toString() {

}

//convert Model to Dictionary
if let dict = model.toDictionary() {

}

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/FullStack-Swift/SwiftExtension", .upToNextMajor(from: "1.0.0"))
]
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
February 2021

Copyright (C) 2021 Mike Packard <[email protected]>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

About

A library for building for converting primitive values like String, Int Number and so so to another type in Swift

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages