iphone - Cocoa错误260,文件路径结构

标签 iphone xcode cocoa file ftp

我正在使用这个方法

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

当我在应用程序中打开文件时,它会给出特定的文件 URL。该网址看起来像

file://localhost/Users/User/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/229C1D14-28D4-4B82-8CBB-8B9BC472E7A3/Documents/Inbox/Trinity_2016-4.pdf

我正在尝试将其从文件 url 转换为路径,以便我可以将它与开源库 SCRFTPRequest 一起使用。 .

问题是当我尝试指向一个文件时,我收到了 cocoa 错误 260,我只是想确保我的设置正确。

NSFileReadNoSuchFileError = 260,                        // Read error (no such file)

这是示例代码

SCRFTPRequest *ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:@"ftp://192.168.1.101/"] 
toUploadFile:[[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]];

这就是我改变它的方式

NSString *fileurl = @"file://Users/User....pdf";
NSString *path = [fileurl substringFromIndex:16];
SCRFTPRequest *ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:@"ftp://192.168.1.101/"] 
toUploadFile:path];

这段代码的格式正确吗?正如所写,它使用 substringFromIndex 将文件 url 从 file://localhost/Users/User... 更改为/Users/User...

最佳答案

NSString *fileurl = @"file://Users/User....pdf";
NSString *path = [fileurl substringFromIndex:16];

您缺少正确的索引。此代码不会使 path 成为 /Users/User....pdf,而是 ....pdf。如果没有方法从 URL 中提取路径

NSString *path = [fileurl substringFromIndex:6];

就足够了。但是,由于我们有很棒的 NSURL 类,所以没有借口不写

NSURL *realURL = [NSURL URLWithString:fileurl];
NSString *path = [realURL path];

关于iphone - Cocoa错误260,文件路径结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12591097/

相关文章:

ios - 使用自定义 NSURLProtocol 时 WebView 无法处理响应

ios - 临时 iOS 应用程序在撤销其签名证书后是否可以运行?

cocoa - 从属键如何注册? (键值观察)

Objective-C:EXC_BAD_ACCESS并且没有响应

objective-c - 检查 NSArray 中对象的重复属性值

使用 AVAudioPlayer 播放音频文件时 iPhone 崩溃且来电中断播放

iphone - iPhone 5横向模式下,导航栏右侧的按钮停止工作,如何解决?

xcode - 我可以使用 Xcode 7 在 Swift 1(适用于 iOS 7 和 8)中编写应用程序吗?

iphone - 产生两个数字的每个组合,例如 1000

c++ - 将值从 Xcode 中的运行脚本传递到编译