🪶 Lightweight, Swift-y looking code for modern SwiftUI developers
🧩 Useful methods and properties written in native Swift
🚧 Wiki under construction. Read below to get started!
FoundationPlus is a suite of basic Swift extensions that help speed up the process of app development, and help make your code look nicer. Functions that you'll normally need, like methods to check the app's version, get the current day of week, and so on, can have convoluted code. FoundationPlus simplifies these with built-in extensions.
// 😴 Before
letinterval=TimeInterval(9945)
// ✨ After
letinterval=2.hours +45.minutes +45.seconds// 😴 Before
iflet value = optionalValue {
myVar = value
}else{
myVar =nil}
// ✨ After
myVar =? optionalValue// 😴 Before
if array.count >=5{
myVar =array[4]}
// ✨ After
myVar =array[safe:4]- Extensions
- Other Features
Most of the above features are cross-platform and are supported on iOS, macOS, tvOS and watchOS.
Add FoundationPlus to your project using Swift Package Manager:
https://github.com/Flowductive/foundation-plus
Get TimeIntervals easily:
letinterval=10.minutes
letinterval2=4.daysFormat TimeIntervals:
print(3.hours.formattedColon) // 3:00
print((2.minutes +30.seconds).formatted(.short)) // 2m 30s
print(4.hours.formatted(.full)) // 4 hoursGet the current month, day, hour, minute, etc:
letdate=Date()letdateFormatted="The current time is \(date.hour) : \(date.minute)."Get a date moved forward to right before midnight:
letthisEvening= date.atMidnightGet formatted versions of the date:
print(date.shorthand) // 6/7/22
print(date.longhand) // Tuesday, June 07, 2022Subtract dates and get a TimeInterval:
lettimeBetween= laterDate - earlierDateGet the next/preview occurence of a specific weekday:
letnextTuesday=Date().next(.tuesday)letlastWednesday=Date().previous(.wednesday, considerToday:false)Get a string representing how long it's been since the Date:
print(lastWednesday.timeAgoSince()) // 2 days agoSafely subscript arrays, and even wrap array indices:
letmyString=["a","b","c","d"]letmyString:String=myStrings[safe:5]??"-" // "-"
letmyString2:String=myString[wrap:5] // "b"Use new operators on arrays:
vararray:[1,2,3]
// Add the element(s) if it is not inside
array <=4 // [1, 2, 3, 4]
array <=2 // [1, 2, 3, 4]
array <=[4,5] // [1, 2, 3, 4, 5]
// Remove elements
array -=1 // [2, 3, 4, 5]
array -=[2,3,6] // [4, 5]Use additional methods on arrays:
letarray=[1,2,3,3]
// Add to the end if not already in the array
array.appendUniquely(5) // [1, 2, 3, 3, 5]
// Add to the beginning if not already in the array
array.pushUniquely(0) // [0, 1, 2, 3, 3, 5]
// Remove all of an element
array.removeAll(3) // [0, 1, 2, 5]
// Pick a random bunch of elements
array.pick(2) // [0, 2]Optionally assign the right-hand side to the left-hand side if non-nil:
varstr:String="Hello World"varoptionalStr:String?=nil
str =? optionalStr // Only sets str if myOptionalString is non-nil
print(str) // "Hello, world"Optionally check if two Optional or non-Optional values are equal/nonequal, and return false for equality and true for inequality if either value is nil:
letareEqual:Bool{nil==?4} // False
letareNonEqual:Bool{4!=?3} // TrueReturn only alpha-numeric parts of a String:
letalphanumeric="Abc123+-=~".alphanumeric // "Abc123"Return the first word of a String:
letfirst="Hello World!".firstWord // "Hello"Get a random alphanumeric string with a specified length:
letrandom=String.random(16) // "b7vb92Fg9FEN2g8A"Repeat a String:
letrepeated="Hi".repeat(5) // "HiHiHiHiHi"Check if a String is a valid email:
letisEmailValid:Bool="test@hi.com".isValidEmail() // TrueQuickly open a URL in iOS or macOS:
myUrl.open()Grab information regarding the time of day:
letpart=DayPart.get(from:Date()) // Chooses automatically from .morning, .midday, .afternoon, .evening, .night, and .midnight
print(part.greeting) // "Good morning!".png)