[MUSIC] All right, let's return to the submit function inside of me view controller. And so now that we have gotten our user back and we can get the hobbies out of that user that the server returned to us. We also get back a user ID with that returnUser, that's the whole point of creating a new user or signing in so we have a user ID which we can use to identify us when we ask the server to update our information inside of the database. So we also want to cache that user ID into NSUserDefault so that it's available for other classes in our application to use when they need them. For example, the edit hobbies view controller will need our user ID in order to tell the server edit the hobbies for a user with the ID such and such. Again, we need to do the same thing and register a default value for the current user ID key inside of NS user defaults. So let's go do that, too. All right. Again, I check if there's nothing in that key yet. Then I set an empty string as the value for my CurrentUserId field. And then we say, synchronize. Store that in there. All right, let's go back to our submit function and finish that up. So if we got our return status code of zero that means everything went well. The server gave us back our user and now we can save our hobbies and save our user ID so that it's ready for use. But what if something bad happened? Let's go, we want to show the user hey, the server is telling us something bad happens. So let's display that to the user. So if that's the case, then status.code will, oops. Status.code will not equal 0. So I can just say else, and then it would be nice to have some function that will help us show errors where ever we need to in that application. because a few lines of code to show something on the screen when an error happens, so rather than typing those lines of code over and over and over again, let's factor them into one function, and then call that function whenever we need to show an error. So let's go do that. I'm going to implement that in the super class, hobby share view controller, that way all of the subclasses can use it to show errors when they need to. All right, so here we are inside of how user view controller and we will implement this function called show error which takes a string that contains the message that we want to show in the error. So we'll say let alert equal UI alert controller, and we'll use its init with title, message, and preferred style method. So the title will be the app title. So rather than me typing the app title everywhere and possibly misspelling it in one place, let's save that into a constant so that we only have to type it in code once, so if it is misspelled, we only have one place to correct the misspelling. So I'm going to create a new file over here, called constants. So I'll say new file, and I'll just this time create a new swift file and call it constants, and create it. Save it in the default location. And here I will just say let kAppTitle equal hoBshare. All right so now if I go back to where I was trying to use we'll notice that the error is resolved. So the title will be the title of the app. The message will be the message that was passed into us by the show error function. And the preferred style will be dot alert. All right, so now we need a way for the user to dismiss that alert so that they can go on with whatever they want to do in the application, so let's add an OK button. We do that by adding on a UI alert action to the UI alert controller which is called alert in our case. So we say let OK action equal UI alert action, again in it with the title dismiss, the style of this OK button should be the default style, and then we have this handler argument which decides after the user, excuse me, which we use to express what should happen when the user actually taps that Dismiss button or the OK button as we're calling it. So that's what this handler function is, and you'll notice again it's another trailing closure. So we'll say when the user presses the action button, and by the way that action button is returned to us inside of the argument of this closure, in case we need to look at it. But we'll just say alert.dismissViewControllerAnimated true, and we don't need anything to happen after the alert goes away, so we say nil for the completion argument here. So now we need to actually add that okAction into the alert itself, so we say alert.addAction, the okAction. And now we can show the alert on the screen now that it's set up. So self.presentViewController. We want to present the alert in animated fashion so we set this to True. And once the alert gets onto the screen, we don't need anything else to happen until the user presses the OK button so the completion can be nil here. So now we have a nice function that will allow us to show alerts anywhere we need to and if we return to the submit function on our meViewController, remember, here we're trying to create a user or sign in. So if something bad happens, we fall into this else and then we'll call that showError statement. What error do we show? We want to show the error that the server told us happened so luckily on the SFL base model class helps us look at that through the status.statusDescription object or property. So we can just pass in the returned user.status.statusDescription which is a string into the showError function and we'll see what the server told us went wrong on the screen. Okay, so that finishes up our submit function. We have a little error here because again, we haven't finished implementing our user model on this initializer. Let's go just quickly touch up the text field should return function. We said if validate equals true, then call this admit function, which we just finished implementing. But what if validate equaled false. We should show an error to the user and say hey, you need to correct this, you need to correct this form. So we could do something here like say else, self.showError("Did you enter a user name?") and this will help guide the user to correct the form if they did something wrong before trying to submit it. Okay, so now we have our form validation. We have our submit function which we'll ask our data provider to go call the server for us. So we're pretty much almost ready to go actually and make the call out to the internet. We know that this will happen when the user pushes the return button on the keyboard, but it would be really nice to have a very obvious submit button. So let's go and add one now. And I'm going to create this function as an IP action that can be called when my Submit button gets pressed. So, this is going to be called saveButtonPressed, and we're going to say the same thing, if validate equals true, then submit and it looks like I need one more curly brace here. Okay good. So now all we need to do is go put that submit button into our UI because we haven't done that yet. So let's return to our Main.storyboard. Here it is. And, go to our Me function. And now I want to add that Submit button. A nice place to do it will be up in this navigation bar in the top. So we'll open up our Archive Office Library. And this time we're going to look for a UI bar button. So I'll drag that out into the navigation bar. We don't want to use the regular button. We want to use the bar button item, okay? So we'll double click on this and make its text be the word submit. And now, we can use the assistant editor again. If I select this yellow object that represents our view controller and set this to automatic, then we should get to the new view control class, and I can, while holding down Control on my keyboard, drag from that new submit button we created to the save button pressed function. So now, when we type in our username and hit the submit button here or again tap the return button on the keyboard, it will go and ask the data provider to make the call to the server for us. [MUSIC]