Post

Replies

Boosts

Views

Activity

Extension method for Optional of String
I came across an article about extension of Optional (https://useyourloaf.com/blog/empty-strings-in-swift/), but I cannot get it to work for the Optional extension. extension Optional where Wrapped == String { var isBlank: Bool { return self?.isBlank ?? true } } func testIsBlank() { for s in [nil, "", " ", "\t", "abc"] { print(s?.isBlank) } } For nil value, the output is still nil. I see the article is quite old dated back in 2019. There must be some changes in the language specs. What's going on here?
1
0
488
Sep ’23
Ensure value in a Dictionary
In many cases, I need to get the value from a dictionary given a key (usually a string). I have the following helper class: public class ObjectCache<T> { private var cache = [String: T]() subscript(name: String) -> T? { get { return cache[name] } set { cache[name] = newValue } } func get(_ name: String, with builder: () -> T?) -> T? { var obj = cache[name] if obj == nil { obj = builder() cache[name] = obj } return obj } } This saves much keyboard typing and avoid common errors of oversight. Like below: let serviceURL = self.urlCache.get(name) { return comp.url }! Now my question is - Does Swift provide some builtin functionality like this? I just hope I did not re-event the wheel.
1
0
289
Sep ’23
switch case and === operator
I have the following code: func numberOfRows(in tableView: NSTableView) -> Int { switch tableView { case self.stringsTable: return self.stringsList?.count ?? 0 case self.localeTable: return self.localeMap.count default: print("numberOfRows not handled for \(tableView)") return 0 } } I wonder if there is any (performance) difference between case and ==== operator like below: func numberOfRows(in tableView: NSTableView) -> Int { if tableView === self.stringsTable { } // ... }
3
0
372
Sep ’23
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
642
Sep ’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
268
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
409
Aug ’23