Post

Replies

Boosts

Views

Activity

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
402
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
737
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
667
Nov ’23
What should I do with UTI (NSImage.imageTypes)?
Per the docs, NSImage.imageTypes returns a list UTI's, something like below: com.adobe.pdf com.apple.pict com.adobe.encapsulated-postscript public.jpeg public.png com.compuserve.gif com.canon.tif-raw-image ... What I need is get file extensions of a UTI. For example, public.jpeg picture file may have several file extensions, say .jpg,.jpeg,.jfif. Does Cocoa provide any API to query for this information?
2
0
911
Dec ’23