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?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
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.
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
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?
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
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?
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?
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
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 {
}
// ...
}
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.
}
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?
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.
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?
Suppose I have the following class:
class Some {
var list = [String]()
}
// In other places, I want to append to the list
someInstance.list.append("new string")
// ...but I do not want to re-assign the list itself:
someInstance.list = [String]()
What is the exact syntax for declaring list?
I am having coding design difficulties with Array in Swift, see this post. So I decided to turn to the old NSMutableArray.
I'd like to know if there are any known problems of this approach. Any pitfalls and known practices?