SwiftySweetness 2.0.0

SwiftySweetness 2.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2018
SPMSupports SPM

Maintained by Christopher Szatmary, Christopher Szatmary.


Downloads

Total25
Week0
Month0

Installs

Apps5
Test Targets1
powered by Segment

GitHub

Stars0
Watchers0
Forks0
Issues0
Contributors1
Pull Requests0

Code

Files20
LOCLines of Code 368


  • By
  • Christopher Szatmary

SwiftySweetness

Language Swift Version License Platform Swift Package Manager compatible

SwiftySweetness is a list of extensions that provides extra functionality and syntactic sugar.

Version 2 Changes

As of version 2.0.0 all UIKit and AppKit extensions have been removed. They have been moved to HotCocoa. This was done as this framework is meant to be a pure swift framework that should work cross platform without need for the Objective-C runtime.

Examples

Int & Double

Power operator **:

let power = 2 ** 4 // 16

Or assign the result to one of the variable arguments:

var num = 3
num **= 3 // 27

isNegative Bool property indicating whether or not the given number is negative:

if num.isNegative {
    ...
}

Optionals

hasValue property to quickly check if the optional is nil or not:

var myOptional: Int? = nil
myOptional.hasValue // false

Strings

Use subscripts to get character at a given index:

let str = "Hello world!"
str[2] // 'l'

Or pass a range to get a substring:

str[2..<7] // "llo w"
str[2...7] // "llo wo"

trimmed returns a trimmed version of the string without leading or trailing whitespaces and newlines.

let str = "      \n\nMy String\n\n    \n"
str.trimmed // "My String"

splitCamelCase() splits a camel cased string.

let str = "thisIsACamelCasedString"
str.splitCamelCase() // "this Is A Camel Cased String"

initials() returns the first letter of each word in the string.

let str = "Hello World"
str.initials() // "HW"

Encodable & Decodable

Swift 4 makes encoding and decoding JSON easy with the Encodable and Decodable protocols:

let json = JSONEncoder().encode(myStruct)
let decodedStruct = JSONDecoder().decode(MyStruct.self, from: json)

But SwiftySweetness makes this even easier:

let json = myStruct.encode(to: .json)
let decodedStruct = MyStruct.decode(from: json, ofType: .json)

PropertyRepresentable

The PropertyRepresentable allows any conforming type to generate an array containing all its properties.

struct Person: PropertyRepresentable {
    var name: String
    var age: Int
}

let person = Person(name: "John Doe", age: "20")
person.properties() // [(label: "name", value: "John Doe"), (label: "age", value: 20)]
person.propertyLabels() // ["name", "age"]
person.propertyValues() // ["John Doe", 20]
person.propertiesDictionary() // ["name": "John Doe", "age": 20]

Pipes

SwiftySweetness offers multiple pipe operators. Piping is supported for all unary, binary, and ternary functions. |> is the standard pipe operator. It will pipe the input on the left to the function on the right.

func add(_ x: Int, _ y: Int) -> Int {
    return x + y
}

let num = 4
num |> (add, -10) |> abs // 6

The |>? operator takes an optional and either pipes it to the given function if it has a value or returns nil if the value is nil. Note that the value returned is an optional.

let optional1: Int? = 4
optional1 |>? (add, 6) // 10 NOTE: This is of type Int?

let optional2: Int? = nil
optional2 |>? (add, 6) // nil

The |>! operator force-unwraps an optional and then pipes it to the given function. This should only be used when you are certain the value is not nil!

let optional1: Int? = 4
optional1 |>! (add, 6) // 10 NOTE: This is of type Int since the value was unwrapped

let optional2: Int? = nil
optional2 |>! (add, 6) // fatal error: unexpectedly found nil while unwrapping an Optional value

And much more!

Use it in a project to see what's available.

Installation

Requirements

  • iOS 8.0+
  • macOS 10.9+
  • tvOS 9.0+
  • watchOS 2.0+
  • Linux
  • Swift 4

CocoaPods

SwiftySweetness is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftySweetness', '~> 1.4'

Swift Package Manager

SwiftySweetness is available through the Swift Package Manager To install it, add the following to your Package.swift.

import PackageDescription

let package = Package(
    name: "MyProject",
    dependencies: [
        .package(url: "https://github.com/cszatma/SwiftySweetness.git", from: "1.4.0")
    ]
)

Contributing

Open an issue or submit a pull request.