iOS
Jeśli implementujesz nowy ekran lub nową funkcję, od razu piszesz na SwiftUI, czy zdarzają się dyskusje na temat wyboru między UIKit a SwiftUI?
Opowiedz o przydatnych Property Wrapper'ach, których używałeś w projekcie
Opowiedz więcej o swoim ostatnim projekcie — module galerii zdjęć na Avito, za który odpowiadałeś (krótkoterminowy wynajem, formularz rezerwacji itp.)
Self -> Zadanie -> (silny) Self .... Wstecz -> View desinit -> View model deinit?
Dlaczego wybrałeś słownik jako pamięć podręczną?
klasa final Cache<T>: CacheProtocol { func getValue(forKey key: String) -> T? { return storage[key] } func fetchValue(forKey key: String, completion: @escaping (T?) -> Void) { let value = storage[key] completion(value) } } finalna klasa CacheTests: XCTestCase { override func setUp() { super.setUp() } func testSetAndGetValue() { let cache = Cache<String>() let key: String = "key" let value: String = "value" cache.setValue(value, forKey: key) XCTAssertEqual(cache.getValue(forKey: key), value) cache.setValue(nil, forKey: key) XCTAssertTrue(cache.getValue(forKey: key) == nil) } func testCompletion() { } } CacheTests.defaultTestSuite.run()
Jak rozumiesz termin strukturalnej współbieżności w Swift? Czy Task.detached pasuje do tego pojęcia?
import UIKit protocol CacheProtocol { associatedtype Value func setValue(_ value: Value?, forKey key: String) func getValue(forKey key: String) -> Value? func fetchValue(forKey key: String, completion: @escaping (Value?) -> Void) } final class Cache<T>: CacheProtocol { private var storage: [String: T] = [:] private let queue = DispatchQueue(label: "my.queue", attributes: .concurrent) func setValue(_ value: T?, forKey key: String) { queue.sync(flags: .barrier) { storage[key] = value } } func getValue(forKey key: String) -> T? { queue.sync { return storage[key] } } func fetchValue(forKey key: String, completion: @escaping (T?) -> Void) { queue.async { let value = self.storage[key] completion(value) } } }
Dlaczego utworzyłeś oddzielne metody load i getLastCities, i co powinno się stać, jeśli getLastCities zostanie wywołane przed załadowaniem danych?
Na ile narzędzia AI (Cursor, Proxyman itp.) pomagają ci być bardziej produktywnym w pracy?
import UIKit protokół CacheProtocol { associatedtype Value func setValue(_ value: Value?, forKey key: String) func value(forKey key: String) -> Value? func fetchValue(forKey key: String, completion: @escaping (Value?) -> Void) }
Wyjaśnij różnicę między pamięciową pamięcią podręczną a pamięcią podręczną na dysku.
// Zapisuje `data` do pliku o nazwie `file`. func writeData(_ data: Data, to file: String) { let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let fileURL = documentDirectoryURL.appendingPathComponent(file) try? data.write(to: fileURL) } // Usuwa plik o nazwie `file`. func remove(file: String) { let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let fileURL = documentDirectoryURL.appendingPathComponent(file) try? FileManager.default.removeItem(at: fileURL) } // Odczytuje `Data` z pliku o nazwie `file`. Jeśli plik nie istnieje, zwraca `nil`. func readData(from file: String) -> Data? { let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let fileURL = documentDirectoryURL.appendingPathComponent(file) return try? Data(contentsOf: fileURL) } final class DiskCache<T>: CacheProtocol { func setValue(_ value: T?, forKey key: String) { } func getValue(forKey key: String) -> T? { } func fetchValue(forKey key: String, completion: @escaping (T?) -> Void) { } }
Jak byś priorytetyzował zidentyfikowane problemy, jeśli projekt musi trafić do produkcji za godzinę?
Dlaczego używać klasy finalnej zamiast klasy do implementacji pamięci podręcznej?
enum HTTPMethod: String { case post = "POST" } enum Errors: Error { case invalidUrl case invalidQuery case invalidResponse case httpStatus(Int) } protocol MyDecoder { func decode<T: Decodable>(_: _ type: T.Type, from data: Data) throws -> T } final class NetworkService { private let session: URLSession private let encoder = JSONEncoder() private let decoder: MyDecoder init( session: URLSession = .shared, decoder: MyDecoder ) { self.session = session self.decoder = decoder } func request<Response: Decodable, Body: Encodable>( urlString: String, method: HTTPMethod, body: Body? = nil, headers: [String: String] = [:], query: [String: String] = [:] ) async throws -> Response { guard var components = URLComponents(string: urlString) else { throw Errors.invalidUrl } if !query.isEmpty { components.queryItems = query.map { URLQueryItem(name: $0.key, value: $0.value) } } guard let url = components.url else { throw Errors.invalidQuery } var request = URLRequest(url: url)
// Serwis wysyłający zapytania (get/post), url, tworzenie zapytania w zależności od semantyki (decode/encode)