Reputation: 1273
I'm trying to load an image from URL to a Widget. I've done so before just fine in another project I've recently made. Now I'm trying to do the same exact method and seeing some strange errors Argument type 'URL' does not conform to expected type 'Decoder'
and Cannot convert value of type 'ExampleWidgetExtension.Data' to expected argument type 'Foundation.Data'
. My other project works just fine and I'm using Codable
data just like before. Everything but the image seems to be working.
struct ExampleWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack (alignment: .leading, spacing: 5) {
ForEach(entry.model?.example.results?.prefix(1) ?? [], id: \.id) { example in
/*
Text(example.data.title ?? "")
.fontWeight(.bold)
.font(Font.system(.caption))
.lineLimit(1)
*/
if let url = URL(string:example.data.url ?? ""),
let imageData = try? Data(from: url),
let uiImage = UIImage(data: imageData) {
Image(uiImage: uiImage)
.resizable()
.frame(width: 30, height: 30)
}
}
}
.padding(.all, 10)
}
}
Upvotes: 0
Views: 875
Reputation: 52387
There is no initializer on Data
(the one in Foundation) that takes a URL
in the from:
field. The reason that you're getting an error is that Data(from:)
expects a Decoder
in that argument. You say that you have another project where you do that, but you must have some sort of custom extension on Data
for that to work.
What you want is let imageData = try? Data(contentsOf: url)
try? Foundation.Data(contentsOf: url)
You may want to consider renaming your Data
type to something else to avoid namespace collisions with Foundation's Data
Upvotes: 1