ios – 无法从存储在资产目录中的文件中获取数据

前端之家收集整理的这篇文章主要介绍了ios – 无法从存储在资产目录中的文件中获取数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在资产目录中存储了几个.svg文件.
要检索资产,我使用以下代码

NSDataAsset *asset = [[NSDataAsset alloc] initWithName:@"p"];

运行此代码时,我收到以下日志消息:

CoreUI: attempting to lookup a named data ‘p’ with a type that is not a data type in the AssertCatalog

文件名称是“p.svg”,它存储在以“dataset”作为扩展名的文件夹中.我尝试使用其他扩展名的文件,但没有任何作用.我总是得到同样的错误,资产是零.

解决方法

只需引用svg或项目中的任何文件,如.h / .m:

NSString *path = [[NSBundle mainBundle] pathForResource:@"p" ofType:@"svg"];
[NSData dataWithContentsOfFile:path];

如果path为nil,请尝试:

In the Xcode target "Build Phases" add the file under "Copy Bundle Resources"".

猜你在找的Xcode相关文章