Displaying Details

Start course
Overview
Difficulty
Intermediate
Duration
3h 18m
Students
11
Description

In this course, we're going to build a Foursquare Clone using Parse. Parse provides you with a great tool to work in cloud servers and you will learn the advantages and disadvantages that come with it.

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills

Prerequisites

To get the most out of this course, you should have some basic knowledge of iOS.

Transcript

Hi. Within this lecture, we're going to process this information that we got from Parse so that we can show the place name, the place type, the place atmosphere, and accordance to the user in our 'DetailsViewController'. So, how do we do that? Of course, I will start to see if objects is not nil or not. So, if objects is not nil, I can go and do my thing. If objects are nil, then there is no point of doing that at all. In most of the times, we have to go in a for loop in here because we're working with individual objects. But in this case, I expect to see only one object, right? So, I can try to create this object by saying, 'let myObject', 'object[0]'. So, this will give me the first object, first element of this array. This will ask me to first cast this. I believe it's kind of safe to do it in here because we have already checked to see if objects is not nil. But maybe in order to make this safer, we can check to see if objects that count is greater than zero so that we know the objects is not nil and there are some elements inside of our objects array. And this asks us to first cast this, but it doesn't make sense to do it like this. So, I can just control it like this, 'if objects.count > 0', then I believe this is much more safer, so that I can create my individual object like this without having to have a for loop because we're going to be dealing with only one object. And let's call this 'chosenPlaceObject', 'myObject' is not a very good choice for a variable name. So, in this 'chosenPlaceObject', if we say something like 'object (forKey)', I can just get whatever I want out of this, right? Like, 'if let placeName', then I can just say (forKey: "name"). Remember we have defined this as name and I will cast this optionally as string. And if that's the case, I'm going to change 'detailsNameLabel'. So, 'detailsNameLabel.text'', is now the chosen place name. So, I'm going to do this for other variables as well. So, let me copy this and paste it. So, I did this for 'placeName', let's do that for 'placeType'. So, this will be 'type'. Again, this will be a string and 'detailsTypeLabel.text' going to be 'placeType'. So far so good. Let's go for the third one. So, rather than 'type', I'm going to go for 'atmosphere'. So, this will be 'placeAtmosphere' rather than 'placeType', 'Atmosphere'.

So, this will be 'placeAtmosphere' and this will be 'detailsAtmosphereLabel.text'. So, that's all for labels, I believe. Now, we're going to have to get the result from. It asks us to give a 'self', I believe in front of everything. So, let's first wrap this and let's say 'self' in front of this because we're in a closure. And then, later on we're going to have to get the image, and also the latitude and longitude. First, go for latitude and longitude. 'if let placeLatitude = chosenPlaceObject', showing that object.

And '(forKey)', I'm going to go for 'latitude', and we're going to have to convert this to double, later on, because we saved it as string. So, let's create some chosenLatitude and longitude over here as doubles. We're going to need doubles eventually. So, let's say 'chosenLongitude', this is a double as well. And we're going to convert them in here to be double. So, I'm going to say, 'if let placeLatitudeDouble = Double (placeLatitude)'. So, this is fun if I can make this string into a double. Then I will say, 'self.chosenLatitude' is 'placeLatitudeDouble'. So, that's how you actually convert double to string and string to double, right? You know this.

And we're going to do the same thing for longitude as well, 'if let placeLongitude = chosenPlaceObject.object (forKey)', and the key that I'm looking for is the longitude, I'm going to cast this option as string and say 'placeLongitudeDouble', again 'Double (placeLongitude)'. And, then, later on, I'm going to say 'self.chosenLongitude' is now 'placeLongitudeDouble'. And the last thing that we have to do is just to get the image, and we know how to upload the image, but we don't know how to download it yet. It's fairly easy to do. We just get the data and we download the data, and if we can make it, then we're going to convert it to an image. So, let's see how we can do that.

First of all, I'm going to create something called Image Data because we have saved this as data, now the UI image, and we are going to download this as data again. Then, we're going to use that data to display it in an image view. So, let's say that 'if let imageData = chosenPlaceObject.object', and the key that I'm looking for is image. Remember, we had saved this column as image and I'm going to cast this as PF file, so 'PFFileObject'. And I'm going to check to see if we can actually download this. So, after you get your variable, you have to manually download this to get the data.

So, you're going to have to say 'imageData.getData'. As you can see, if you say 'getData', we have something in background. So, 'getData' in background. It either gives you data or an error, and this time I'm not going to check if error is not nil. Maybe we can just check if error is nil like this.  If  error is nil, then, you can check to see if data is not nil one more time. But I'm just gonna go for data, not this one. 'self.detailsImageView', this one, '.image'. So, this will be a UI image, and this will ask you for a data and the data that I'm looking for is the data itself. So, if you do that, it will ask you to first cast this. You can do that, of course, but again you can check to see if data is not nil like this to be extra cautious so that you won't crash your app.

So far, so good. So, rather than just zooming in the map, I believe we have done everything right. So, let's cut everything from here because we made the 'viewDidLoads', very filled up. So, let me create another function called 'getDataFromParse', and paste everything under that function and call that function under my 'viewDidLoad'. So, I believe now we are ready to test this. We won't get the map animating to the selected location, but I believe we can do that in the next lecture as well. Let's see if we can get the other details. So, let's open our simulator, and let's choose the only place that we have. Here you go. We see the image, we see the image name, the restaurant name, the type and everything. So, we don't see the data in my maps yet, but we're going to see it in a minute in the next lecture.

About the Author
Students
951
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics