iOS
protokol SomeProtocol { func test() } func test1_1(qiymat: SomeProtocol) {} func test1_2(qiymat: any SomeProtocol) {} func test2_1(qiymat: some SomeProtocol) {} func test2_2<Qiymat: SomeProtocol>(qiymat: Qiymat) {} class A { func test2_2<Qiymat: SomeProtocol>(qiymat: Qiymat) { } } class B: SomeProtocol { func test() {} } class C: B {} A().test2_2(qiymat: B())
So'nggi WWDCda qiziqarli sessiyalar yoki freymvorklar bo'ldimi va ularni ishda sinab ko'rishni xohlaysizmi?
oxirgi sinf 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) } } oxirgi sinf 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()
Xotira ichidagi kesh bilan diskda saqlangan kesh o'rtasidagi farqni tushuntiring.
Yangi ekran yoki yangi funksiyani amalga oshirayotganingizda, darhol SwiftUI da yozasizmi, yoki UIKit va SwiftUI o'rtasida tanlov bo'yicha muhokamalar bo'ladimi?
Loyihada ishlatgan foydali Property Wrapper'lar haqida gapir
protokol 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()
// `data`ni `file` nomli faylga yozadi. 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) } // `file` nomli faylni o‘chiradi. 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) } // `file` nomli fayldan `Data` o‘qiydi. Fayl mavjud bo‘lmasa, `nil` qaytaradi. 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) { } }
Loyihada SwiftUI dan foydalanish haqida gapir — siyosat nima, nechta ekran SwiftUI da yozilgan
Agar loyiha bir soat ichida ishlab chiqarishga chiqishi kerak bo'lsa, aniqlangan muammolarni qanday ustuvorlik bilan belgilaysiz?
import UIKit protokol 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) }
Nima uchun siz alohida load va getLastCities metodlarini yaratdingiz va agar getLastCities ma'lumotlar yuklanishidan oldin chaqirilsa, nima bo'lishi kerak?
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) } } }
Swiftda tuzilgan hamkorlik terminini qanday tushunasiz? Task.detached bu tushunchaga mos keladimi?
Keshni amalga oshirish uchun nima uchun final sinfdan foydalaniladi, sinf o'rniga?
// So'rovlarni yuboradigan xizmat (get/post), url, semantikaga qarab so'rovni shakllantirish (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)