Group Group Group Group Group Group Group Group Group

Models and Views | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/15234721-your-second-ios-and-swiftui-app/lessons/3

This specific tutorial video seemed to confuse me more than help me. The narrator wasn’t quite clear on why we are making extensions of the Book struct in another file rather than with the existing book file. Also trying to explain why we are putting an optional in front of a SwiftUI Image extension with “we wont always have a valid image for a title to represent that, add a question mark after the keyword init.” I have no idea what the narrator is trying to say here. Then not explaining the .square part for the initialization of symbolName, along with not explaining line 57 right after that (self.init…).

Hi,

I tried to explain this from about 2:58 to 3:33. Please let me know what confused you about that, and I can elaborate.

It’s a failable initializer.

SFSymbols are covered in this video.

Thanks for taking the time to reply. The SFSymbols are my fault as I forgot that we learned that earlier. Will be reading more about failable initializers thanks for the resource! I’m still confused on why we call self.init(systemName: symbolName) on line 57 in our init? method. Is there a chance you can elaborate on that?

1 Like

Sure!

After we get to that part of the failable initializer, we know that the character is not nil. So the Image we’re initializing won’t be nil, either. Instead, it will be one of these SFSymbols:

In order to create an Image from one of those symbol names, you call
https://developer.apple.com/documentation/swiftui/image/init(systemname:).

How come when we do extension Image {} swift knows that we are extending Swift’s Image and not the struct image that we just created? about 6:10 minutes into the video

How come when we do extension Image {} swift knows that we are extending Swift’s Image and not the struct image that we just created? about 6:10 minutes into the video

I don’t understand of the grammar in part of " case let … " why do we use ? and how to be possible ?

An extension is written at file-scope. The only Image in that scope is SwiftUI.Image.

You would have to write

extension Book.Image {

if you specifically wanted to extend that type.

guard let and if let are just shorthand.

For example…

guard let character = title.first

is shorthand for

guard case .some(let character) = title.first

But as I mention in the video, what you bind on each line does not have to be optional. If it’s not, you have to use the “case” form.

1 Like

Thank you.
Could you give me a reference for the part
"If it’s not, you have to use “case” form "

1 Like

I mention that it’s necessary at 10:39.
Sorry, I can’t find any documentation on binding non-optionals, only optionals.

All there is to it is that if you want to define new variables inside of an if or guard statement, you do that using case.

Example:

if
  case let ten = 2 * 5,
  ten < 11
{
  print("Ten is less than eleven.")
}

It’s an alternate form of this:

switch 2 * 5 {
case let ten where ten < 11:
  print("Ten is less than eleven.")
default:
  break
}
1 Like

Thank you very much for you rapid and kind answer.