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)
}
Post
Replies
Boosts
Views
Activity
I want to optimize file reading performance. I believe DispatchIO is the solution. Can anyone give some good pointers?
Does Swift provide such shorthand/sugar syntax for commonly used thread synchronization?
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?
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?
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?
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?
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
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?
I vaguely remember I came across some classes about file packages. Just cannot recall the exact names. Can anyone help?
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.
Does Swift support this? Til now my understanding is that reflection only works with public members. Is it possible to get private/static members of a type?
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?
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?
In C++, I can write 123457890ull to imply it's an unsigned long long integer. Does Swift provide similar language construct?