Skip to content

Instantly share code, notes, and snippets.

View erdemildiz's full-sized avatar
🏠
Working from home

Erdem ILDIZ erdemildiz

🏠
Working from home
View GitHub Profile
@erdemildiz
erdemildiz / run_shell_script.swift
Last active July 18, 2024 10:20
Run Shell Script on MacOS
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
@erdemildiz
erdemildiz / root-viewcontroller-in-swift.md
Created September 21, 2023 13:46
Root view controller in Swift

IOS 15

extension UIApplication {
    var firstKeyWindow: UIWindow? {
        return UIApplication.shared.connectedScenes
            .compactMap { $0 as? UIWindowScene }
            .filter { $0.activationState == .foregroundActive }
            .first?.keyWindow
@erdemildiz
erdemildiz / check-tc-identify.swift
Created March 13, 2023 12:34
Turkish Identify Number Control in Swift
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 {
@erdemildiz
erdemildiz / taskgroup.swift
Created January 30, 2023 07:38
Task Group
// 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]()
@erdemildiz
erdemildiz / browser_bundle_identifiers.txt
Last active September 1, 2022 13:15
Browser Bundle Identifiers
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"'
enum PhotoListConstant {
static let listURL = "https://jsonplaceholder.typicode.com/photos"
}
enum NetworkError: Error {
case invalidURL
}
final class PhotoListViewModel: ObservableObject {
@Published var photoItems = [PhotoItemModel]()
struct PhotoItemModel: Codable, Identifiable {
let id: Int
let title: String
let url: String
let thumbnailUrl: String
}
@erdemildiz
erdemildiz / PhotoListView.swift
Created August 31, 2022 10:27
PhotoListView
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")
@erdemildiz
erdemildiz / async_await_syntax.swift
Created August 31, 2022 10:15
Async&Await Syntax
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 {
// ...
}
}
import UIKit
import AVFoundation
enum CameraSession: Error {
case inValidInput
case inValidOutput
}
final class CameraSessionViewController: UIViewController {
// Camera Setting