Alamofire: Presenting Server Data | Swift 3, Xcode 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody my name is Kylie aka kilo loco from kilo loco comm and today we're gonna be going into Alamo fire we're gonna be going over how to present server data now this wasn't one that I was actually planning on doing I was planning on giving you guys a tutorial that somebody had suggested on how to scrape web data but then having a problem with that trying to figure it out so I decided to give you busted some of you have been having trouble how to get understanding how to get your JSON or any type of data however you got it from your local file or whether it was from a server into maybe a table view and I haven't gone over something like that so I decided to use Alamo fire haven't gone through this yet it's gonna be on the fly you're gonna be learning with me if I run into anything then you'll see me run into something but yeah super long tutorial just to go over how to present data from a server into a table view and yeah alright so the tools we're gonna be using Xcode eight point two point one Swift three cocoapods in order to get Alamo fire into our project we're gonna be using Alamo fire which is a networking library and then we're going to be using SWA P which is the Star Wars API so it's gonna be kind of a interesting thing to play with I said the skill level to beginner because I think that it's a fairly simple topic and I plan on going through and explaining everything that I'm doing so all you really need to know are the Swift language basics and then just knowing how to use how to create and use data models how to pretty much know how to create a struct and how to use a struct and like understand why it's working that way because I won't be explaining that part but you'll be seeing me make it so yeah you can learn that way too anyway let's get into it all right as always we're gonna do a new Xcode project and as always sorry for any background noise you know there's always something going down at the Li residence alright so the project single view application we're just gonna go with that I'm gonna call this star VARs yeah call it that and then we gonna put it in KL learning cuz that's where I learned yeah alright so first first thing that we want to do is we want to set up all our UI so let's go ahead and go into storyboards let's get all that taken care of and I will drag this make it look a little bit bigger alright so what we're gonna have we're gonna have two screens I'm gonna go go ahead get the data and then we're gonna present it and we're gonna have a detail screen so let's go ahead and do that I'll actually do this with a nav bar make it look a little bit pretty yeah I guess so yeah nav bar whatever alright we're gonna pin this to all three sides right I'm gonna set the height to 64 cuz we don't want it to be up there with a little battery so we're just gonna make it a little bit taller BAM all right update those frames give it a nice color Star Wars is yellow but I don't think that's gonna be a good color why is it not working let's go ahead click nav bar there we go bar 10th we're gonna go with my favorite purple and then title color will be white because it looks better like that and then we'll have a Barba and right bar button item throw it up there and I'll just call this get like get data right tint will be white as well so when I tap that that's when the the stuff will load I mean usually you want to put it in a viewdidload but whatever am I gonna do it this way just going with my good I feel like there's there's benefits to showing you how to use a button or something like that alright so get that table of you going we're gonna you know pin them pin it to all the sides right and then what are we gonna do so we're gonna get another view controller and we'll put it right there and we'll put in another nav bar I know I could have done a now navigation controller but man what are you gonna do and I know that I could have copied and pasted but what are you gonna do title font mmm that's not what I want and we'll just call this details yeah and we'll call this people cuz I'm gonna get a list of the people and we'll see that right now and you know we do the same exact thing pin it pin pin pin 64 right and then add the constraint BAM update those frames BAM and then for this it's not choosing the right thing alright so bar 10th same color right wanted to look at least uniform and title color obviously white uniform like I said and then we're just gonna have like a bunch of labels in there but we'll get to we'll come back to the to the storyboard when we're done now let's see I think I'm only gonna do a simple table view because I only want to show like the name and then after you tap on the name you get all the details about so that's I'm not gonna make a custom cell we don't need to really do that and then we just needed to drag control click and drag from the view controller over did this view controller and we're just gonna say show this way that we can use a segue and then well well don't do what I just did I don't know what I just did and then we'll just have another whoa what am i doing Barbra I'm sorry okay okay we're back okay and then this one's gonna call be called back and all its gonna do is it's gonna take us back right um this will be show and then we want the item tint color to be white as well right let's see so we press get and then goes okay so we'll call this back to people and we'll call this one people to details right well this one probably should a bit actually I'm gonna call this one details to people that makes more sense for now details to people all right so now we have our segways and if I were to run this it should work right right right right right right right right no because I don't have I want to be able to click anything to to call that Segway all right yeah but essentially it would work all right so I haven't really looked over the Star Wars API that much yet so I don't know what details I want to put in here but we'll get into that don't worry next what do we want to do so probably so we're not going to be calling the view we're not going to be calling the API right when we load so we don't need the view did load we're gonna do it when we do the get so we actually have to get the ape we have to get the people right so we'll have a function called this we'll get a IV action this is also a different way to you know connect your your actions to the interface builder our storyboards or whatever you want to call it so funk on get tapped right and then we do some stuff print and get and then we're also gonna have the table view right IB outlet week variable and then it's gonna be called table view of type UI tableview and for some wrap that what else do we have I think that's it since we're using a table view we want to do an extension and call the datasource so that we can you know update the datasource obviously so extension of our viewcontroller and this will be called this will be getting the UI tableview datasource and what the UI tableview datasource does is it allows us to access the different methods that are available through the data sources the data source is a protocol if you command click right here you go in you know you can see some of the ones that are required will be like a number of sections cell for row and then number of rows Oh maybe number of rows isn't required interesting anyways you can see all the optional ones that you can call in here I usually just use three of them so we want to do number of sections in row and right now we're just we only have one so we don't need multiple sections number of rows in section and then we're just going to return 0 because right now we don't have an array of the people that we want to do and then we do a cell for row right and then right now we just returned UI table view cell because it does return a UI tableview cell and we haven't configured it yet but yeah we just want to get stuff rolling so now the next thing they do is go back to the main storyboard we want to connect everything up so we want to connect our action our outlet and the datasource so okay let's see click on this we control click drag this is our table view so we'll click that so now that's connected we can control click drag back up to here we want to make sure that our data source is connected right and then we control click drag heap oh wait does it work that way it might not work that way so we could tap on get and control click drag here and then sent actions on get tapped right so now when I run this if everything is working then the tableview should well one it shouldn't crash because if we're missing anything that it will crash too when we press get it should print get in the logs because that's the only action that's in there and yeah you know obviously we should see the rest of our UI but that's if all goes well all right so we got the table view looks like it's gonna work okay now we press get and get alright so everything looks like it's working looking pretty golden alright now we can actually go about installing our our cocoa pods and you get Alamo fire going doo-doo-doo-doo so what you want to do head over to Google and you're gonna want to type in you guess it Alamo fire oh yeah it's gonna be the first github reference BAM just go ahead and click on that and then you're gonna want to scroll down it's gonna say it's gonna show you how to install it you can use cocoapods Carthage or Swiss package manager soon I'll be switching over to Swift package manager once they figure out how to get all their files out of the source of folder or whatever it was but for now we're just gonna keep using cocoapods so all you need to do is just copy this right here pod Alamo fire and then on the version is 4.4 that we're gonna be using open up your terminal your handy dandy terminal you're gonna change directory all the way to your project and I kept mine right here on the duck desktop inside KL learning so we're just gonna do that so change directory CD right we got a desktop autocomplete with tab and then you go KL learning right autocomplete with tab and then we want it what did we call this Star Wars right auto clip the tap alright so now as see we're in that directory and all we have to do is pod in it and it will do that for us and then we open the pod file just go ahead and pods for Star Wars I'm just gonna go and paste that one thing command s for save and then all we have to do pod oh wait is my excellent project no it's a it's closed okay so pod install make sure well you don't have to but I usually close my Xcode project whenever I install a cocoa pod so we just kind of wait for it to do its business and yeah while we're waiting what I'm gonna do is I'm gonna go back to Google and then we're gonna go to Star Wars API it's gonna be swap eco alright BAM and this is what we're gonna be using now you can select an individual like like so by doing people - one all we're gonna do is just do the people right and we want to get all of the people right so there's a total of 87 people or so it says and yeah I don't know why it says 87 but we're gonna go in we're gonna go into this results we're gonna go through each of these that it has and we're just gonna put them in the table view that's all we're gonna do and then we're gonna get the the details of each of those people and we're gonna put them in the details control yeah alrighty so now that it's the pot has been installed successfully all we do is open period opens it up in our finder so that we can go we want to make sure that we click on the EXCI workspace yeah alright and you know what I just realized is I'm working with the small font for you guys so let me just fix that because I like to work with the small font I could see more things but you might be watching this on your phone so I want to make sure that you can see - all right so as long as we got the workspace open we should have access to our Alamo fire now what we're gonna do Oh No hold on hold on we're just gonna do okay okay okay we're just gonna do a new file right and this is gonna be just a swift file cuz it's gonna be ours we're gonna make a data sort data service so we'll go ahead and call it actually I call it networking service networking service and that's what we're gonna call it and this one will import Alamo oh yeah and then this will be a class and we're gonna call it networking service right now we want to make sure that we have an instance we have an instance a singleton inside of our networking service so hold on command build so that Alamo fire is properly important I think that it should automatically do that but it doesn't hopefully maybe an Xcode 9 it will do that for us automatically we shall see anyway moving on let's see so we want to create a singleton just so that we can always use the same Alamo fire service so what we'll do is we'll do a static this means that it will allow us to access this property from without initializing the networking service and then we'll do static let shared we'll just call it going to call it shared and then this will be an instance of our networking service right and then we want to make sure that you can't initialize the networking service so we will do private in it and then we'll just leave a blank so only we can initialize a networking service inside of the actual service because it's back sis control is set to private so yeah alright next what do we have to do we have to get people right so let's do a funk call it get people right and then this function will have a completion Handler completion completion and it will be called like such for right now until I actually create a response handler for it and you'll see what I mean by that in a second so what we're gonna do is we want to do Alamo fire dot did what does it request and then we could just hold up hold up hold up want to make sure I autocomplete the right one request with URL convertible yeah we'll do that one okay so we'll just allow it accepts a string so what we'll do is we'll do actually response JSON because we want to have the response be returned in JSON right oh no hold on response JSON there's a better completion yeah see we don't want like all those different options we just want like just JSON right keep it easy keep it simple right why is it not doing it okay you're not autocomplete incorrectly request response response JSON completion Handler maybe I'm doing something wrong could be let's see completion Handler all right so it's times like these where you make little mistakes and then things don't work so what I'm gonna do is I'm just gonna go back over to the to the documentation for Alamo fire and we got to see exactly how they do it because I personally don't even like using Alamo fire and once you set this stuff up you're supposed to set up a networking layer and then you don't really have to deal with this stuff ever again once you set your networking layer that's what I'm doing bud oh did I do request yeah this is what I'm doing I almost fired that request you know what I think it's just the autocomplete is not working for me that's probably just that's probably all it is so what we'll do is we'll do this right and where is this coming from and then we'll just call this response and let's see if that works kind of reliant on some of those completely those autocompletes it looks like it's gonna work I'm gonna go back to swappi make sure that I could get the URL that I want so remember we just want we just want it all this so see didn't do that for us so I'm just gonna copy it all I'll copy that yes see no errors slash people slash there we go that's all we needed right yes and this is HTTP so notice that it's not secure so another thing that we have to do before we move any further is that we want to go in here into your info dot plist make sure we turn on app Transport Security you press ENTER it'll automatically turn into a dictionary now you press this little arrow you press the plus button allow our arbitrary loads press enter you turn the no and - guess what oh yes this way it works yes all right back to our networking layer so now we're gonna do this and then if we get a response it should be in JSON so what we need to do is if let JSON equal what is it response dot result dot value or something like that then print JSON oh you know what let's make sure that it is as JSON so I'm gonna put in a little type alias up here just makes things look a little bit prettier and we'll just call this JSON no not JSON encoding JSON and what JSON we will refer to JSON as a dictionary of string and any right so we'll cast this as JSON right and if all goes well then it shall print and we don't have a completion handler so nothing will happen with it yet but when we do tap get what we want to do is we want to have our net working service dot shared dot get people what's the proper one print we'll just say got here all right so let's go ahead and run it make sure that everything is working this will up well this will actually not even up here because we're not calling our completion Handler but what we should see in our log is we should see oh yeah let's see we should see the JSON pop up as expected so let's go ahead press get BAM and look at that we see our JSON isn't it lovely alright so what I usually do and to be honest I'm not sure if this is a proper way to do things is I turn all my networking requests responses into their own audit like into like objects so what I'm gonna do is I'm gonna turn this entire response into an object right so feel free to follow along with me this may not be the best practice but it makes things a whole lot cleaner when I'm writing my code so I feel like it is a good idea but I've never seen anybody do this okay so this is going to be the get people response right call whatever you want and then hold on hold on let's just let's let's stop that so that you're not using up all your processing power all right and then I'm just gonna make this a struct and we'll call it get people response right and then what we're gonna do is we're gonna initialize this it is going to be let's see is it available yes no wait does it fail the bull no I'm actually like making them how do I like making them I like making them throw that's the way I like to do it so we're gonna initialize this with JSON right and then it's gonna throw and the reason for that is better error handling this is how I usually do it you don't have to do this one it's just how I usually do right throat no throws right action and let's see when we get this JSON what we need to do do we solve this JSON is it still printed out for us yes okay so when we get this JSON we need to access the results right so first thing that we need to parse out what are the results so what we'll do is we'll do guard let results equal JSON keep oh shoot why did you go down there keyword is resolved is it plural results yes as and this is an array of JSON right else we also have to make another object throw let's see I'm pretty sure that this doesn't work because error is not a real thing it's a protocol I believe so we have to make a new a new object and we'll just call this well we could call it networking error right that's what I usually call it networking there and then we'll just do it eat'em of networking error and then it will be of type error and then we'll just throw in one case bad networking stuff this is usually how I do it you probably gonna want to name it better better more realistic things but just so that we're all on the same page of how things kind of go down in my code that's the way I do it networking we're gonna throw a networking error bad and it working stuff right all right so if we can't parse the JSON for whatever reason because it's not in this proper format we're just going to throw this error alright so now we have an array of JSON right but now we have we have access to like this oh right right and each of these are people so now we can finally make our person object right so let's go ahead and make that person object and we're gonna just choose like all the different fields that we kind of want to display on the next on the next screen so we'll have a person and then this will also be a struct so and this one will also be initialized with JSON because this is still in JSON format right so JSON but what we want to do is we want to make this available initializer because if for whatever reason we can't get that data back then we just need to get rid of this person object so this is the way that I prefer to do it anyway so let's see what things do we want to get out of this JSON so we have the birth year that seems like a good thing to get right so we'll do guard let birth year equal JSON and we want to just do birth year as let's see as a string Doe and then let what else do we want to get so we'll do let create it now we don't need that edit it no eye color sure I call it whatever JSON and then we'll do how do they do it what are they doing it underscores whoa what's up with the big s string right what else do we want do we want the films I don't think so gender seems like a something that you I mean not to be sexist or anything but I mean it's just something that seems like it be it a normal thing to kind of parse for right a string and then what else do we want hair color sure let hair color equal JSON and then do hair go go water as string what's next height yeah let height equal JSON I make sure you spell these right or else none of this is gonna work right because we're making sure that each of these keys are spelled correctly and they can be cast into a string object or else we don't move forward let's see what was after hi homeworld I think I want to get home in world because I want to show you guys how to get this I guess right let hope this one I'm gonna do a little bit different home world home world wait hold on yeah it's one word home world link right is going to be JSON home home home world as string now the reason I'm doing this as homeworld link is because as you can see it's not giving us the the data directly we have to do another request just to get that data so I guess I want to show you guys how to do that too if you ever need to do that okay so mass name and skin color right that's probably it let mass equal JSON mass let name JSON name and let skin color JSON skin color as string else returned you know so either you gonna let me parse all this data properly or we're not going anywhere okay next coat stripping out right there okay so uh it's just saying that we're not using those don't worry about that yet okay so for each person that's in this array we're gonna get all this data and we're gonna have access to all this data so what we have to do is we have to make properties for each of them to go through the entire process again so let birth year type string let I color of type string let gender of type string let hair color type strain you might be wondering like Kyle why did you do all these different properties just to get a point across well because you know sometimes people want to see a little bit more than the usual and if you're watching my channel at all they need probably just want to watch more than the usual so that's why I don't know it's better to have the information in one spot then have to go digging for all kinds of stuff right let's see name and then skin color and then homeworld link okay so this one I'm actually gonna make private and we'll get into that a little bit later but just because it's a little bit different we're gonna segregate them because racist and or we discriminate against the private today okay so just to make things look a little bit cleaner in your code I've seen this I've seen people do this so you know you could feel free to do this if you feel like it don't have to you it's not required it's a neat thing in Swift not every single language will ignore white spaces but Swift will just makes it look a little bit more lined up just so you know and we could do the same thing with this just they're all strings right so all I'm doing is just pressing tab you know you just tab them until they're lined up there's probably like a faster way to do it but I do it that way cuz I'm slow anyway on to the next now we have to since all these are required we have to initialize each of them and we're gonna do that by doing we'll just start at the top so we'll do self dot homeworld link is equal to home world link right self dot birth ear Berthier self dot eye color is equal to eye color self gender it's equal to gender self dot hair color I'm like you talking to you guys hair color so height you go to high self just mass is equal to mass then we'll just put you up there and then self die its name is equal to name self dot skin color it's equal to skin color alright and then we're just gonna make all of these look pretty too I could have probably done with the equal if I look better but whatever do-do-do-do-do making it look a little bit better all right so now we can initialize an object properly right so now we have to go back to our response which was dealing with the people right because remember that in our results it's an array of JSON and the results is just an array of people so now we have to parse through we have to pass in each JSON object into to try to initialize the person right so what we'll do is we'll do let and I'm going to use a map function it's pretty much like a for for in loop but it's a little bit cleaner and faster and what I'm gonna do is I'm gonna do let people equal results remember that results is an array so we can use map the map function on it well do map and then we'll do brackets right curly braces and then all we're gonna do is we're gonna do we want to initialize a person and we're gonna do it with JSON and remember that we're doing a for each in results right and each object in results is JSON and we were able to initialize a person with JSON so the shorthand for it would be dollar signs zero and all that means is that it's saying whatever type of object each of these results are which we know to be JSON then we're just gonna pass that in right there right the problem is is that it's gonna give us back optional people because remember that person with JSON when you do the initialize with JSON it's available so it might not work which means that we could give back a nil result so what we do to fix that is we just do dot flatmap now I know that you can use dot flatmap usually just right there and they'll do it automatically but when you are working with your own Swift classes for some reason it doesn't work and we're just gonna pass in a dollar sign zero right there because now it's now this optional people now we're passing in a person object right here so if it's if it's a true person object then it'll be added to our array of people if it's nil then it will just be skipped over right all right and then for our get people response we'll have this people property which will be of type array of persons right so all we have to do is to do self dot people is equal to people BAM all right so now we set up our our data model to handle the the response but we haven't implemented it yet so now we have to go back to our networking layer that's networking error I said networking layer and that would be networking service there we go okay so remember we're getting our JSON in here and we want to initialize our our get people response with that so so let whoa what am i doing oh we have to do with the try to do this right do actually you know what I don't like the way that this looks so I'm gonna just make it into a card statement return else and then we just replace this wood guard so we guard let JSON and then um wanna do catch because remember that and I'm not gonna do any error handling right now you have to remember that our get people response is it throws so yeah we just got to do it this way what am i doing what are you doing kind of let get people response equal get people response and then remember that we and this initialize this with JSON and we have JSON write this JSON object we're gonna pass in here and we have to try this so if that works then what we can do is we can say that our completion handler you know what we call this success because I'm not doing errors success we'll just call this success block whatever you call it whatever you want and then we have to make sure that this is at escaping okay so I just did a couple of different things right here let me explain what I did okay so since I'm not checking for errors right now and that's just because I'm just doing a tutorial and what you need to do if you're gonna be going into production you're gonna want to check for errors but we're not gonna be doing that right now so I'm just gonna say it's always gonna be success and if not then this block would never be called so I decided to rename it and success and then when I want to call my completion handler which is gonna now be called success block inside of this inside of this method I'll be using success block instead of completion we have to specify at escaping because the contents of this data will live outside the scope of this method which means that this block of code is gonna finish running but the data from this block of code is going to be used in other places from our app so we just have to specify that all right so get people and then now when we call our success block what we do is we just pass Oh hold on I also need to specify that success block is only gonna return I get I get get people response right so we have to pass in I get people response so success block and then we pass in our get people response that we just checked for right here right and we pass it in right here so now when we go back over to our view controller we will be able and this will be called a response and and we'll be able to print out our response so now this response object that it's being passed into this block of code will be our get people response so let's go ahead and run this and let's see what's going on if all goes well you know you know what I mean it's been a weird day outside in Southern California it's getting cloudy and stuff watch if I option-click my response it'll show see it's a get people response so when we print it will hat we'll actually be able to see our people object our people property or whatever so look at that so now we have a lot of people in there right see it's art get people response and then it's the people and then it's this array and then that's where the array ends you can see all their different properties in there okay so now what I need to do is I need to actually save all these people I have to have like a reference to all these people in my view controller so what I'm gonna do is I'm gonna do a variable called people and this is going to be of type you guessed it on a right Oh actually we're gonna make it like this because we want to have a initial value call it uh no no this person right array of person and then it's gonna be an empty array this way we can call the purse the people dot count right here so that it updates the table properly right already so it's gonna be an array of person so all we're gonna do is now that we have this response what we'll do is we'll set our people object to the the people property inside of our response so self dot people is equal to response dot people see how clean that is that's pretty damn clean that's pretty clean no look at how small our viewcontroller is whoa 37 lines of code beat that all right anyway okay so now we're just setting our people up in here and then we have to update our table view so self dot table view remember we're inside of closures so you get the you cell reload data right and that should be good now we just have to set up the cell for the table view so we're not gonna be doing excuse me we're not gonna be doing anything special with this cell all we want to do is present the name right so we'll do a guard let Cell equal table view dot DQ let's see DQ D a table view dot d I was waiting for it to autocomplete DQ reusable cell with identifier I haven't set the identifier yet but I'm just gonna call it actually we don't even need to do this so we're just gonna use the default cell right so uh let cell equal UI table view cell yeah yeah we're just gonna use the default cell so we don't need to do it usually what you would want to do is you want to do the DQ reusable with identifier but since we only have a limited amount of data and we're just wanting to present it then we're just gonna do it like this we're not gonna go into how to optimize your table view and all that other stuff so the cell dot title wait no it's the text label text label dot text is going to be equal to our people array and then we want the individual person for each of those rows so we do index Road dot index path dot Rho so that'll be that individual in that row that's possible in that row right dot and we want to use their name and that's a string so it will work and all we want to do is return our cell all right so now when I do that it should update let's see UI label not unwrapped what Oh text label I think this is optional yeah there we go I'm about to say I could have sworn I did it all right hmm will Kyle be able to do it find out on this week's episode of k-l learning no this isn't a show I'm sorry anyway okay so now okay so now when I press get BAM look at that you got it see it's all in there and since Alamo fire is thread safe which means that it automatically brings the responses into the main thread it will update the UI properly so now what we want to do is when we click on one of these we want to go in and see the rest of the details now we don't really have the next screen set up but what we need to do first is we need to add the delegate that will allow us to click on the cell so extension view controller is of type you I table view delegate so so this is just a different protocol and what we can do with the delegate is did weight self a row and then did select that's what it was did select row at index path that's the one that we want to use so when we select a row it the index path we want to perform a segue right perform segue with identifier and what did I call it I called it like I called it people to details something like that remember back there when I called it the segue and then the sender you know what I want to send that object so what I will do is I'll do people and we'll do the same thing as right here we want to send that specific person right so index path dot row so now when we perform this perform the segue it should be sending our person but first we have to get the we have to prepare the the segue prepare for segue right you want to use this one and then want to verify that the segue is going to be called what we think it's gonna be called so guard let segue or let's see guard let ID equal to segue dot dot identifier you know how do we want to check this we want to say guard segue identifier is equal to people to details else or return right guard yeah yes we want to make sure that that's the correct Segway and then we want to also let our we want to get our destination so let destination or actually we'll call it details because that's what our destination of detail details BC which we haven't created yet is equal to Segway dot destination and we actually have to create the other view controller our details view controller in order to cast this as the details VC so we'll make a cocoa touch class right right right right and we'll call it uh I don't know why command all doesn't work on that one but okay details BC and then we'll just do create okay we don't need any of this definitely don't need that and what we want to do is we want to make sure that we update our storyboard make sure that it's of the right class type I guess what you would call it oh hey Kyle you talk a lot a lot going on right here something so simple really really yeah I'm sorry man sorry okay so we'll just call this details BC right and then we'll also make sure that it's this right that works okay so now we should be able to go to the blank screen so we want to make sure we do a guard right so guard actually I could just throw it on to the to the previous one right just throw a comma right there as we're gonna call this details BC and then that's when we go to the next time we do our else statement else return else come on Kyle what are you doing there we go so now we want to make sure that our details VC and we want to pass it a person object so we have to have an interceptor I don't know if that's a real word I just you know interception right so we'll do a bar person is of type person right and want to make sure that we tell I know that it's gonna be there viewdidload we're just gonna use this super dot you did did load and then I'll print the person that we selected so person okay uh yeah that looks like it should work so now we have something that will actually receive what we're gonna pass to it so we're gonna do details B C dot person is equal to oh you know what I didn't grab that person object that that we that we should be receiving from it so the sender remember that we said the sender should be of a type person the weird thing about Swift three and hopefully not so forth but Swift three is that if you send a Swift object which is your your own custom object and pass it as any at least for a segue I don't know about any other anything else when it gets passed as any it loses like the the bridging between swift and Objective C to be able to pass that object kind of gets messy so you actually have to cast it so we'll do guard let sender will do guard let person equal sender and then you have to do as any object as a person I don't know why it's like that it shouldn't be like that but whatever return this should work there should be no problems with this so now what we'll do is we'll do details B C dot person is equal to this person that I just made rent and let's clean that up a little bit yes and there and uh there and let's um that should work okay that should work okay so now when we perform our segue we should be passing in a person when we do select it's gonna perform a segue to perform that segue to be specific it should be passing that person in and when we prepare for the segue we set the person object in the next view controller all right let's so let's see if this works I don't know if it works I haven't tried this it might not work and then I'll just have to stop the video right there and be like oh yeah it just worked yeah I'm sorry this was supposed to be a different video I was not prepared for this it's been a busy weekend it's my one-year anniversary a lot of stuff has been going on in my personal life and oh I'm tired and I still got to do a whole nother video for you guys alright so we're gonna get right alright so let's go on with Darth Vader so we hopefully when we click Darth Vader we should see it print something because we're going to a different view controller so Darth Vader nope doesn't oh you know why because I didn't connect the delegate do so that's usually the problem got to make sure you connect the delegates yes so we're gonna click on this one option yeah no control whoa control drag onto the yellow thing delegate see now we do it hopefully it'll work that's if I named everything properly which you always want to make sure that everything's spelled properly because if you don't spell one of these little things properly it won't work but let's see what happens it might work it might not work don't know if it will work Darth Vader again yay it worked guys yeah yay okay so you should know how to get this data onto the screen but in case if you don't we're gonna go through that right now so now we have to create a labels for each of these okay so labels are gonna be our best friend right now label and I'm only gonna make like one like one really long label for each of them I'm not going to make it look all pretty or anything like that cuz no way so we're just gonna option drag option drag option drag option drag option drag how many properties do we have in here 1 2 3 4 5 6 7 8 9 we have 3 6 ok so we need three more ok so we got three three three yeah okay so now we just want to change the name of these whatever right so we'll do name we'll do and this is this doesn't really matter I'm just gonna go in order after that cuz name is like the one they usually put at the top right so we'll do a home world and we'll actually get the home world we're gonna do the label which will be uh I don't know I'm saying birth year this one will be a I color whatever we're not going to see any of this gender hair color yeah after that its height mass and then a skin color okay let's see okay so this is the order that they're gonna be listed in when we actually fill these out but first we need to create references in our viewcontroller the details of view controller to each of these so I like to generally put my outlets at the top do them wherever you please but this is how I'm going to do it okay okay name do I'm gonna call it label yeah and then home world so think that's where the only one word I would have thought it was to a birth year year I color gender hair color height come on masse and skin color all righty BAM let's close out the Simula the simulator because that was probably what was making everything go little bit slower okay let's make our viewcontroller look a little bit prettier all right and skin color okay so now we have access to each of those labels so we can populate them properly with our person object so now we'll just do that in the viewdidload because we already have all that data so what we can do is we could do name dot text is equal to our person dot name right we're gonna skip over home world for this second because we have to do some stuff to that but birth year is equal to person dot berthier nope can't do that because we have to do dot text text there we go I color dot text is equal to person I color gender dot text is equal to person dot gender hair color dot text is equal to person you guessed it dot hair color height dot text is equal to person dot height oh is that gonna be problem okay hold on I don't know if this is gonna be a problem because height onokay height that text is equal to our mass text is equal to person dot mass for some reason I thought that the viewcontroller had a property called height which wouldn't make any sense but you know it could have which is why you have to be careful with what you name some of these properties person dot skin color okay so now when we go in everything should work when we click on the on one of our people with the exception of the home world because remember that home world is just a link and we want to actually get that property so we actually have to do another request just to get that property but let's go ahead and see if everything else works so we'll go with uh you know do want OB one you know what let's do r2d2 r2d2 homeworld as you can see it's still homeworld let's see forgot the layout this is kind of a weird thing to work on so we have to get it again there we go okay Luke Skywalker right okay so this is the year that he was born some whatever I color a gender hair color height art weight was it mess no height and then mass and then uh skin colors fair okay so now what we want to do is we want to actually get the homeworld and then we'll be done right hopefully so what we have to do remember that we set this one private so we wouldn't even have access to it so what we want to do is we want to do a function called home world home world and then this is gonna have a completion handler in it because we have to we have to actually make a networking request to get the actual data that we want from it and um or you know what let's see this is going to return a string right this is gonna return a string and void right here so we'll have a completion handler that looks like that but we need to call it completion right and then we don't really want to give it a name because we're not doing anything with it and then okay so this is what has to happen so we have to do another request and what we're gonna do is we're gonna go back to our networking layer so that we could get that request what's going on why are you complaining you don't know what you're talking about I do oh not the networking error but the networking service so we go back in here we want to do another one called get home world get a home world and then let's see aha crap we're gonna make a whole nother response for this one uh you know what we're not gonna make a whole nother response for it we're just gonna parse it out and just pass it we're gonna go a little bit cheapy on this one so completion and then the completion is going to be at escaping and then this will you know this will pass this will also pass a string because we're just gonna pass it right and then void and let's see what am I missing I am missing a parenthesis so so what we'll do is we'll do the same request so Alamo fire dot request and we want to do the URL convertible I don't know what the link is oh wait I have it right here so oh we have to actually pass something in here so we have to do a home world link that's gonna be able type string and we're gonna pass it in here home world link and then dot request and then we're no no not request we want response ok because we want JSON to come back right so oh it worked this time okay so response the autocomplete work this time okay so that's good and what are we going to do okay so the response we want to make sure that it's JSON so guard let JSON equal response dot result dot any I don't know dot value as JSON else just go ahead and return and then let's see how that data comes back so that we don't really have to go through it so when we get the home world if we could find it home world if I copy this and put it into the you of the address bar or whatever you call it then it will automatically generate this and it will tell us how it's coming back so we want to just grab the name so it's coming back as JSON so it's coming back just as a dictionary right and we want to grab the name object so that makes it easy so guard let name equal JSON or actually whatever JSON and then name as string else returned and then we just replace this with the comma enter do that all right so now we have a name and we can do our completion and we'll say name so now when we call this get home marled in our person object so networking hey that's my baby networking service dot shared dot get home world we could pass in our home world link and we'll call it a name and our completion will call our completion with this name so now it'll just essentially be passed and passed again and then we also have to mark this as escaping so now we could just call homeworld and it will pass us a name so when we go back to our details all we have to do is we have to do this we'll do person dot homeworld we press ENTER say name and we'll do self dot name dot text is equal to native so it will update but it'll just take a second to update because we have to actually make that request and yeah so now when we click on a person we should have all their information filled out and we'll start off with Darth Vader again all right so we're going to get it I'm gonna go we should do obi-wan all right obi-wan home world watch wait for it wait for it if it even works it might not have worked oh no it didn't work okay so got to go back I have to figure out why it's not working so let's see our response it should be coming in as JSON let's see if we're even getting here print got here yeah that's weird that's not working okay so and then print nope this is me this is how I do bug it's not the most elegant way of doing it but it's one of the ways of doing it so let's just make sure that our networking layer is all good first whoa what is that what's going on with the simulator so we get it right BAM obi-wan BAM and then we got there right so we've got the completion so now our person object should be calling it interesting so completion and then Oh am i stupid oh I am stupid what the heck oh you guys probably saw that I'm sorry I'm being an idiot for some reason I was calling it name when I should have been home world home world it doesn't matter what you call this this is just a name space home world I got confused Wow tired and not paying attention that's what happens home world and then home world and then oh okay so now it should work because I'm actually pointing it at the right label stupid me see I was setting the name to obi-wan and then I'd go down here and then it would point to whatever his home world was so Josh said yoga whatever it is so we do get get a response bam bam stooge on baby yeah look at it all of that nice and sexy so that is how you present data from the server that's all you know like comment subscribe all that good stuff that's all you need to do I am just going to go ahead make this in github so that make sure that you have access to it yes because that's what I do good old pal buddy boy how does where we can call this Alamo buyer credit presenting ha no yo initialize what the readme sure why not okay so now we won't who yet do that BAM go back and then we're gonna get in it yes so we'll then do get add remote origin pace that in there oh no what remote rim remote get remote Oh get remote add origin and then get pull origin master and then you enter a password and then you do the get add all get commit - am finished that Star Wars tutorial y'all and then you get push origin master and then ya enter in yeah password and then you know make sure to refresh your page button and then you look at it and it's like oh yeah I'm on yeah it's in there guys so if you have any questions feel free to reach out to me once again you're gonna want to reach out to me on slack my applied Swift slack team feel free to reach out to me on their Twitter in the comments never through email because I don't check my emails like that and then uh yeah any suggestions feel free to send them off to me but yeah sorry for the long video but that's how you do it anyway see you guys later once again my name is Kylie aka kilo local from keylolo cocom and as always remember to code passionately yay
Info
Channel: Kilo Loco
Views: 13,704
Rating: undefined out of 5
Keywords: swift get request, swift post request, swift json request, swift json get request, swift json post request, swift json networking, swift networking, swift json serialization, ios networking, swift alamofire networking, swift alamofire, swift api call, swift get data, swift post data, how to call an api in swift, swift networking for beginners, swift api for beginners, swift json for beginners, swift 3 alamofire, how to use alamofire, swift swapi, swift star wars api
Id: 0S75Wdj6ggs
Channel Id: undefined
Length: 87min 43sec (5263 seconds)
Published: Sun Jun 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.