如何从包含路径的 NSString 创建 FSRef?

发布于 2024-07-23 05:09:18 字数 71 浏览 0 评论 0原文

我在 NSString 中有一个文件系统路径,但我需要一个 FSRef 来进行系统调用。 创建 FSRef 的最佳方法是什么?

I have a file system path in a NSString, but I need an FSRef for the system call I will be making. What is the best way to create the FSRef?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相思碎 2024-07-30 05:09:18

尝试 FSPathMakeRef:

NSString *path = @"Some... path... here";
FSRef f;
OSStatus os_status = FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &f, NULL);

if (os_status == noErr) {
    NSLog(@"Success");
}

Try FSPathMakeRef:

NSString *path = @"Some... path... here";
FSRef f;
OSStatus os_status = FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &f, NULL);

if (os_status == noErr) {
    NSLog(@"Success");
}
白鸥掠海 2024-07-30 05:09:18

您可以使用 < code>FSPathMakeRef() 从 UTF-8 C 字符串路径创建 FSRef,并且您可以使用 -UTF8String< NSString 的 /a> 方法获取 UTF-8 C 字符串:

FSRef fsref;
Boolean isDirectory;
OSStatus result = FSPathMakeRef([myString UTF8String], &fsref, &isDirectory);
if(result < 0)
    // handle error
// If successful, fsref is valid, and isDirectory is true if the given path
// is a directory.  If you don't care about that, you can instead pass NULL
// for the third argument of FSPathMakeRef()

You can use FSPathMakeRef() to make an FSRef from a UTF-8 C string path, and you can use the -UTF8String method of NSString to get a UTF-8 C string:

FSRef fsref;
Boolean isDirectory;
OSStatus result = FSPathMakeRef([myString UTF8String], &fsref, &isDirectory);
if(result < 0)
    // handle error
// If successful, fsref is valid, and isDirectory is true if the given path
// is a directory.  If you don't care about that, you can instead pass NULL
// for the third argument of FSPathMakeRef()
2024-07-30 05:09:18

您可以在 Nathan Day 的 NSString+NDCarbonUtilities 类别中使用此方法:

- (BOOL)getFSRef:(FSRef *)aFSRef
{
    return FSPathMakeRef( (const UInt8 *)[self fileSystemRepresentation], aFSRef, NULL ) == noErr;
}

请参阅 NDalias http ://homepage.mac.com/nathan_day/pages/source.xml 了解更多信息(麻省理工学院许可)。

You can use this method from Nathan Day in his an NSString+NDCarbonUtilities category:

- (BOOL)getFSRef:(FSRef *)aFSRef
{
    return FSPathMakeRef( (const UInt8 *)[self fileSystemRepresentation], aFSRef, NULL ) == noErr;
}

See NDAlias at http://homepage.mac.com/nathan_day/pages/source.xml for more (MIT Licensed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文