将 NSData 写入文件的最简单方法

发布于 2024-07-16 04:34:25 字数 230 浏览 7 评论 0原文

NSData *data;
data = [self fillInSomeStrangeBytes];

我现在的问题是如何以最简单的方式将此数据写入文件。

(我已经有一个 NSURL file://localhost/Users/Coding/Library/Application%20Support/App/file.strangebytes

NSData *data;
data = [self fillInSomeStrangeBytes];

My question is now how I can write this data on the easiest way to an file.

(I've already an NSURL file://localhost/Users/Coding/Library/Application%20Support/App/file.strangebytes)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水中月 2024-07-23 04:34:25

NSData 有一个名为 writeToURL:atomically: 的方法,它完全可以完成您想要做的事情。 查看 文档 < code>NSData 查看如何使用它。

NSData has a method called writeToURL:atomically: that does exactly what you want to do. Look in the documentation for NSData to see how to use it.

倦话 2024-07-23 04:34:25

请注意,将 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];
});

Notice that writing NSData into a file is an IO operation that may block the main thread. Especially if the data object is large.

Therefore it is advised to perform this on a background thread, the easiest way would be to use GCD as follows:

// 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];
});
绝影如岚 2024-07-23 04:34:25

writeToURL:atomically:writeToFile:atomically: 如果您有文件名而不是 URL。

writeToURL:atomically: or writeToFile:atomically: if you have a filename instead of a URL.

没企图 2024-07-23 04:34:25

您还有 writeToFile: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;
}

You also have writeToFile:options:error: or writeToURL:options:error: which can report error codes in case the saving of the NSData failed for any reason. For example:

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;
}
以往的大感动 2024-07-23 04:34:25

如果您想手动执行几次而不是编码,最简单的方法是仅使用鼠标:

  1. 在 NSdata 完成后的一行上放置一个断点。

输入图片此处描述

  1. 将鼠标悬停在 NSData 变量上,单击“Eye”按钮,然后导出。

  2. 之后选择存储详细信息(文件的名称/扩展名/位置)。

输入图片此处描述

  1. 单击“保存”即可完成!

The easiest way, if you want to do this a few times manually instead of coding, is to only use your mouse:

  1. Put a breakpoint one line after your NSdata are fulfilled.

enter image description here

  1. Hold your mouse over your NSData variable, click on the Eye button, and then export.

  2. After that chose the storage details (name/extension/location of the file).

enter image description here

  1. Click on "Save" and you are done!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文