Basic Question about Swift

Hello Caroline, long time no see. I have a basic question. Would you please tell me why using try textureLoader.newTexture(…) to compile successfully in chapter 6 source code. I thought maybe use try! or try? without do-catch.

The loadTexture method in Texturable itself throws.

 static func loadTexture(imageName: String) throws -> MTLTexture? {

So when that method does:

let texture = try textureLoader.newTexture(URL: url,
                                               options: textureLoaderOptions)

if this fails, texture will have nil and return nil from loadTexture. In Submesh.Textures init() calls property() which calls:

let texture = try? Submesh.loadTexture(imageName: filename)

So if loadTexture has an error inside it, there will be nil in texture. If texture is nil, then property() will return nil, and baseColor will hold nil, and no errors will need to be caught.