I don't understand these staments

let itemsController = window!.rootViewController as! ItemsViewController
itemsController.itemStore = itemStore
where is this explained in this book ?

p. 151 ? Do you have a more specific question to give some clue about what you find confusing? Is it the Swift syntax? Something else?

thanks 7stud7stud. These statements are at the beginning of Homepwner app, and in the class ItemsViewController is : var itemStore: ItemStore!.I suppose that they have a relationship, but I don’t know why I need to use them; the first two , and this one. What these 3 statements are doing , and why ?

in the class ItemsViewController is :

var itemStore: ItemStore!

Because the itemStore variable was declared as an optional type, it is set to nil by default. So, the question becomes: what is the best way to assign a value to that variable?

As you become an intermediate to advanced programmer you become aware of concepts in the computer programming field known as design patterns. The best, most advanced programmers, have learned that over time it is easier to change, improve, and maintain code if the code is organized in specific ways–using certain design patterns that they invented. The architecture of a project becomes more and more important as a project gets bigger and bigger. Very smart programmers discuss different architectures among themselves and over time they have come up with certain design patterns that seem to work best to organize code for specific situations.

On p. 151, the book tries to explain the dependency injection design pattern. One problem with design patterns is that they tend to use language that is so arcane that it obfuscates what the pattern is supposed to accomplish. For instance, the phrase:

is incomprehensible to anyone who has ever read it. However, it is a famous design pattern so you can google it to learn more about it. Here’s a simple explanation from jamesshore.com/Blog/Dependen … ified.html with the code converted from Java to Swift:

When I first heard about dependency injection, I thought, “Dependendiwhatsit?” and promptly forgot about it. When I finally took the time to figure out what people were talking about, I laughed. “That’s all it is?”

“Dependency Injection” is a 25-dollar term for a 5-cent concept. That’s not to say that it’s a bad term… and it’s a good tool. But the top articles on Google focus on bells and whistles at the expense of the basic concept. I figured I should say something, well, simpler.

The Really Short Version
Dependency injection means giving an object its instance variables. Really. That’s it.

The Slightly Longer Version, Part I: Dependency Non-Injection
Classes have these things they call methods on. Let’s call those “dependencies.” Most people call them “variables.” Sometimes, when they’re feeling fancy, they call them “instance variables.”

class Example { 
  var myDatabase: DatabaseThingie
  init() { 
    myDatabase = DatabaseThingie()  //The class creates the object
  }                                 //that gets assigned to the property
  func doStuff() { 
    ... 
    myDatabase.getData()
    ... 
  } 
}

Here, we have a variable… uh, dependency… named “myDatabase.” We initialize it in the constructor.

The Slightly Longer Version, Part II: Dependency Injection
If we wanted to, we could pass the variable into the constructor. That would “inject” the “dependency” into the class. Now when we use the variable (dependency), we use the object that we were given rather than the one we created.

class Example { 
  var myDatabase: DatabaseThingie  
  init() { 
    myDatabase = DatabaseThingie()
  } 
  //For the init() below, something outside the class
  //creates the object then passes it into the class:
  init(useThisDatabaseInstead: DatabaseThingie) { 
    myDatabase = useThisDatabaseInstead
  }
  func doStuff() { 
    ... 
    myDatabase.getData() 
    ... 
  } 
}

That’s really all there is to it. The rest is just variations on the theme. You could set the dependency ( variable) in… wait for it… a setter method…[or some outside entity could assign to the property directly].

If you don’t understand the design pattern, don’t worry about it–instead just write code in a way that makes the most sense to you.

Our project is setup with a storyboard in such a way that when Xcode executes our project, iOS creates a window and assigns an instance of the ItemsViewController to the window’s rootViewController property. Then iOS also creates an instance of the AppDelegate class, and iOS assigns the window to the window property of the AppDelegate instance. Then iOS uses the instance of the AppDelegate to call application(_:didFinishLaunchingWithOption:), which causes our code inside the method to execute. Then our code sets the value of the itemStore property for the instance of the ItemsViewController that iOS created.

1 Like

7stud7stud thanks very much for you explanation, i will go to the link ,and study the information.

Thank you for the explanation. It was extremely helpful! :smiley:

Thank you! :innocent: