iOS
protokols SomeProtocol { func test() } func test1_1(uzdevums: SomeProtocol) {} func test1_2(uzdevums: any SomeProtocol) {} func test2_1(uzdevums: some SomeProtocol) {} func test2_2<Uzdevums: SomeProtocol>(uzdevums: Uzdevums) {} class A { func test2_2<Uzdevums: SomeProtocol>(uzdevums: Uzdevums) { } } class B: SomeProtocol { func test() {} } class C: B {} A().test2_2(uzdevums: B())
noslēdzošā klase 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) } } noslēdzošā klase 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()
Ja īstenojat jaunu ekrānu vai jaunu funkciju, vai uzreiz rakstāt SwiftUI, vai ir diskusijas par izvēli starp UIKit un SwiftUI?
Vai pēdējā WWDC bija interesantas sesijas/rāmji, kuras vēlētos izmēģināt savā darbā?
protokols 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] = [:] func setValue(_ value: T?, forKey key: String) { storage[key] = value } func getValue(forKey key: String) -> T? { return storage[key] } func fetchValue(forKey key: String, completion: @escaping (T?) -> Void) { let value = storage[key] completion(value) } } import XCTest final class 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) } } CacheTests.defaultTestSuite.run()
Pastāsti par noderīgiem Property Wrapper'iem, kurus izmantoji projektā
Izskaidrojiet atmiņā esošās kešatmiņas un uz diska esošās kešatmiņas atšķirību.
// Rakstiet `data` uz failu ar nosaukumu `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) } // Noņemiet failu ar nosaukumu `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) } // Lasiet `Data` no faila ar nosaukumu `file`. Ja fails neeksistē, atgriež `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) { } }
Pastāsti par SwiftUI izmantošanu projektā — kāda ir politika, cik ekrāni ir uzrakstīti SwiftUI
Kā jūs prioritizētu identificētās problēmas, ja projekts jāizlaiž ražošanā stundas laikā?
Kāpēc jūs izveidojat atsevišķas load un getLastCities metodes, un kas būtu jādara, ja getLastCities tiek izsaukta pirms datu ielādes?
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) } } }
import UIKit protokols 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) }
Kā jūs saprotat strukturētu konkurenci Swift? Vai Task.detached atbilst šai jēdzienam?
Kāpēc izmantot galīgo klasi vietā klases kešatmiņas ieviešanai?
// Pakalpojums, kas nosūta pieprasījumus (get/post), url, pieprasījuma veidošana atkarībā no semantikas (decode/encode)
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)