2013-03-14 31 views
0

我使用下面的代碼來下載一個文件slqite並存儲未能將writeToFile iphone

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 
+3

'writeToFile'返回一個'BOOL',所以它的值檢查返回值。還要考慮' - (BOOL)writeToFile:options:error:'。調查錯誤對象也可能會提供一些線索。 – 2013-03-14 19:56:45

+0

另外,你確定'getDBPath'返回一個有效的路徑嗎? – 2013-03-14 19:58:09

+0

路徑有效,@MikeD是我已經嘗試錯誤,給我第二個:) – 2013-03-14 20:02:16

回答

1

您正在追加[self.selectedBtn.dbPath lastPathComponent]兩次,一次在getDBPath,並且再次登錄connctionDidFinishLoading

選擇其中一個要移除的實例。

+0

權利只是想出了。 ty tho :) – 2013-03-14 20:14:26

1
Documents/music.sqlite/music.sqlite 

不,這不是一個有效的路徑...你的意思Documents/music.sqlite,不是嗎?另外,我不明白你爲什麼強姦可憐的NSURL一個不相關的任務。

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

此外,還要確保self.selectedBtn.dbPath也確實是一個有效的路徑,而不是垃圾(我能想象它是)。