Post

Replies

Boosts

Views

Created

How extract files in Assets.car?
I got a few answers from SO, but the tools are too old and they don't even run my macOS (12.6). Is there any official way to extract an Assets.car file? The reason I ask this question is that I want to re-use the icons for strings/storyboard files in Xcode packaged Assets.car (if it's legal).
0
0
980
Sep ’23
How throw error in willSet
I have the following code: public var endpoint: String! { willSet { if newValue == nil { throw ErrorCode.NullValue("endpoint") } } } But compiler gives me error: Error is not handled because the enclosing function is not declared 'throws'
3
0
761
Aug ’23
Does Swift support stored property with computed value?
This may sound strange, but I encounter real world need on this. private var queryItems_: [URLQueryItem]? private var queryItems: [URLQueryItem]? { get { if queryItems_ == nil { if !queries.isEmpty { queryItems_ = queries.map { (key: String, value: String?) in return URLQueryItem(name: key, value: value) } } } return queryItems_ } } /// Query strings public private(set) lazy var queries = [String: String?]() { didSet { queryItems_ = nil } } The queryItems will be (re)created on get if queries property was changed. What I wish is that I could use queryItems as a simple var property but let me do my logic in its getter. Is this supported already?
1
0
328
Aug ’23
String interpolation produces a debug description for an optional value; did you mean to make this explicit?
This makes my head dizzy! Help me out of this peril. How to avoid this with my own class objects? let obj: LanguageItem? = LanguageItem(language: "en") print("object: \(obj)") struct LanguageItem: Codable { var language: String var name: String? } extension LanguageItem: CustomStringConvertible { var description: String { "Lang:\(language) name:\(name ?? "(none)")" } } The print statement still prints "Optional(Lang:en name:(none))". How to get rid the Optional prefix?
2
0
544
Aug ’23
Does Swift has a string builder class?
In other languages, I usually have a StringBuilder class that provides the functionality to concatenate strings in an efficient way. // pseudo code let sb = StringBuilder() sb.append("text") sb.appendFormat("name=%@", name) I am aware of @resultBuilder, but does Swift provide a builtin construct?
2
0
2.3k
Aug ’23
JSONEncoder with dictionary of objects?
I have a need to wrap several kinds of objects into a dictionary say [String: Any?], but how do I tell that the Any? object is all Encodable? let params: [String: Any?] = ["num": 123, "text": "abc", "obj": encodableobject] JSONEncoder().encode(params) // compiler error because Any? is not Encodable
1
0
718
Aug ’23
How make SomeClass<T> Decodable?
I am trying to encode/decode JSON data, and I have the following code: struct SomeOrdinaryClass: Decodable { // ... } struct SomeBox<T>: Decodable { var data: T? } But Xcode gives me the following error: myfile.swift:16:8 Type 'SomeBox' does not conform to protocol 'Decodable' Is there anyway to overcome this?
1
0
375
Aug ’23
Not able to reference classes/structs in Swift playground sources
I have to admit that this is strange for me. Though I have been using playgrounds for years, but I only write small pieces of code to test simple ideas, and I never used another source file. Today I want to add a new struct in Sources folder. To my surprise, I am not able to reference the struct in the main playground file. Sources/testlets.swift: struct Dummy { var name: String } MyPlayground: var box = Dummy(name: "abc") // error: /.../MyPlayground.playground:22:11 Cannot find 'Dummy' in scope
2
0
913
Aug ’23