如何将 .mp3 文件存储到核心数据中?

How to store .mp3 file into Core Data?

我找到了将文件保存在磁盘上的代码:

char *saves = "abcd";
NSData *data = [[NSData alloc] initWithBytes:saves length:4]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile"];
[data writeToFile:appFile atomically:YES];

我是 Core Data 的新手。我想用它来存储 MP3 文件。首先我想从网上下载它 URL,然后将它保存在核心数据中。

您应该将文件保存在应用程序文档目录中(也许创建一个 "music" 子文件夹),并在 Core Data 中存储对这些文件的引用。确保这些文件的名称是唯一的 (NSUUID may be useful) and that you don't store the path to the file on your managed object, since this is not guaranteed to be the same next time the application runs.