NSData *data;
data = [self fillInSomeStrangeBytes];
我的问题现在是如何以最简单的方式将data
写入文件。
(我已经有一个NSURL file://localhost/Users/Coding/Library/Application%20Support/App/file.strangebytes
)
请注意,将NSData
写入文件是可能会阻塞主线程的IO操作,特别是当数据对象很大时。
因此建议在后台线程上执行此操作,最简单的方法是使用GCD,如下所示:
// Use GCD's background queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Generate the file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"yourfilename.dat"];
// Save it into file system
[data writeToFile:dataPath atomically:YES];
});
writeToURL:atomically:或writeToFile:atomically:(如果您有文件名而不是URL)。
NSData
对象的来源是否重要?在这个问题中,我将从URL下载的.sqlite数据库保存到了一个NSData
对象中,但似乎没有正确地保存它。文件已经被写入,但是当我尝试访问它(无论是通过我的应用程序还是第三方查看器)时,它告诉我它不是有效的SQLite数据库。writeToURL:atomically:
只适用于保存NSString
或类似的东西吗? - GeneralMikewriteToFile:options:error:
或 writeToURL:options:error:
来保存 NSData
,如果由于任何原因保存失败,则可以报告错误代码。例如:NSError *error;
NSURL *folder = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:true error:&error];
if (!folder) {
NSLog(@"%s: %@", __FUNCTION__, error); // handle error however you would like
return;
}
NSURL *fileURL = [folder URLByAppendingPathComponent:filename];
BOOL success = [data writeToURL:fileURL options:NSDataWritingAtomic error:&error];
if (!success) {
NSLog(@"%s: %@", __FUNCTION__, error); // handle error however you would like
return;
}
NSData
对象的来源是否重要?在这个问题中,我从一个URL下载了一个.sqlite数据库并将其存储到了一个NSData
对象中,但似乎没有正确保存。文件已经被写入,但当我尝试访问它时(通过我的应用程序或第三方查看器),它告诉我它不是有效的SQLite数据库。writeToURL:atomically:
只适用于保存NSStrings
或类似的内容吗? - GeneralMikeNSData
是任何二进制数据的包装器。原始来源并不重要。根据您的问题,我建议使用NSURLConnection
(或类似AFNetworking
的库)而不是initWithContentsOfURL:
。对于小型下载,NSData
可能很好,但它不提供对下载的任何控制。当出现问题时,这使得诊断问题变得困难。 - Alex