Swift but a bit Sweeter - More Syntactic Sugar for Swift
This is a collection of extensions and operators that make swift a bit sweeter. I have added these from multiple projects where I've been using these.
Note: These operators are supposed to help me in the way I write Swift. Which is a functional style. So most of these regard possible problems and annoyances with functional programming in Swift.
Please Contribute to make Swift a bit cooler looking... Post your ideas in the issues as enhancements
To add this to your project you have to add this to your Podfile:
pod Sweeft
Will pipe the left value to the function to the right. Just like in Bash:
value | function
is the same as:
function(value)
This will call map with the function to the right:
array => function
is the same as:
array.map(function)
The same as above but with flatMap.
The same as above but with filter
Will cast a function to allow any input and drop it.
**{
// Do stuff
}
is the same as:
{ _,_ in
// Do stuff
}
or as a postfix it will drop the output
{
return something
}**
is equivalent to:
{
_ = something
}
Will assign b to a if b is not nil
a <- b
is equivalent to:
a = b ?? a
Will assign the result of a map to an array.
array <- handler
is equivalent to:
array = array.map(handler)
If the handler returns an optional, but the array can't handle optionals then it will drop all of the optionals.
Will assign the result of a filter to the array
array <| handler
is the same as:
array = array.filter(handler)
let array = [1, nil, 3]
!array // [1, 3]
// a = 1 and b = 2
a <=> b // a = 2 and b = 1
Note: the type has to conform to the Defaultable protocol.
let i: Int? = nil
let j: Int? = 2
i.? // 0
j.? // 2
Will check if a value is not nil
.?myVariable
is equivalent to:
myVariable != nil