Skip to content

Instantly share code, notes, and snippets.

View andresr-dev's full-sized avatar

Andres Raigoza andresr-dev

View GitHub Profile
@andresr-dev
andresr-dev / CoreDataExample.txt
Last active May 1, 2022 17:24
This is an example of how you can integrate CoreData in SwiftUI.
Create a new Data Model file: "CoreDataExample.xcdatamodeld"
Once inside add a new entity and attributes:
ENTITIES:
Student
ATTRIBUTE:
id: UUID
firstName: String
lastName: String
@andresr-dev
andresr-dev / CoreDataPredicates.swift
Last active May 1, 2022 17:22
This is an example of how to apply predicates with CoreData in SwiftUI
import SwiftUI
// MARK: CORE DATA MODEL
/*
ENTITY: ShipEntity
ATTRIBUTES:
name: String
universe: String
*/
@andresr-dev
andresr-dev / ACoreDataRelationshipsExample.txt
Last active May 1, 2022 17:27
This is an example of how to use relationships with CoreData in SwiftUI
First create your data model file: "CoreDataExample.xcdatamodeld" and add this two entities:
1.
ENTITY: "CountryEntity"
ATTRIBUTES:
shortName: String
fullName: String
NOTE: While having Country Entity selected, Open Data Model Inspector on the right side of the screen and add "shortName"
constraint, this is because this property must be unique.
@andresr-dev
andresr-dev / CoreDataDynamicallyFiltering.txt
Created May 1, 2022 17:20
This is an example of how you can perform dynamically filtering with CoreData in SwiftUI
First create a data model file: "CoreDataExample.xcdatamodeld", once inside create this entity with this attributes:
ENTITY: "SingerEntity"
ATTRIBUTES:
firstName: String
lastName: String
Now let's generate a class of our entity manually so that we don't have to deal with optionals, like this:
1. While having "SingerEntity" selected, open the Data Model inspector on the right side of the screen and change Codegen
from "Class Definition" to "Manual/None"
@andresr-dev
andresr-dev / LocalNotificationsExample.swift
Created May 1, 2022 17:56
This is an example of how you can schedule local notifications with SwiftUI
// To test this code: Run the simulator, tap request permission and grant it.
// Now tap schedule notification and lock the screen, the notification should appear in 5 seconds
import SwiftUI
import UserNotifications
struct LocalNotifications: View {
var body: some View {
VStack {
Button("Request Permission") {
@andresr-dev
andresr-dev / MLTabularRegressionExample.txt
Created May 2, 2022 21:10
This is an example of how you can use Machine Learning in your SwifUI Apps
First open Create ML App:
1. Go to Xcode/Open Developer Tool/Create ML. In the prompt click New Document
2. Choose a template: "Tabular Regression" in this case and click Next.
3. Give it a project name "BetterRest" in this case, click Next and save the project.
Now we need to provide training data to our ML, you can use this sample data from Hacking with swift:
https://github.com/twostraws/HackingWithSwift/blob/main/SwiftUI/project4-files/BetterRest.csv
This is a spreadsheet with 10.000 samples of 4 values:
1. When someone want to wakeup
2. How much sleep they want to have
@andresr-dev
andresr-dev / GreatestCommonDivisor.swift
Last active May 5, 2022 15:27
This is a function that helps you to find the greatest common divisor between 2 numbers.
func gcd(_ a: Int, _ b: Int) -> Int {
var a = a
var b = b
while b != 0 {
let temp = b
b = a % b
a = temp
}
return a
@andresr-dev
andresr-dev / PhoneStackNavigation.swift
Last active May 3, 2022 18:14
This View extension helps you detect when your app is being used by an iPhone, and then forces the navigations views to behave like stack navigations views.
import SwiftUI
extension View {
@ViewBuilder func phoneOnlyStackNavigation() -> some View {
if UIDevice.current.userInterfaceIdiom == .phone {
self.navigationViewStyle(.stack)
} else {
self
}
}
@andresr-dev
andresr-dev / HorizontalAvailableSpace.swift
Last active May 3, 2022 23:39
This horizontal size class environment lets you run different layouts on your views depending on how much horizontal space you have.
// Note: to actually see the result of this code please use the iPhone 13 Pro Max simulator or any Plus device
// Since these are the ones with the largest screens.
import SwiftUI
struct UserView: View {
var body: some View {
Group {
Text("Name: Andrés")
Text("Country: Colombia")
Text("Pets: No pets at the moment!")
@andresr-dev
andresr-dev / DynamicTypeSizeExample.swift
Last active May 4, 2022 00:10
This is how you can change your layout based on the text size that the user configured in settings.
// Note: you can change the text size directly from Xcode going to the Environment Override button, on the bottom side of the screen.
struct UserView: View {
var body: some View {
Group {
Text("Name: Andrés")
Text("Country: Colombia")
Text("Pets: No pets")
}
}
}