Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何从包含路径的NSString创建FSRef?_Objective C_Cocoa_Macos_Filesystems - Fatal编程技术网

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

Objective c 如何从包含路径的NSString创建FSRef?,objective-c,cocoa,macos,filesystems,Objective C,Cocoa,Macos,Filesystems,我有一个NSString格式的文件系统路径,但是我需要一个FSRef用于我将要进行的系统调用。创建FSRef的最佳方法是什么?试试FSPathMakeRef: NSString *path = @"Some... path... here"; FSRef f; OSStatus os_status = FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &f, NULL); if (os_status == noE

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

试试FSPathMakeRef:

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

if (os_status == noErr) {
    NSLog(@"Success");
}
您可以使用从UTF-8 C字符串路径生成
FSRef
,也可以使用
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()
FSRef-FSRef;
布尔isDirectory;
OSStatus result=FSPathMakeRef([myString UTF8String],&fsref,&isDirectory);
如果(结果<0)
//处理错误
//如果成功,则fsref有效,如果给定路径
//是一个目录。如果您不关心这一点,可以改为传递NULL
//对于FSPathMakeRef()的第三个参数

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

- (BOOL)getFSRef:(FSRef *)aFSRef
{
    return FSPathMakeRef( (const UInt8 *)[self fileSystemRepresentation], aFSRef, NULL ) == noErr;
}
更多信息请参见NDAlias(麻省理工学院许可)