Sobes.tech

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())

Senior
Salmon
35

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()

Middle+
RevolutRevolut
35

Ja īstenojat jaunu ekrānu vai jaunu funkciju, vai uzreiz rakstāt SwiftUI, vai ir diskusijas par izvēli starp UIKit un SwiftUI?

Senior
Salmon
34

Vai pēdējā WWDC bija interesantas sesijas/rāmji, kuras vēlētos izmēģināt savā darbā?

Senior
Salmon
34

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()

Middle+
RevolutRevolut
33

Pastāsti par noderīgiem Property Wrapper'iem, kurus izmantoji projektā

Senior
Salmon
33

Izskaidrojiet atmiņā esošās kešatmiņas un uz diska esošās kešatmiņas atšķirību.

Middle+
RevolutRevolut
32

// 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) { } }

Middle+
RevolutRevolut
32

Pastāsti par SwiftUI izmantošanu projektā — kāda ir politika, cik ekrāni ir uzrakstīti SwiftUI

Senior
Salmon
31

Kā jūs prioritizētu identificētās problēmas, ja projekts jāizlaiž ražošanā stundas laikā?

Senior
Uzum
29

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?

Senior
Uzum
28

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) } } }

Middle+
RevolutRevolut
28

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) }

Middle+
RevolutRevolut
28

Kā jūs saprotat strukturētu konkurenci Swift? Vai Task.detached atbilst šai jēdzienam?

Senior
Salmon
27

Kāpēc izmantot galīgo klasi vietā klases kešatmiņas ieviešanai?

Middle+
RevolutRevolut
20

// Pakalpojums, kas nosūta pieprasījumus (get/post), url, pieprasījuma veidošana atkarībā no semantikas (decode/encode)

Middle+
СБЕРСБЕР
11

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)

Middle+
СБЕРСБЕР
9
/206