Post

Replies

Boosts

Views

Activity

@escaping vs @nonescaping closures
I know this is a frequently asked question, but... After reading many articles on the net, I am still not sure if I have a solid understanding of the logics behind the design. One of my puzzles around this question is - Is the Swift compiler clever enough so that I can solely rely on its warnings/errors?
1
0
786
Jan ’24
FileManager.enumerator and URL problem
I have the following pseudo code: func load(at packageURL: URL) { let realPackageURL = packageURL.resolvingSymlinksInPath() guard let it = fileMan.enumerator(at: realPackageURL) for case let fileURL as URL in it { print(fileURL) // Get filename relative to package root directory let relativeFileName = String(filePath.suffix(filePath.count - packagePath.count)) } } When called with "file:///tmp/some.app", the enumerated fileURL is actually file:///private/tmp/GIMP.app/Contents/ packageURL.resolvingSymlinksInPath() actually does nothing, I assume /tmp is a hard link. This makes it impossible to get a correct relative path. Is there any remedy for this?
2
0
647
Jan ’24
Reading multi parts of a file concurrently
I have a need to read first half and second half of a file concurrently. Is there any best practices for this scenario? BTW, I did research on DispatchIO but it turned out DispatchIO is all about asynchronous operations on a single file handle; it cannot perform parallel reading. // naive approach Task { fileHandle.read(into:buffer) } Task { // seek to file size / 2 fileHandle.read(into:buffer) }
5
0
1.1k
Jan ’24
How detect cyclic symbolic links using NSFileManager?
My code is crashing Xcode (or even macOS kernel) during debugging - Xcode just vanishes from screen! // pseudo code public func hunt(in directory: URL) { let fileIterator = fileMan.enumerator(at: directory) // collect app packages into a list var packages = [URL]() for case let fileURL as URL in fileIterator { if fileURL.pathExtension == "app" { packages.append(fileURL) } } // FileWrappers var wrappers = [FileWrappers]() for packageURL in packages { //!!! The line below eventually crashes Xcode (or even macOS kernel once)! wrappers[packageURL] = try? FileWrapper(url: packageURL, options: .immediate) // NOTE: I need FileWrapper.matchesContents later in some code } } // unit test case func test() {} myObj.hunt(in: URL(fileURLWithPath: "/Applications")) } I suspect that the FileWrapper constructor is traversing directories and encounter cyclic symbolic links and eventually it crashes; since it's running at system runtime level, most probably it also crashes macOS kernel! So my question is that is there any way to detect cyclic symbolic links so that I can design my own logics similar to FileWrapper?
2
0
561
Jan ’24
GridView addRow height problem
I am trying to add rows to GridView and not able to get expected row height correctly. override func viewDidLoad() { super.viewDidLoad() // Remove the row in IB designer gridView.removeRow(at: 0) let image = NSImage(named: NSImage.colorPanelName)! //image.size = NSMakeSize(80, 80) let imageView = NSImageView(image: image) imageView.imageFrameStyle = .grayBezel imageView.imageScaling = .scaleAxesIndependently imageView.frame = NSMakeRect(0, 0, 80, 80) let label = NSTextField(labelWithString: "test text") let row = gridView.addRow(with: [imageView, label]) row.height = 80 } What was wrong with my code?
5
0
936
Jan ’24
Where is help on Swift documentation markup?
I am reluctant to admit that I only came to know that Swift provides a builtin documentation markup syntax just a few months ago. /** Test func Some description here. - Parameters: - b:Test - d: Test - f: Test - Returns: Bool */ func myMethod(a b:Int, c d:Int, e f:Int) -> Bool { b > d } It seems the markup is pretty simple and has only a few keywords. But, I want to read through the complete reference. Any useful pointers?
2
0
1k
Dec ’23
Default value of template T
Does Swift has the syntax that lets me get the default value of a template type T, like below: struct VarLock<T> { private var v = default(T) // or T() }
Replies
2
Boosts
0
Views
728
Activity
Jan ’24
@escaping vs @nonescaping closures
I know this is a frequently asked question, but... After reading many articles on the net, I am still not sure if I have a solid understanding of the logics behind the design. One of my puzzles around this question is - Is the Swift compiler clever enough so that I can solely rely on its warnings/errors?
Replies
1
Boosts
0
Views
786
Activity
Jan ’24
Any new URL for macappstore://itunes.apple.com/app/idxxx
I have used URL like "macappstore://itunes.apple.com/app/idxxx" in my apps for several years. Just curious to know if there is any update from this URL scheme? I believe iTunes is not app specific any more (or I am wrong).
Replies
0
Boosts
0
Views
515
Activity
Jan ’24
Dynamic access of unnamed tuple fields
Is there any way to get an unnamed property of a tuple given its position? Like below: let record = ("field1", "field2") func getRecordFieldValue(at: Int, of record: (String, String)) -> Any? { // pseudo code record.[at] }
Replies
2
Boosts
0
Views
927
Activity
Jan ’24
How turn my function into async pattern
Suppose I have the following function: func doWork(_ someValue: Int, completionHandler: () -> Void) { let q = DispatchQueue() q.async { // Long time of work completionHandler() } } How do I turn it into async function so that I can call it using await doWork()? Are there guidelines/principles/practices for this purpose?
Replies
2
Boosts
0
Views
1.2k
Activity
Jan ’24
FileManager.enumerator and URL problem
I have the following pseudo code: func load(at packageURL: URL) { let realPackageURL = packageURL.resolvingSymlinksInPath() guard let it = fileMan.enumerator(at: realPackageURL) for case let fileURL as URL in it { print(fileURL) // Get filename relative to package root directory let relativeFileName = String(filePath.suffix(filePath.count - packagePath.count)) } } When called with "file:///tmp/some.app", the enumerated fileURL is actually file:///private/tmp/GIMP.app/Contents/ packageURL.resolvingSymlinksInPath() actually does nothing, I assume /tmp is a hard link. This makes it impossible to get a correct relative path. Is there any remedy for this?
Replies
2
Boosts
0
Views
647
Activity
Jan ’24
Reading multi parts of a file concurrently
I have a need to read first half and second half of a file concurrently. Is there any best practices for this scenario? BTW, I did research on DispatchIO but it turned out DispatchIO is all about asynchronous operations on a single file handle; it cannot perform parallel reading. // naive approach Task { fileHandle.read(into:buffer) } Task { // seek to file size / 2 fileHandle.read(into:buffer) }
Replies
5
Boosts
0
Views
1.1k
Activity
Jan ’24
Need example code about DispatchIO
I want to optimize file reading performance. I believe DispatchIO is the solution. Can anyone give some good pointers?
Replies
1
Boosts
0
Views
780
Activity
Jan ’24
Does Swift provide something like @synchronized in objc?
Does Swift provide such shorthand/sugar syntax for commonly used thread synchronization?
Replies
1
Boosts
0
Views
959
Activity
Jan ’24
How detect cyclic symbolic links using NSFileManager?
My code is crashing Xcode (or even macOS kernel) during debugging - Xcode just vanishes from screen! // pseudo code public func hunt(in directory: URL) { let fileIterator = fileMan.enumerator(at: directory) // collect app packages into a list var packages = [URL]() for case let fileURL as URL in fileIterator { if fileURL.pathExtension == "app" { packages.append(fileURL) } } // FileWrappers var wrappers = [FileWrappers]() for packageURL in packages { //!!! The line below eventually crashes Xcode (or even macOS kernel once)! wrappers[packageURL] = try? FileWrapper(url: packageURL, options: .immediate) // NOTE: I need FileWrapper.matchesContents later in some code } } // unit test case func test() {} myObj.hunt(in: URL(fileURLWithPath: "/Applications")) } I suspect that the FileWrapper constructor is traversing directories and encounter cyclic symbolic links and eventually it crashes; since it's running at system runtime level, most probably it also crashes macOS kernel! So my question is that is there any way to detect cyclic symbolic links so that I can design my own logics similar to FileWrapper?
Replies
2
Boosts
0
Views
561
Activity
Jan ’24
Weird source code problem of a specific file
After upgrading to Xcode 15.1 on Sonoma, I get a very weird problem in an on-going project. This source file has no syntax coloring. No matter how I try "Open As->Source Code" it just won't work. All other files (sources/resources) work fine. Is this a known bug? Is there any way to get syntax coloring back?
Replies
1
Boosts
0
Views
448
Activity
Jan ’24
GridView addRow height problem
I am trying to add rows to GridView and not able to get expected row height correctly. override func viewDidLoad() { super.viewDidLoad() // Remove the row in IB designer gridView.removeRow(at: 0) let image = NSImage(named: NSImage.colorPanelName)! //image.size = NSMakeSize(80, 80) let imageView = NSImageView(image: image) imageView.imageFrameStyle = .grayBezel imageView.imageScaling = .scaleAxesIndependently imageView.frame = NSMakeRect(0, 0, 80, 80) let label = NSTextField(labelWithString: "test text") let row = gridView.addRow(with: [imageView, label]) row.height = 80 } What was wrong with my code?
Replies
5
Boosts
0
Views
936
Activity
Jan ’24
How display shortened path in a label?
I have a single-line label whose purpose is display file path, possibly very long. Is there any way to shorten/compact the path string (with ellipse ...) so that the label still displays full path even it's too long? Like below: /some/very/long/path/to/some/filename.txt to /some/.../filename.txt
Replies
4
Boosts
0
Views
783
Activity
Jan ’24
Is there any way to tell if a file is on SSD volume?
I have a need to optimize reading strategy, based on if the file is on hard disk or SSD. Does macOS provide any low-level API so that I can query such information?
Replies
2
Boosts
0
Views
609
Activity
Jan ’24
Where is help on Swift documentation markup?
I am reluctant to admit that I only came to know that Swift provides a builtin documentation markup syntax just a few months ago. /** Test func Some description here. - Parameters: - b:Test - d: Test - f: Test - Returns: Bool */ func myMethod(a b:Int, c d:Int, e f:Int) -> Bool { b > d } It seems the markup is pretty simple and has only a few keywords. But, I want to read through the complete reference. Any useful pointers?
Replies
2
Boosts
0
Views
1k
Activity
Dec ’23