Post

Replies

Boosts

Views

Activity

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
404
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
318
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
515
Dec ’23
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
560
Dec ’23
Environment variable not working in another user account
I have the following in my .zshrc: export MY_LIBRARY_DIR=~/bin In Xcode I can set header/lib search path using something like $(MY_LIBRARY_DIR)/abc. This works fine in my daily used user account. But today I found that this technique does not work in a test user account (for testing purpose only). I even reboot my machine but still can't get it working. Am I missing something very obvious??? BTW, I am using Xcode 14.2 and 14.3.1.
1
0
370
Dec ’23
How read image file metadata?
I want to read metadata of image files such as copyright, author etc. I did a web search and the closest thing is CGImageSourceCopyPropertiesAtIndex: - (void)tableViewSelectionDidChange:(NSNotification *)notif { NSDictionary* metadata = [[NSDictionary alloc] init]; //get selected item NSString* rowData = [fileList objectAtIndex:[tblFileList selectedRow]]; //set path to file selected NSString* filePath = [NSString stringWithFormat:@"%@/%@", objPath, rowData]; //declare a file manager NSFileManager* fileManager = [[NSFileManager alloc] init]; //check to see if the file exists if ([fileManager fileExistsAtPath:filePath] == YES) { //escape all the garbage in the string NSString *percentEscapedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)filePath, NULL, NULL, kCFStringEncodingUTF8); //convert path to NSURL NSURL* filePathURL = [[NSURL alloc] initFileURLWithPath:percentEscapedString]; NSError* error; NSLog(@"%@", [filePathURL checkResourceIsReachableAndReturnError:error]); //declare a cg source reference CGImageSourceRef sourceRef; //set the cg source references to the image by passign its url path sourceRef = CGImageSourceCreateWithURL((CFURLRef)filePathURL, NULL); //set a dictionary with the image metadata from the source reference metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(sourceRef,0,NULL); NSLog(@"%@", metadata); [filePathURL release]; } else { [self showAlert:@"I cannot find this file."]; } [fileManager release]; } Is there any better or easy approach than this?
1
0
562
Dec ’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
448
Nov ’23