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

时间:2009-06-10 19:30:50

标签: objective-c cocoa macos filesystems

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

3 个答案:

答案 0 :(得分:5)

尝试FSPathMakeRef:

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

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

答案 1 :(得分:2)

您可以使用FSPathMakeRef()从UTF-8 C字符串路径制作FSRef,并且可以使用NSString的{​​{3}}方法获取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()

答案 2 :(得分:1)

你可以在Nathan Day的NSString + NDCarbonUtilities类别中使用这种方法:

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

请参阅http://homepage.mac.com/nathan_day/pages/source.xml的NDAlias了解更多信息(MIT许可)。