无法编写ToFile iphone

时间:2013-03-14 19:48:58

标签: iphone objective-c xcode cocoa-touch

我使用以下代码下载slqite文件并存储

self.responseData = NSMutableData

我收到了responseData = 2048bytes。运作良好。

然而,白色写入会创建一个文件myFile.sqlite,但它是零字节。 有什么问题?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath];
    if(!success)
    {
        if(self.responseData)
        {
            dbPath = [dbPath stringByAppendingPathComponent:[self.selectedBtn.dbPath lastPathComponent]];
            [self.responseData writeToFile:dbPath atomically:YES];
            [self performSegueWithIdentifier:@"list" sender:self];
        }
    }

    [self.alertView removeFromSuperview];
}

-(NSString *) getDBPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSURL *url = [[NSURL alloc] initWithString:[self.selectedBtn.dbPath lastPathComponent]];
    return [documentsDir stringByAppendingPathComponent:[url lastPathComponent]];
}

我收到错误

`The operation couldn’t be completed. (Cocoa error 4.)`

dbPath : /Users/umar/Library/Application Support/iPhone Simulator/6.1/Applications/00027635-CE9C-48C3-8000-64CA1E6532F1/Documents/music.sqlite/music.sqlite

2 个答案:

答案 0 :(得分:1)

Documents/music.sqlite/music.sqlite

不,这不是一条有效的道路......你的意思是Documents/music.sqlite,不是吗?另外,我不明白为什么你会因为不相关的任务而强奸穷人NSURL

return [documentsDir stringByAppendingPathComponent:[self.selectedBtn.dbPath lastPathComponent]];

另外,请确保self.selectedBtn.dbPath确实是一个有效路径,而不是垃圾(我可以想象)。

答案 1 :(得分:1)

您将[self.selectedBtn.dbPath lastPathComponent]追加getDBPath两次,一次放入connctionDidFinishLoading,再放入{{1}}。

选择其中一个要移除的实例。