How to add try catch in IOS app development?

Because context a can get errors unpredictably right so you never want to do that here but other methods sometimes it’s like this method has to succeed or I’m just doomed so I’m going to try exclamation point there’s one other way which I would talk about in a few slides which is try question mark okay try question arc means try this I’m not going to catch the error but return nil if there was a thrown error okay that’s right question mark I’m going to show you an example that in a couple slides okay so that’s throwing of errors already got that and not a lot of things that throw errors but you are going to run across them okay in both in some of the homeworks and also certainly in your final project,

 

Okay so let’s get back to core data here now calling value for key and set value for key is pretty ugly okay there’s no type checking there because it’s any object is the type of the argument okay and you got all these literal strings like quote text and quote created and quote ID inside your code which you’re probably going to put in a struck G no static let like you do all your constants but it’s just a mess okay communal that what you really want is VARs you want these tweets you want to be able have a varam they’re called texts and just want to say that tweet dot text equals that’s what you want okay object oriented so of course we can do that and the way we do that is we’re going to create a subclass of in its managed object okay so for each thing a tweet Twitter user whatever we’re going to create a subclass of nsobject manage object for it and it’s going to have VARs which are all the properties okay couldn’t be easier really really really simple and Xcode will even generate this subclass for us okay so let’s look at how you do that so I’m going to select the entities that I want to generate these little subclasses for.

 

Okay so I’m selecting them both in this case right here then I go up to the editor menu and I pick create and it’s managed object subclass okay when I do that it’s going to say okay for which of your models do you want to do that because you might have multiple models so I’ll pick the only model I have here then it says okay which entities well i selected both so it’s got these both pre-selected i want to create a ns-managed object subclass for tweet and one for twitter users can be two different classes okay now it’s saying where do you want to put it it’s also asking what language be careful right here sometimes this comes up objective-c even if you’re in a Swift project so make sure that says Swift otherwise you’re going to get an objective-c class which is a subclass of manage manage object you know with that so Swift the other thing is there’s this use scalar properties for primitive data types be careful of this one if you turn this on then for example NS date the property it creates of the VAR is going to be an NS time interval which is gonna be the number of seconds.

 

Two since 1970 okay so you probably don’t want that most of the time right you want it to be an estate object also you’re going to put it it’s just going to say where it’s going to put it by default it usually says to put it at the very top level but you usually actually want it down where all the rest of your files are down here like in Core Data all right so make sure you get that right – so be it pay attention to this thing and answer all these questions the way you want okay it’s not one you can just click create and just move on through or you won’t get what you want I don’t think all right so it created these things here they are you can see them on the side here there’s one tweet dot Swift okay here’s Twitter user dot Swift and it’s just a class tweet as promised look at that inherits from ns-managed object and you can put any code you want in here to do tweet specific stuff because you know the fact that the tweet is stored in the database is great but it might also have some other behavior that you want to add it’s just a class right you’re using it in your app so you might want to put some some code in there.

 

Okay so it’s perfectly good for that here’s Twitter user right okay today mismanage object put all your Twitter user stuff here but what about those VARs okay I thought there’s going to be VARs for text and created and the screen name for the where are those well it turns out those are put in extensions you see them right here there’s two of them that one and that one so let’s look at those okay this file right here this extension this is creating a property of our for each of the things the attributes and relationships to in my tweet object now why does it put them off in an extension here why didn’t it just put it in the other class can anyone think of a reason why this would want to be extension it’s a good reason actually which is that you might want to change these attributes and relationships over time.

 

As you’re working on your app you decide you need a new property okay well you’re going to regenerate this file when you do that you don’t want to blow away and the other code that you wrote for tweet right so it keeps all the stuff that’s being generated by Xcode when you do that create an advantage object subclass is keeping it all in one file so it can be constantly overwritten make sense see that’s why we put an extension there now let’s look at this wacky extension here what’s going on here well it’s these are obvious right text ID and created why are they optional because they’re nil when you first create a tweet it’s empty those things are all nil so it has to be an optional then look at this one tweeter as promised again Twitter user now I told you the tweeter since is a relationship it would be an ns-managed object but the system is smart enough to say well not only as an annus manager object but I know it’s a twitter user okay which is this other class over here that it created by the way sometimes when you generate a whole bunch of things at once it will miss that okay it’ll be it won’t notice it’ll just have this be ns-managed object just regenerate them again

Leave a Comment