Join Todd Perkins for an in-depth discussion in this video Creating global properties and constants, part of Building a Note-Taking App for iOS 9 with Swift.
- View Offline
- Now we'll add some of the global variables and constants that we're going to be working with, with our app. So I'm going to switch back to the Standard view, that's the button to the left of the Assistant Editor. So when I click that I go to the Standard editor, and then I'll show the Navigator by clicking the button in the toolbar. So let's go to MasterViewController.swift, and we're going to add our global variables and constants right here. So before I write any code I'm actually going to delete the reference to detailViewController and objects.
And I'm going to make them global variables instead of instance variables. So I'll do that right under import, so I''ll create objects, and this is going to be of the type String array, so String in brackets, and then I'm going to instantiate that array right here. So on the next line I'm going to create a variable called currentIndex, this will represent the current index of the array that we're working with.
So, as we add in notes, which each note will just be a string, into our objects array, the currentIndex will keep track of which index of the array that we're currently working with. So when we go to the detailView, we'll set currentIndex to the appropriate index so that detailView knows which object to work with. So we're going to initialize that to zero, and I'll create a variable called masterView, and this is going to be a MasterViewController.
That's the same as the class that we're in right now. Now right now, we can't give this a value because we're going to instantiate this inside of viewDidLoad, so we need to specify that this value can be optional. In other words, it's possible to be null, or in Swift, nil. So I'll add a ? right there, so that's saying that masterView can possibly be nil. Now I'll go to the next line, I'll create a variable called detailViewController, and the type of that is going to be DetailViewController, and this is going to be optional as well.
Now I'll declare two constants using the keyword let. First is kNotes, that's going to be a String, and we'll set that equal to the string notes. This is the key that we're going to use to save data to persistent storage. We'll look at how that all works later in this chapter. And then finally we're going to let BLANK_NOTE be a String equal to (New Note).
Now this the string that's going to show up when we create a new note, the default note text. I'm just creating it as a constant since we're going to be referencing this value a few times in our code. So these are the various globals that we're going to be manipulating as we work in this course and as we build our note-taking application. Now, before we move on, I want to point out that there are a couple of errors in our code. And if I click on the error message, which is the octagon with an exclamation point inside, I'm then taken in the Navigator to the Issue navigator, and here I see that there's two errors.
If I click on this one, I'm shown that self.detailViewController doesn't work because MasterViewController has no member 'detailViewController' because we moved it from being an instance variable to a global variable. So if I just delete self and the dot, then that error should go away. Now if we go to the next error, which is Cannot convert value of type 'NSDate' to expected argument type String, this is because we're trying to insert an NSDate, which the template does, into the objects array, but we've changed it to only hold strings.
So I just have to change this to a string, and we actually already have that string which is BLANK_NOTE. So we'll insert that there instead, and now I'm seeing another error, I'll click on that, that's taking me to the previous error. I also have some warnings, and this warning is taking me to line 56, where it says I'm trying to cast from NSString to NSDate, and that's not going to work. So we'll just delete the cast right there, as! and NSDate.
And then finally, this warning here, which is doing the same thing, so I'll just delete that cast right there. And now what I'm going to do is press Cmd B to build the application, and just make sure that there aren't any failures. So I'm seeing Static member 'description' cannot be used for an instance of type 'String', so let's get rid of that as well. So when I click on that, it's saying object.description, and description is a property on objects that are not strings, that converts them to a string, and so if we just delete .description, since if we look on the previous line, object is an instance of the objects array, which is always going to be a string, we don't need to get its description property on the next line.
so I'll just delete .description, and that should fix that error. And I'll try to build again, and at this point it should say Build Succeeded. Alright, so our build has now succeeded, and we've added our global variables and constants, and fixed all of the resulting errors and warnings.
Building a Note-Taking App for Android and Building a Note-Taking App for Windows Phone 8 and Windows Store use the same assets. Compare and contrast the steps and discover the similarities and differences between the three platforms.