无法转换'String'类型的值?预期的参数类型'URL'

时间:2022-03-18 16:33:41

I'm trying to load data from file which was in main bundle. When I use this code

我正在尝试从主包中的文件加载数据。当我使用此代码时

 let path = Bundle.main.path(forResource: "abc", ofType: "txt")
 let dataTwo = try! Data(contentsOf: path)\\ error here

Also I tried to convert String to URL

我也尝试将String转换为URL

 let dataTwo = try! Data(contentsOf: URL(string: "file://\(path)")!)

But after execution am getting this

但执行后我得到了这个

fatal error: unexpectedly found nil while unwrapping an Optional value

致命错误:在展开Optional值时意外发现nil

2 个解决方案

#1


2  

You may want to use .url instead:

您可能想要使用.url:

let url = Bundle.main.url(forResource: "abc", withExtension:"txt")
let dataTwo = try! Data(contentsOf: url!)

and safely handle errors instead of force unwrapping.

并安全地处理错误而不是强行展开。

Simple version:

简单版本:

if let url = Bundle.main.url(forResource: "abc", withExtension:"txt"),
    let dataTwo = try? Data(contentsOf: url) 
{
    // use dataTwo
} else {
    // some error happened
}

Even better:

更好的是:

do {
    guard let url = Bundle.main.url(forResource: "abc", withExtension:"txt") else {
        return
    }
    let dataTwo = try Data(contentsOf: url)
    // use dataTwo
} catch {
    print(error)
}

This way you don't need to convert a path to an URL, because you're using an URL from the beginning, and you can handle errors. In your specific case, you will know if your asset is there and if your URL is correct.

这样您就不需要将路径转换为URL,因为您从头开始使用URL,并且可以处理错误。在您的具体情况下,您将知道您的资产是否存在以及您的网址是否正确。

#2


0  

For file URL use init(fileURLWithPath:) constructor.

对于文件URL,请使用init(fileURLWithPath :)构造函数。

Also here

也在这里

let dataTwo = try! Data(contentsOf: path)\\ error here

get rid of try! and use proper error handling to see whats the real error happens.

摆脱尝试!并使用适当的错误处理来查看真正的错误发生的原因。

#1


2  

You may want to use .url instead:

您可能想要使用.url:

let url = Bundle.main.url(forResource: "abc", withExtension:"txt")
let dataTwo = try! Data(contentsOf: url!)

and safely handle errors instead of force unwrapping.

并安全地处理错误而不是强行展开。

Simple version:

简单版本:

if let url = Bundle.main.url(forResource: "abc", withExtension:"txt"),
    let dataTwo = try? Data(contentsOf: url) 
{
    // use dataTwo
} else {
    // some error happened
}

Even better:

更好的是:

do {
    guard let url = Bundle.main.url(forResource: "abc", withExtension:"txt") else {
        return
    }
    let dataTwo = try Data(contentsOf: url)
    // use dataTwo
} catch {
    print(error)
}

This way you don't need to convert a path to an URL, because you're using an URL from the beginning, and you can handle errors. In your specific case, you will know if your asset is there and if your URL is correct.

这样您就不需要将路径转换为URL,因为您从头开始使用URL,并且可以处理错误。在您的具体情况下,您将知道您的资产是否存在以及您的网址是否正确。

#2


0  

For file URL use init(fileURLWithPath:) constructor.

对于文件URL,请使用init(fileURLWithPath :)构造函数。

Also here

也在这里

let dataTwo = try! Data(contentsOf: path)\\ error here

get rid of try! and use proper error handling to see whats the real error happens.

摆脱尝试!并使用适当的错误处理来查看真正的错误发生的原因。