Post

Replies

Boosts

Views

Activity

How to write a wrapper around CFSomeRef
I am testing with FSEventStreamCreate which returns an FSEventStreamRef, but I cannot find an equivalent toll-free class in Cocoa. In order to free-up resources used by this Ref, I need to do following: FSEventStreamStop(stream); FSEventStreamInvalidate(stream); FSEventStreamRelease(stream); That is quite error-prone and tedious. So I want to write a wrapper class, but don't have any idea on when or where to release the Ref. Is it correct to do 'free' in dealloc?
1
0
1k
Apr ’23
libarclite_macosx not found when building a framework project
Not sure if it's specific to me only. I tried to build a framework project using Xcode 14.3.1 RC but got a strange error: File not found: /Users/USERNAME/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_macosx.a The project was created several years ago and was upgraded all the way to Xcode 14.2. I remember last time I posted a question about getting an empty xcarchive when doing archive build. The remedy is quite simple - setting SKIP_INSTALL to no. I wonder if this problem also has a simple remedy.
1
0
2.4k
May ’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
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
377
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
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
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
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
463
Sep ’23
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
643
Sep ’23
Symbol navigator is polluted with thousands of system classes!
I have a test app. I added some extension classes to one of source code like below: extension String { func addPathComponent(_ path: String) -> String { return (self as NSString).appendingPathComponent(path) } func sameText(with: String) -> Bool { return self.caseInsensitiveCompare(with) == .orderedSame } var isBlank: Bool { allSatisfy { $0.isWhitespace } } } extension Optional where Wrapped == String { var isBlank: Bool { self?.isBlank ?? true } } Now the symbol navigator is polluted with many system classes: BTW, I am using Xcode 14.3.1.
1
0
610
Sep ’23
Any way to get array by reference?
Today I spent one hour to get myself educated on Array type. I have the following class in one of my app: class PathNode: Hashable, Comparable, CustomStringConvertible { var name: String! var path: String! var children: [PathNode]? static func == (lhs: PathNode, rhs: PathNode) -> Bool { lhs.name == rhs.name } static func < (lhs: PathNode, rhs: PathNode) -> Bool { lhs.name < rhs.name } func hash(into hasher: inout Hasher) { hasher.combine(name) hasher.combine(children) } /// Sort child nodes. func sort() { if let children = self.children { children.sort() for child in children { child.sort() } } } // other members... } The problem is in the sort function. I found out in my outline view the result is not sorted even though I did call sort on the root node. After about one hour's frustration, I came to realize that I forgot one import fact about the array type in Swift - it's a value type! I have to adjust sort function to the following code: /// Sort child nodes. func sort() { if self.children != nil { self.children!.sort() for child in self.children! { child.sort() } } } That's not an elegant way of writing code! Is there any other way to get a 'reference' to an array in Swift?
2
0
404
Sep ’23