extension UIApplication {
var firstKeyWindow: UIWindow? {
return UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.filter { $0.activationState == .foregroundActive }
.first?.keyWindow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func callShell(_ command: String) throws -> String { | |
let task = Process() | |
let pipe = Pipe() | |
task.standardOutput = pipe | |
task.standardError = pipe | |
task.arguments = ["-c", command] | |
task.executableURL = URL(fileURLWithPath: "/bin/zsh") | |
task.standardInput = nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let tcNumber = "{TC_NUMBER}}" | |
let lastDigitOfTCNumber = "\(tcNumber)".suffix(1) | |
let totalOfNumbers = tcNumber.prefix(10).reduce(0) { Int($0) + (Int("\($1)") ?? 0) } | |
let lastDigitOfTotalNumber = "\(totalOfNumbers)".suffix(1) | |
if | |
tcNumber.count == 11, | |
lastDigitOfTCNumber == lastDigitOfTotalNumber { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: https://www.avanderlee.com/concurrency/task-groups-in-swift/ | |
let images = try await withThrowingTaskGroup(of: UIImage.self, returning: [UIImage].self) { taskGroup in | |
let photoURLs = try await listPhotoURLs(inGallery: "Amsterdam Holiday") | |
for photoURL in photoURLs { | |
taskGroup.addTask { try await downloadPhoto(url: photoURL) } | |
} | |
var images = [UIImage]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Safari : com.apple.Safari | |
Chrome: com.google.Chrome | |
Firefox: org.mozilla.firefox | |
Opera: com.operasoftware.Opera | |
// Learn Bundle Id on Termina | |
osascript -e 'id of app "Opera"' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum PhotoListConstant { | |
static let listURL = "https://jsonplaceholder.typicode.com/photos" | |
} | |
enum NetworkError: Error { | |
case invalidURL | |
} | |
final class PhotoListViewModel: ObservableObject { | |
@Published var photoItems = [PhotoItemModel]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct PhotoItemModel: Codable, Identifiable { | |
let id: Int | |
let title: String | |
let url: String | |
let thumbnailUrl: String | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct PhotoListView: View { | |
// 1 | |
@StateObject var viewModel = PhotoListViewModel() | |
var body: some View { | |
NavigationView { | |
List(viewModel.photoItems) { photoItem in | |
// 2 | |
ListItem(photoItem: photoItem) | |
} | |
.navigationTitle("Photo List") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fetchPerson(by userId: String) async -> Person { | |
do { | |
// Tell compiler wait for the result with `await` keyword | |
let (data, response) = try await URLSession.shared.data(from: <URL>) | |
} catch { | |
// ... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import AVFoundation | |
enum CameraSession: Error { | |
case inValidInput | |
case inValidOutput | |
} | |
final class CameraSessionViewController: UIViewController { | |
// Camera Setting |
NewerOlder