I'm new to SwiftUI coding and trying to learn step by step. Sometimes I don't get what XCode is trying to tell me with its errors. Following a super simple tutorial on how to resize an image. Did everything ok until this error came up:
Value of type 'some View' has no member 'resizable'
I'm trying to resize an image in my assets folder. This is my code:
import SwiftUI
struct ContentView: View {
...
...
var body: some View {
...
Image("home_illustration")
.padding(.bottom,60)
.resizable()
...
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Each SwiftUI modifier returns a type. The final type of a
View'sbodycan be extremely complex, hence the existence ofsome View- that basically tells the compiler that you're returning something that conforms to theViewprotocol, but that you don't have the time or the energy to spell out exactly what it is, and anyway that's your job, you're a compiler, just figure it out.Some modifiers only make sense when applied to certain types of view. In your case,
.paddingcan be added to anything, so it's a method on theViewprotocol. It returns a type ofModifiedContent<SwiftUI.Image, SwiftUI._PaddingLayout>. These types build up the more modifiers you apply..resizable()is a method onImage, which returns anImage.So if you resize, then pad the image, you're OK. If you apply padding, you aren't dealing with an image any more, so the compiler can't figure out what is happening.