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?
Post
Replies
Boosts
Views
Activity
I have a checkbox button in a table column (of course in a NSTableCellView).
@IBAction func check_click(_ sender: Any) {
// How do I know in which row this event occurred?
// Once I get the row index I get the associated data item so that I can update the checked state.
}
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.
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 {
}
// ...
}
Can anyone give any clues about how the navigation bar is implemented like below?
I have a NSTableView which shows a list of translated text items. I want to provide a Find Bar for user to filter through the items, like the one I have in Xcode.
Of course I don't want so many features; I only want to let user enter some text.
I want to expand the first only root node when NSOutlineView is finished loading all data.
How to get notified in code (possibly by some delegate function)?
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).
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'
I could not find any standard error classes/enums in docs. For example, I have the following error situations:
Invalid null parameter
Parameter value out of range
Property value (of T!) not set
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?
I have a simple test project which has a framework bundle (as a target). See attached screenshots.
When I import MyFramework and use classes from the framework, the app compiles fine but got linker errors. It seems Xcode does not automatically link the bundle into the app.
The following code compiles fine in a normal Swift app, but not in the accompanying unit test bundle.
I don't have any idea why.
import Foundation
struct Dummy: NSObject { // Error: Inheritance from non-protocol type 'NSObject'
var name: String?
}
I have the following code:
let obj: LanguageItem? = LanguageItem(language: "zh-CN")
// Argument type 'LanguageItem?' does not conform to expected type 'CVarArg'
print(String(format:"obj: %@", obj))
struct LanguageItem: Codable
{
var language: String
var name: String?
}
How can I make my class work with String(format:)?
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?