简体   繁体   English

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

[英]Can't get data from file stored in asset catalog

I am storing several .svg files in an asset catalog.我在资产目录中存储了几个 .svg 文件。 To retrieve an asset I use the following code:要检索资产,我使用以下代码:

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

When running this code I get the following log message:运行此代码时,我收到以下日志消息:

CoreUI: attempting to lookup a named data 'p' with a type that is not a data type in the AssertCatalog CoreUI:尝试在 AssertCatalog 中查找类型不是数据类型的命名数据“p”

The name of the file is 'p.svg' which is stored in a folder with 'dataset' as extension.该文件的名称是“p.svg”,它存储在一个以“dataset”为扩展名的文件夹中。 I tried using other extensions for the file but nothing works.我尝试为文件使用其他扩展名,但没有任何效果。 I always get the same error and the asset is nil.我总是得到同样的错误,资产为零。

This may be related to a problem I'm having, in that the UTI I'm identifying my assets as being could run up the UTI dependancies and find public.image , thus making CoreUI think the asset is an image type and not a data type.这可能与我遇到的一个问题有关,因为我确定我的资产的 UTI 可以运行 UTI 依赖项并找到public.image ,从而使 CoreUI 认为资产是图像类型而不是数据类型。

SVGs inherit from public.image , so you will need to set the File Type to something that doesn't inherit from public.image . public.image继承自public.image ,因此您需要将文件类型设置为不继承自public.image Having it be just public.data will suffice.让它只是public.data就足够了。

你需要改变什么

Just reference the svg or whatever file in your project like .h/.m:只需引用 svg 或项目中的任何文件,如 .h/.m:

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

If path is nil, try:如果路径为零,请尝试:

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

I also have this issue;我也有这个问题; I wanted to provide images etc from local assets store.我想从本地资产商店提供图像等。 Dragging the files into the catalog I would fetch them like so - here a String:将文件拖到目录中我会像这样获取它们 - 这里是一个字符串:

extension NSString {
    class func string(fromAsset: String) -> String {
        guard let asset = NSDataAsset.init(name: fromAsset) else {
            return String(format: "Unable to locate asset:\n%@", fromAsset)
        }
        let data = NSData.init(data: (asset.data))
        let text = String.init(data: data as Data, encoding: String.Encoding.utf8)
    
        if fromAsset.hasSuffix(".md"), let html = try? Down(markdownString: text!).toHTML()
        {
            let htmlDoc = String(format: "<html><body>%@</body></html>", html)
            let data = Data(htmlDoc.utf8)
            if let attrs = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
                return attrs.string
            }
        }
        return text!
    }
}

the premise is they have to exist.前提是它们必须存在。 I think a per type / class extension would make retrieval simple.我认为每个类型/类扩展将使检索变得简单。 Eventually a generic object extension could dispatch to the proper extension, as here, you'd have to know what it was, so the decision which to use is currently a manual one.最终,通用对象扩展可以分派到适当的扩展,因为在这里,您必须知道它是什么,因此使用哪个决定目前是手动的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM