Post

Replies

Boosts

Views

Activity

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
930
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
561
Aug ’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
429
Sep ’23
FileMerge won't run on macOS 12.7
I am still on Xcode 14.3 and my macOS is version 12.7 (21G816). Today I am surprised to find out that FileMerge tool won't run when I invoke it from Xcode "Open Developer Tool" menu. Is there a standalone download for this tool? Or is there any better alternatives to it?
2
0
764
Oct ’23
Data(contentsOf:) with huge file
I have a function that computes MD5 hash of a file: func ComputeMD5(ofFile path: String) -&gt; [UInt8]? { if let data = try? Data(contentsOf: URL(fileURLWithPath: path)) { var digest = [UInt8](repeating: 0, count: 16) data.withUnsafeBytes { _ = CC_MD5($0.baseAddress, UInt32(data.count), &amp;digest) } return digest } return nil } Now I wonder/worry what happens if the file is very huge. Does the runtime perform disk memory paging?
2
1
702
Nov ’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
Replies
2
Boosts
0
Views
930
Activity
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?
Replies
2
Boosts
0
Views
2.3k
Activity
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?
Replies
2
Boosts
0
Views
561
Activity
Aug ’23
Argument type 'LanguageItem?' does not conform to expected type 'CVarArg'
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:)?
Replies
2
Boosts
0
Views
502
Activity
Aug ’23
Linker errors in Swift app with a framework bundle
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.
Replies
2
Boosts
0
Views
574
Activity
Aug ’23
Does Swift provide conventional standard error classes/enums?
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
Replies
2
Boosts
0
Views
527
Activity
Aug ’23
Is there any builtin Text Find Bar?
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.
Replies
2
Boosts
0
Views
621
Activity
Sep ’23
Get row index when user click a button in NSTableCellView
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. }
Replies
2
Boosts
0
Views
561
Activity
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?
Replies
2
Boosts
0
Views
429
Activity
Sep ’23
How make a var modifiable but not assignable?
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?
Replies
2
Boosts
0
Views
436
Activity
Oct ’23
Need advices on NSMutableArray in Swift
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?
Replies
2
Boosts
0
Views
441
Activity
Oct ’23
Is it possible to get current executing function's name?
In other languages, I am able to get current function's name using some kind of so-called reflection API. Does Swift provide similar API?
Replies
2
Boosts
0
Views
456
Activity
Oct ’23
FileMerge won't run on macOS 12.7
I am still on Xcode 14.3 and my macOS is version 12.7 (21G816). Today I am surprised to find out that FileMerge tool won't run when I invoke it from Xcode "Open Developer Tool" menu. Is there a standalone download for this tool? Or is there any better alternatives to it?
Replies
2
Boosts
0
Views
764
Activity
Oct ’23
Data(contentsOf:) with huge file
I have a function that computes MD5 hash of a file: func ComputeMD5(ofFile path: String) -&gt; [UInt8]? { if let data = try? Data(contentsOf: URL(fileURLWithPath: path)) { var digest = [UInt8](repeating: 0, count: 16) data.withUnsafeBytes { _ = CC_MD5($0.baseAddress, UInt32(data.count), &amp;digest) } return digest } return nil } Now I wonder/worry what happens if the file is very huge. Does the runtime perform disk memory paging?
Replies
2
Boosts
1
Views
702
Activity
Nov ’23
Common audio/image file extensions on macOS
I have a need to list all known audio/image file types in a planned app. What I have known so far: images .apng .avi, .avif .gif .jpg, .jpeg, .jfif, .pjpeg, .pjp .png .svg .webp audio .aif .cda .mid, .midi .mp3 .mpa .ogg .wav .wma What are the missing ones?
Replies
2
Boosts
0
Views
910
Activity
Dec ’23