写字符串会给我错误,写图像不会

时间:2022-01-03 20:17:09

I have this code:

我有这段代码:

            let image = UIImage(data: downloadedImage!)


            let convertImage = UIImagePNGRepresentation(image!)
            let pathUIDImage = self.getDocumentsDirectory().appendingPathComponent(playersUID + "Image")
            try? convertImage!.write(to: pathUIDImage) //working


            let playersUIDImageVersion = "1"
            var pathUIDImageVersion = self.getDocumentsDirectory().appendingPathComponent(playersUID + "ImageVersion")
            try? playersUIDImageVersion.write(to: pathUIDImageVersion) //error

The error is:

错误的是:

Cannot convert value of type "URL" to expected instrument type "inout_"

无法将“URL”类型的值转换为预期的仪表类型“inout_”

When I replace try? playersUIDImageVersion to try? convertImage! the error is gone. Is there a difference between writing different types of values to the directory? Thank you. Below the function getDocumentsDirectory

当我更换试试吗?playersUIDImageVersion尝试吗?convertImage !错误消失了。将不同类型的值写入目录是否有区别?谢谢你!以下函数getDocumentsDirectory

func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

1 个解决方案

#1


1  

The write function of String is different, it requires additional parameters atomically and encoding.

字符串的写函数不同,需要原子化和编码附加参数。

try? playersUIDImageVersion.write(to: pathUIDImageVersion, atomically: true, encoding: .utf8)

In such a case retype the function and see what Xcode suggests for code completion.

在这种情况下,重新输入函数并查看Xcode对于代码完成的建议。

Besides it's highly recommended to use always an appropriate file extension (.png, .txt).

此外,强烈建议始终使用适当的文件扩展名(。png,. txt)。

#1


1  

The write function of String is different, it requires additional parameters atomically and encoding.

字符串的写函数不同,需要原子化和编码附加参数。

try? playersUIDImageVersion.write(to: pathUIDImageVersion, atomically: true, encoding: .utf8)

In such a case retype the function and see what Xcode suggests for code completion.

在这种情况下,重新输入函数并查看Xcode对于代码完成的建议。

Besides it's highly recommended to use always an appropriate file extension (.png, .txt).

此外,强烈建议始终使用适当的文件扩展名(。png,. txt)。