GET
let manager = APIManager()
let url = "http://localhost:8000/users"
func getUsers() async throws -> [User] {
let users: [User] = try? manager.get(url: url)
return users ?? []
}POST
let manager = APIManager()
let url = "http://localhost:8000/api/auth/signup/"
func postUser() async throws -> User? {
let newUser = User(name: String)
let user: User = try? manager.post(url: url, value: newUser)
return user
}PUT
let manager = APIManager()
let url = "http://localhost:8000/user/"
func putUser(user: User) async throws -> User? {
let user: User = try? manager.put(url: url, value: user)
return user
}PATCH
let manager = APIManager()
let url = "http://localhost:8000/user/"
func patchUser(user: User) async throws -> User? {
let user: User = try? manager.patch(url: url, value: user)
return user
}DELETE
let manager = APIManager()
let url = "http://localhost:8000/user/"
func deleteUser(userID: Int) async throws -> User? {
let user: User = try? manager.delete(url: url, id: userID)
return user
}Fetch saves
let manager = SaveManager()
let countries: [CountryEntity] = []
func fetchCountries() {
guard let fetchedSaves: [CountryEntity] = try? saveManager.fetchedObjects(entityName: "CountryEntity") else { return }
countries = fetchedSaves
}Save
let manager = SaveManager()
func save() { try? manager.save() }Delete save
let manager = SaveManager()
func deleteCountry(_ country: CountryEntity) {
withAnimation {
saveManager.delete(country)
fetchCountries()
}
}Delete all save
let manager = SaveManager()
let countries: [CountryEntity] = []
manager.deleteAll(objects: countries)func content() async throws { // Async await content }
func completion() { // Completion }
AsyncManager.loadContent(content: content, completion: completion)let manager = BundleManager()
let countries: [Country] = try? manager.decodeJSON("countries", fileExtension: "json")Canvas
struct MyCanvasView: View {
@StateObject var canvasManager = CanvasManager(lineColor: .blue, lineWidth: 10.0)
var body: some View {
CanvasBoard(manager: canvasManager)
.frame(width: 300, height: 300)
}
}Payment Manager
import PassKit
var paymentRequest: PKPaymentRequest {
let item = PKPaymentSummaryItem(label: "Balloon", amount: NSDecimalNumber(value: 3.14), type: .final)
let request = PKPaymentRequest()
request.paymentSummaryItems = [item]
request.merchantIdentifier = "merchant.com.YOURDOMAIN.YOURAPPNAME"
request.merchantCapabilities = .capability3DS
request.countryCode = "US"
request.currencyCode = "USD"
request.requiredShippingContactFields = [.phoneNumber, .emailAddress]
request.supportedNetworks = [.visa]
}
struct MyPaymentView: View {
var paymentManager = PaymentManager()
var body: some View {
PayWithApplePayButton {
paymentManager.startPayment(paymentRequest: paymentRequest) { result in
result ? print("Success") : print("Failure")
}
}
}
}FX Animation
struct MyFXAnimationView: View {
let frames: [String] = ["sparks0", "sparks1", "sparks2"]
var body: some View {
FXAnimation(frames: frames, isRepeatingForever: true)
.scaledToFit()
}
}UITextAnimation
struct MyTextAnimationView: View {
@StateObject var textManager = UITextManager()
var body: some View {
Text(textManager.prompt)
.onAppear {
textManager.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
textManager.speed = .verySlow
textManager.startWriting()
}
}
}ChronoDisplay
struct MyChronoView: View {
@StateObject var chronoManager = ChronoManager()
var body: some View {
ChronoDisplay(chronoManager: chronoManager)
}
}SF Symbols
struct SFMathsImageView: View {
var body: some View {
Image(sfMaths: .multiply)
}
}
struct SFNatureImageView: View {
var body: some View {
Image(sfNature: .sunMax)
}
}Custom Font
struct MyCustomFontText: View {
var body: some View {
Text("Hello World")
.font(.custom(systemName: .americanTypewriter, size: 15))
}
}New colors
The colors exists in the XCode Color Panel but not available on Color yet.
struct ColorsView: View {
var body: some View {
VStack {
Rectangle()
.foregroundColor(.salmon)
Rectangle()
.foregroundColor(.banana)
Rectangle()
.foregroundColor(.strawberry)
}
}
}AcceptOnlyNumbers
struct PhoneNumberFieldView: View {
@State var number = ""
var body: some View {
TextField("Number", text: $number)
.acceptOnlyNumbers(text: $number)
}
}LimitText
struct UsernameFieldView: View {
@State var username = ""
var body: some View {
TextField("Username", text: $username)
.limitText(text: $username, limit: 15)
}
}Tooltip
struct TooltipView: View {
@State var isPresented = false
var body: some View {
Button {
isPresented.toggle()
} label: {
Text("Tap me")
}
.tooltip(isPresented: $isPresented,
arrowDirection: .up) {
Text("This is the tooltip content !")
}
}
}Cropped
It will crop the image by focusing on a specific part.
struct CroppedImageView: View {
var body: some View {
Image("Portrait")
.cropped(zoomedPart: .top)
}
}Carousel
struct Student: Identifiable {
var id = UUID()
var name: String
}
var students: [Student] = [
Student(name: "John"),
Student(name: "Mary"),
Student(name: "Harry"),
Student(name: "Kate"),
Student(name: "Patrick"),
]
struct MyCarouselView: View {
@State var index: Int = 0
var body: some View {
Carousel(index: $index, items: students) { student in
RoundedRectangle(cornerRadius: 15)
.overlay(
Text(student.name)
.foregroundColor(.white)
)
}
}
}Shapes
struct MyShapesView: View {
var body: some View {
VStack {
Diamond()
Heart()
Hexagon()
Star(corners: 5, smoothness: 0.45)
Triangle()
}
}
}GradientContent
struct MyGradientTextView: View {
var body: some View {
GradientContent(gradient: Gradient.rainbow,
startPoint: .leading,
endPoint: .trailing) {
Text("Hello World")
}
}
}World capital
import MapKit
struct MyMapView: View {
@State var capital = MKCoordinateRegion(capital: .amsterdam)
var body: some View {
Map(coordinateRegion: $capital)
}
}Default value subscript
let values: [Int] = []
print(values[0, default: 99])
// 99Safe subscript
let values: [Int] = []
print(values[safe: 0])
// nilSplit an array
func splitted(into size: Int) -> [[Element]]let values: [Int] = [0, 1, 2, 3, 4, 5]
let splittedValues = values.splitted(into: 2)
print(splittedValues)
// [[0, 1], [2, 3], [4, 5]]Split
func split -> (firstPart: [Element], lastPart: [Element])var values: [Int] = [0, 1, 2, 3, 4, 5]
let splittedValues = values.split()
print(splittedValues.firstPart)
print(splittedValues.lastPart)
// [0, 1, 2]
// [3, 4, 5]Swap
mutating func swap(between valueA: Int, and valueB: Int)var values: [Int] = [0, 11, 2, 3, 44, 5]
values.swap(between: 1, and: 4)
print(values)
// [0, 44, 2, 3, 11, 5]Remove a random element
mutating func removeRandomElement()Prepend
mutating func prepend(_ element: Element)var values: [Int] = [0, 1, 2, 3, 4, 5]
values.prepend(99)
print(values)
// [99, 0, 1, 2, 3, 4, 5]Replace all nil
mutating func replaceAllNil<T>(by element: T) where Element == Optional<T>var values = [75, nil, 40, 90, nil, 10]
values.replaceAllNil(by: 100)
print(values)
// [75, 100, 40, 90, 100, 10]Max index
func maxIndex() -> Int?let values: [Int] = [0, 1, 2, 99, 4, 5]
let maxIndex = values.maxIndex()
print(maxIndex)
// Optional(3)Remove element
mutating func remove(_ element: Element)var names: [String] = ["Ash", "Brock", "Misty"]
names.remove("Brock")
print(names)
// ["Ash", "Misty"]Contains at least
func containsAtLeast(_ requirement: [Element]) -> Boollet names: [String] = ["Ash", "Brock", "Misty"]
print(names.containsAtLeast(["Misty", "Misty", "Brock"]))
// falseContains elements
func contains(_ elements: [Element]) -> BoolExample:
let names: [String] = ["Ash", "Brock", "Misty"]
print(names.contains(["Ash", "Pikachu"]))
// falseWith XOR operators
func withXOROperators() -> UInt32?Each category of bitmask represents an enum value of a UInt32.
The object could be 0x1 << 0, the player 0x1 << 1 and the npc 0x1 << 2
var categories: [BitmaskCategory] = [.object, .player, .npc]
let categoryValue = categories.withXOROperators()
// The result is "object | player | npc" and is an UInt32.Element frequencie
func elementFrequencies() -> [Element : Int]let values: [Int] = [1, 2, 2, 2, 3, 3, 4, 4]
let frequencies = values.elementFrequencies()
print(frequencies)
// [[1: 1], [2: 3], [3: 2], [4: 2]]Random element with odds
func randomElementWithOdds() -> Element?// Need to conform the method to Probability
struct Student: Probability {
let name: String
var odds: Int
}
let students: [Student] = [
Student(name: "Jean", odds: 10),
Student(name: "Paul", odds: 30),
Student(name: "Jack", odds: 20),
Student(name: "Julie", odds: 15),
Student(name: "Gerard", odds: 25),
]
print(students.randomElementWithOdds)
// This will print a random student depending on its odd