Post

Replies

Boosts

Views

Created

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
716
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
943
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
911
Nov ’23
Weird problem of NSTableView with auto layout
I have had this issue for a long time. If I configure any auto layout constraints in TableViewCell, I get extremely weird layout behavior in IB designer; however, layout is completely good during runtime. For example, with a completely new project and a single NSTableView on the main view, I get: If I resize main view, the tableview won't get resized Every time I reopen the project, the tableview would shrink by height. It seems the shrinked height is doubled every time. For example, in the following screenshot, the gap is 56. Next reopen will double the gap to 112. Is this a known bug? I would want to file bug report at https://feedbackassistant.apple.com.
1
1
643
Nov ’23
Is it safe to call low level Darwin function on FileHandle?
I have the following code: extension FileHandle { func readInto(_ buffer: inout [UInt8]) -> Int { buffer.withUnsafeMutableBytes { Darwin.read(fileDescriptor, $0.baseAddress, $0.count) } } } It can compile, but I wonder if this is supported since it's code in an app that is going to be submitted to App Store. The reason I don't use read(upToCount:) or readData(ofLength:) is that I am reading possibly very large files by small chunks and don't want to let Swift runtime allocate small buffers repeatedly.
0
0
307
Nov ’23
Data(contentsOf:) with huge file
I have a function that computes MD5 hash of a file: func ComputeMD5(ofFile path: String) -> [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), &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
PropertyListDecoder and .strings file
I have the following code: let file = "/path/to/en.lproj/Localizable.strings" let dec = PropertyListDecoder() var f: PropertyListSerialization.PropertyListFormat = .openStep do { //let data = strings.data(using: .utf8)! let data = try Data(contentsOf: URL(fileURLWithPath: file)) let list = try dec.decode([String: String].self, from: data, format: &f) print("foramt:", f.rawValue) list.forEach { print($0.key, $0.value) } } catch { print(error) } It seems PropertyListDecoder can correctly decode .strings file format; detected format is openStep (value is 1). But I am note sure because I couldn't find any docs on PropertyListDecoder about .strings file. Can anyone confirm this?
1
0
434
Oct ’23