简体   繁体   English

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

[英]How do I create an FSRef from a NSString containing a path?

I have a file system path in a NSString, but I need an FSRef for the system call I will be making. 我在NSString中有一个文件系统路径,但是我将要进行的系统调用需要FSRef。 What is the best way to create the FSRef? 创建FSRef的最佳方法是什么?

Try FSPathMakeRef: 尝试FSPathMakeRef:

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

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

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: 您可以使用FSPathMakeRef()作出FSRef从UTF-8的C字符串路径,可以使用-UTF8String的方法NSString得到一个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 this method from Nathan Day in his an NSString+NDCarbonUtilities category: 您可以从Nathan Day的NSString + NDCarbonUtilities类别中使用此方法:

- (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). 有关更多信息,请参见http://homepage.mac.com/nathan_day/pages/source.xml上的 NDAlias(已获得MIT许可)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM