我使用下面的代碼來下載一個文件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
'writeToFile'返回一個'BOOL',所以它的值檢查返回值。還要考慮' - (BOOL)writeToFile:options:error:'。調查錯誤對象也可能會提供一些線索。 – 2013-03-14 19:56:45
另外,你確定'getDBPath'返回一個有效的路徑嗎? – 2013-03-14 19:58:09
路徑有效,@MikeD是我已經嘗試錯誤,給我第二個:) – 2013-03-14 20:02:16