Skip to content

Instantly share code, notes, and snippets.

View StewartLynch's full-sized avatar

Stewart Lynch StewartLynch

View GitHub Profile
import Foundation
// Here is the codable model that I am trying to decode the feed into
struct Feed: Codable {
let items: [Items]
let pageInfo: PageInfo
let nextPageToken: String?
}
struct PageInfo: Codable {
@StewartLynch
StewartLynch / FocusState Not working
Created June 13, 2021 13:41
Example of Xcode 13 FocusState not working on Modal Sheets
import SwiftUI
enum Field {
case text1
}
struct ContentView: View {
// This works fine
@State private var showModal = false
@State private var text1:String = ""
struct ContentView: View {
@State private var isPresented = false
var body: some View {
Button("Show Alert") {
isPresented = true
}
.alert("Alert Title", isPresented: $isPresented) {
Button("OK",role: .cancel) {}
} message: {
Text("Message below Title")
//
// ContentView.swift
// OverlayDrag
//
// Created by Stewart Lynch on 2021-07-08.
//
import SwiftUI
struct ContentView: View {
class APIService {
static let shared = APIService()
enum APIError: Error {
case error(_ errorString: String)
}
func getJSON<T: Decodable>(urlString: String,
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate,
keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys,
completion: @escaping (Result<T,APIError>) -> Void) {
//
// Country.swift
// Country
//
// Created by Stewart Lynch on 2021-07-16.
//
import Foundation
struct Country: Identifiable {
struct ContentView: View {
@State private var names = ["Stewart", "Emily"]
@State private var newName = ""
@FocusState private var isFosused:Bool
var body: some View {
NavigationView {
List() {
ForEach($names, id:\.self) { $name in
TextField(name, text: $name)
.focused($isFosused, equals: true)
extension Bundle {
public func decode<T: Decodable>(_ type: T.Type,
from file: String,
dateDecodingStategy: JSONDecoder.DateDecodingStrategy = .deferredToDate,
keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Failed to locate \(file) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(file) from bundle.")
// is there a better way to do this?
// I want to be able to execute something after all of the items have printed
func doIt() {
let numItems = 3
for item in 0...numItems {
DispatchQueue.main.asyncAfter(deadline: .now() + (Double(item) * 1)) {
print(item)
}
}
//
// Created for ErrorAlert
// by Stewart Lynch on 2022-06-22
// Using Swift 5.0
//
// Follow me on Twitter: @StewartLynch
// Subscribe on YouTube: https://youTube.com/StewartLynch
//
import SwiftUI