User Tools

Site Tools


ios-labs-f15:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
ios-labs-f15:start [2015/10/21 18:55]
achin [Lab 7 - Models, JSON, Tables and more]
ios-labs-f15:start [2015/10/30 12:04] (current)
mbarboi
Line 8: Line 8:
  
 [[https://​docs.google.com/​presentation/​d/​1HcV2kBdjdOZzGV9D2N8sc_OOvNorVjmFYu4w7wikexA/​edit?​usp=sharing|Week 3 Slides]] [[https://​docs.google.com/​presentation/​d/​1HcV2kBdjdOZzGV9D2N8sc_OOvNorVjmFYu4w7wikexA/​edit?​usp=sharing|Week 3 Slides]]
 +
 +[[https://​docs.google.com/​presentation/​d/​16BTLmHk1JQzTwAoZimGI0xcz4n8HvsrPGPOyawa6TE4/​edit?​usp=sharing|Week 4 Slides]]
 +
 +[[https://​gist.github.com/​damouse/​d34a8506aad904943f8a|Closures and Optionals Playground]]
 +
 +
 +=====Midterm Topics=====
 +These are the basic topics covered this semester presented as *first principles* (see a great explanation of first principles from [[http://​www.youtube.com/​watch?​v=NV3sBlRgzTI|Elon]]). This means that the first tier of information requires no knowledge of iOS programming to understand and is easily accessible. The second tier relies on a comprehensive understanding of the first tier, and so on with the last. Each page should be the pure essence of the topic, the bare minimum needed to understand and use it. 
 +
 +Each of the pages are split into three categories based on your TA's somewhat arbitrary sense of difficulty. ​
 +**Consider this a topics list for the exam,.** You will not be tested on topics followed by an asterix.
 +
 +===Basics===
 +  *[[ios-labs-s14:​basics-mvc|MVC Theory]]
 +  *Basic Interface
 +    *[[ios-labs-s14:​basics-ib|IB]]
 +    *[[ios-labs-s14:​basics-views|Common UIViews]]
 +    *[[ios-labs-s14:​basics-tables|UITableView]]
 +  *Basic Controller
 +    *[[ios-labs-s14:​basics-controllers|ViewControllers]]
 +    *[[ios-labs-s14:​basics-navs|UINavigation Controller]]
 +  *Basic Model
 +    *[[ios-labs-s14:​basics-nsurl|NSURLSession]]*
 +    *[[ios-labs-s14:​basics-json|JSON ]]
 +  *Basic Swift
 +    *[[ios-labs-s14:​basics-objects|Objects]]
 +    *[[ios-labs-s14:​basics-methods|Methods]]
 +    *[[ios-labs-s14:​basics-structs|Data Structures]]
 +    *[[ios-labs-s14:​basics-delegation|Delegation]]
 +  *Basic Development
 +    *[[ios-labs-s14:​basics-git|Version Control]]
 +    *[[ios-labs-s14:​basics-xcode|XCode]]
 +    *[[http://​blog.teamtreehouse.com/​understanding-optionals-swift|Optionals]]
 +
 +===Intermediate===
 +  *Intermediate Interface
 +    *[[ios-labs-s14:​intermediate-ui|Designing and Building UI]]
 +    *[[ios-labs-s14:​intermediate-cells|Custom UITableViewCells]]
 +    *[[ios-labs-s14:​intermediate-views|View Hierarchy and Manipulation]]
 +  *Intermediate ViewControllers
 +    *[[ios-labs-s14:​intermediate-modal|Modal Segue]]
 +    *[[ios-labs-s14:​intermediate-tabs|TabBarControllers]]
 +  *Intermediate Models
 +    *[[ios-labs-s14:​intermediate-stores|Persistent Stores]]
 +    *[[ios-labs-s14:​intermediate-defaults|NSUserDefaults]]
 +    *[[ios-labs-s14:​intermediate-api|Intermediate API]]
 +  *Intermediate Swift
 +    *[[ios-labs-s14:​intermediate-arc|Memory Management]]*
 +    *[[ios-labs-s14:​intermediate-notif|NSNotification]]
 +    *[[ios-labs-s14:​intermediate-scope|Variable Declarations]]*
 +  *I/O
 +    *[[ios-labs-s14:​intermediate-gps|GPS]]*
 +  *Intermediate XCode
 +    *[[ios-labs-s14:​intermediate-prov|Provisioning]]*
 +    *[[ios-labs-s14:​intermediate-debugging|Debugging]]
 +    *[[ios-labs-s14:​intermediate-pics|Pictures]]
 +
 +===Advanced===
 +  *Advanced Interface
 +    *[[ios-labs-s14:​advanced-animations|Animations]]*
 +  *Advanced Models
 +    *[[ios-labs-s14:​advanced-core|CoreData]]*
 +    *[[ios-labs-s14:​advanced-singleton|Singletons]]
 +    *[[ios-labs-s14:​advanced-social|Social]]*
 +  *Advanced Swift
 +    *[[ios-labs-s14:​advanced-blocks|Closures]]
 +  *Advanced XCode
 +    *[[ios-labs-s14:​advanced-plist|Project Settings]]
 +    *[[ios-labs-s14:​advanced-frameworks|Frameworks]]
 +
  
 ====== Lab 1 - Sept 11 2015 ====== ====== Lab 1 - Sept 11 2015 ======
Line 367: Line 437:
 DONE!! DONE!!
  
-====== Lab 7 - Models, JSON, Tables and more ====== +====== Lab 7 - Models, JSON, Tables and more - 23rd October 2015 ======
-For today'​s lab, we intend to give you a more holistic view of iOS app development using Swift. This week’s lecture threw some light on Models and data representation like JSON, and XML. This tutorial would build upon those and  would also help recap of the concepts you have already learned. Hopefully, making things crystal clear.+
  
-The only way to learn the code and get used to the syntax is the struggle with it a little bit. There are two things you should keep doing to make progress.+===== Introduction ===== 
 + 
 +For today'​s lab, we intend to give you a more holistic view of iOS app development using Swift. This week’s lecture threw some light on Models and data representation like JSON, and XML. This tutorial builds upon those concepts and  would also help recap the concepts you guys have already learned. Hopefully, making things crystal clear. 
 + 
 +The only way to learn the code and get used to the syntax is the struggle with it a little bit.
 use the internet, specifically StackOverflow. If you don't know how to do something, or can't get the syntax right (more likely) Google the method name, what you're trying to do, whatever. use the internet, specifically StackOverflow. If you don't know how to do something, or can't get the syntax right (more likely) Google the method name, what you're trying to do, whatever.
-Today’s task 
 We are going to create an app which lists the names of people in a table view and on clicking a row, takes you to another scene which shows some more information about that user. In this project we are going to download a json file over the network, parse it, store the data in a model and supply it to the table view. Whoosh! Lot to do, so let’s get started. We are going to create an app which lists the names of people in a table view and on clicking a row, takes you to another scene which shows some more information about that user. In this project we are going to download a json file over the network, parse it, store the data in a model and supply it to the table view. Whoosh! Lot to do, so let’s get started.
 +{{ :​ios-labs-f15:​front-scene.png |}}
 +
 +{{ :​ios-labs-f15:​desc-scene.png |}}
  
 Create a new Xcode project, this will be a Single View Application because we're going to do everything from scratch here, not using any of the other templates. Let’s call this "​nameApp,"​ check that it's in Swift and for iPhone. Create a new Xcode project, this will be a Single View Application because we're going to do everything from scratch here, not using any of the other templates. Let’s call this "​nameApp,"​ check that it's in Swift and for iPhone.
- + 
 +{{ :​ios-labs-f15:​3.png |}} 
 Now let’s start from scratch and create everything from a blank storyboard. So delete the view controller from the storyboard and it’s corresponding swift file (viewcontroller.swift). Select “Move to trash” when the popup comes. Now let’s start from scratch and create everything from a blank storyboard. So delete the view controller from the storyboard and it’s corresponding swift file (viewcontroller.swift). Select “Move to trash” when the popup comes.
- + 
 +{{ :​ios-labs-f15:​4.png |}} 
 Keep the appDelegate file, do not delete that as it handles the start up of an iOS application (Remember???​ Lifecycle of an app).  Keep the appDelegate file, do not delete that as it handles the start up of an iOS application (Remember???​ Lifecycle of an app). 
    
 The first scene we want to show is a table view where the list of names will be displayed. Drag a Table view controller inside the storyboard from the object library. This is our initial scene, so go to the attributes inspector and make this as the initial view controller (There is a check box). The first scene we want to show is a table view where the list of names will be displayed. Drag a Table view controller inside the storyboard from the object library. This is our initial scene, so go to the attributes inspector and make this as the initial view controller (There is a check box).
- + 
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.19.55_pm.png |}} 
 + 
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.21.05_pm.png |}} 
 Now we want to move from this, as in we want to click on one of the rows in this table and move to a view controller which would show me the description corresponding to this name. Now we want to move from this, as in we want to click on one of the rows in this table and move to a view controller which would show me the description corresponding to this name.
    
 So create another view controller, and add a label to it (You might have to zoom out/zoom in to do these tasks). Find a Label and drag it on. Put it somewhere roughly in the middle of the screen in the lower section. You can configure auto layout if you wish to (Now you guys must be expert in it).  So create another view controller, and add a label to it (You might have to zoom out/zoom in to do these tasks). Find a Label and drag it on. Put it somewhere roughly in the middle of the screen in the lower section. You can configure auto layout if you wish to (Now you guys must be expert in it). 
- + 
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.22.38_pm.png |}} 
 At the moment, there are no segues between any of this yet.  Let’s create one. Zoom in and do a ctrl + click from the table view controller to the controller which has the label. (Remember drag from the top - prototype cells). Select “show” as the segue type. At the moment, there are no segues between any of this yet.  Let’s create one. Zoom in and do a ctrl + click from the table view controller to the controller which has the label. (Remember drag from the top - prototype cells). Select “show” as the segue type.
- + 
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.24.31_pm.png |}} 
 We would also like these view controller to be contained inside a navigation controller, because we want to be able to click the back button. Click on the first view controller, come up to editor tab and tell it to embed in navigation controller. We would also like these view controller to be contained inside a navigation controller, because we want to be able to click the back button. Click on the first view controller, come up to editor tab and tell it to embed in navigation controller.
- + 
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.25.25_pm.png |}} 
 Fine, right now we don't actually have any code files that would tie all this together so let’s get cracking on that. Fine, right now we don't actually have any code files that would tie all this together so let’s get cracking on that.
    
-If we look at the code itself, the only thing we have is an AppDelegate,​ there aren't any code files that correspond to these different View Controllers. The last thing I'm going to do here is coming back up to the Table View Controller, this first one. Or the first one of our real content. Here I'm just going to double click that bar at the top. And type in "​Names,"​ that will show up in the navigation bar when we're looking at our names list.+If we look at the code itself, the only thing we have is an AppDelegate,​ there aren't any code files that correspond to these different View Controllers. Here I'm just going to double click that bar at the top. And type in "​Names,"​ that will show up in the navigation bar when we're looking at our names list. 
 + 
 +===== Adding Class code =====
  
 This application is going to show a list of names and some description so let’s go ahead and create a custom class to contain that data.  This application is going to show a list of names and some description so let’s go ahead and create a custom class to contain that data. 
  
-From Xcode'​s File menu, add a new file. Under the iOS Source menu pick the empty Swift file, Click Next, I will call this one Name with an uppercase N. The one I'm going to pick is just an empty Swift file.The one I'm going to pick is just an empty Swift file.. +From Xcode'​s File menu, add a new file. Under the iOS Source menu pick the empty Swift file, Click Next, I will call this one Name with an uppercase N. The one I'm going to pick is just an empty Swift file.The one I'm going to pick is just an empty Swift file. 
- + 
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.28.34_pm.png |}} 
 I'm just going to verify it's adding it to my current project called nameApp, and click Create. So I'll create this as a new class, and I will call this Name. It’s quite common to use the base class NSObject as it’s parent, but for this example we don’t need it. I'm just going to verify it's adding it to my current project called nameApp, and click Create. So I'll create this as a new class, and I will call this Name. It’s quite common to use the base class NSObject as it’s parent, but for this example we don’t need it.
 +
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.28.51_pm.png |}}
  
 And all I'm going to add here are 2 properties, they'​re all Strings, I'm going to have a name and a description. I am going to define these in a struct. Structs are almost as powerful as classes in Swift, so this will be perfect for our simple app.  And all I'm going to add here are 2 properties, they'​re all Strings, I'm going to have a name and a description. I am going to define these in a struct. Structs are almost as powerful as classes in Swift, so this will be perfect for our simple app. 
  
 So we're creating a struct called Name rather than a class called Name, everything else here is the same.  So we're creating a struct called Name rather than a class called Name, everything else here is the same. 
- + 
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.31.56_pm.png |}} 
 I can pretty much ignore the Navigation Controller. But none of these have any corresponding code files yet. So I need to add custom view controller classes for each of the scenes. The first view controller is a special view controller, so we want to create a class which gives us maximum benefit. ​ I can pretty much ignore the Navigation Controller. But none of these have any corresponding code files yet. So I need to add custom view controller classes for each of the scenes. The first view controller is a special view controller, so we want to create a class which gives us maximum benefit. ​
  
Line 410: Line 507:
 Click Next, and the class I want to create here will be the NamesTableViewController. Now, it's crucial that this is made as the subclass of UITableViewController,​ not UIViewController,​ because that will provide some extra functionality that we would not get from UIViewcontroller. ​ Click Next, and the class I want to create here will be the NamesTableViewController. Now, it's crucial that this is made as the subclass of UITableViewController,​ not UIViewController,​ because that will provide some extra functionality that we would not get from UIViewcontroller. ​
  
-The advantage of inheriting from a TableViewController is we get a lot of default code methods. Feel free to ponder over these function ​and try to understand what are they for.+The advantage of inheriting from a TableViewController is we get a lot of default code methods. Feel free to ponder over these functions ​and try to understand what do they correspond to.
  
 We also need a controller for the second screen. So follow the same process of creating a new file but this time inherit from the normal UIViewController. You can call this so I'm going to make this optional by just adding a question mark at the end.so I'm going to make this optional by just adding a question mark at the end.so I'm going to make this optional by just adding a question mark at the end.so I'm going to make this optional by just adding a question mark at the end.DisplayViewController. We also need a controller for the second screen. So follow the same process of creating a new file but this time inherit from the normal UIViewController. You can call this so I'm going to make this optional by just adding a question mark at the end.so I'm going to make this optional by just adding a question mark at the end.so I'm going to make this optional by just adding a question mark at the end.so I'm going to make this optional by just adding a question mark at the end.DisplayViewController.
 +
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.38.32_pm.png |}}
  
 There is one catch, we haven’t yet linked these new classes to their scenes in the storyboard. Without this, the scenes don’t know which code belongs to them. To do that, click on each scene and go over to the identity inspector. There is one catch, we haven’t yet linked these new classes to their scenes in the storyboard. Without this, the scenes don’t know which code belongs to them. To do that, click on each scene and go over to the identity inspector.
  
 Instead of the scenes using the generic viewController,​ change it to the classes just created. Instead of the scenes using the generic viewController,​ change it to the classes just created.
-  + 
-Creating Models for our nameApp+{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.41.07_pm.png |}} 
 + 
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.41.16_pm.png |}} 
 + 
 +===== Creating Models for our nameApp ​=====
  
 Although the navigation controller is our initial view controller, it doesn’t do much. For our app we need to fill up the table view controller with multiple rows of content. ​ Although the navigation controller is our initial view controller, it doesn’t do much. For our app we need to fill up the table view controller with multiple rows of content. ​
  
 Go to the NamesTableViewController.swift,​ and create an array of names. Go to the NamesTableViewController.swift,​ and create an array of names.
- + 
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.49.43_pm.png |}} 
 This creates an empty array. We'll start adding to it in a minute. This creates an empty array. We'll start adding to it in a minute.
  
Line 429: Line 534:
  
 So into the DisplayViewController,​ add a var currentName. So into the DisplayViewController,​ add a var currentName.
 +
 +{{ :​ios-labs-f15:​screen_shot_2015-10-21_at_5.52.22_pm.png |}}
  
 We need not worry about always providing an initial value for this as soon as the ViewController is instantiated,​ make it optional by adding a question mark, so I'm going to make this optional by just adding a question mark at the end. We need not worry about always providing an initial value for this as soon as the ViewController is instantiated,​ make it optional by adding a question mark, so I'm going to make this optional by just adding a question mark at the end.
Line 434: Line 541:
 Now we intend to programmatically change the label (Remember the IBOutlet mantra we learnt in the first lab). So use the assistant editor and add IBOutlet for the label. Control and drag. Simple. Now we intend to programmatically change the label (Remember the IBOutlet mantra we learnt in the first lab). So use the assistant editor and add IBOutlet for the label. Control and drag. Simple.
 Call this detailsLabel,​ verify that it's an outlet of UILabel, and click Connect. ​ Call this detailsLabel,​ verify that it's an outlet of UILabel, and click Connect. ​
 +The displayViewController class should look like this.
  
- +{{:​ios-labs-f15:​screen_shot_2015-10-21_at_5.55.16_pm.png |}}
- The displayViewController class should look like this.+
  
 The job is not over yet The job is not over yet
  
 The first part of the application that needs some data loaded into it is going to be our Table Views. So go into NamesTableViewController,​ and in viewDidLoad,​ add some name objects. I know, I know, we said we will get data from JSON. We will, let’s just do it in a simple way first. ​ I'll create a new variable called newName. It should look something like this, pretty intuitive. The first part of the application that needs some data loaded into it is going to be our Table Views. So go into NamesTableViewController,​ and in viewDidLoad,​ add some name objects. I know, I know, we said we will get data from JSON. We will, let’s just do it in a simple way first. ​ I'll create a new variable called newName. It should look something like this, pretty intuitive.
 +
 +
 +{{:​ios-labs-f15:​screen_shot_2015-10-21_at_6.08.58_pm.png |}}
  
  
 So we have the objects in the array, but now we need to load them into a table view. If you haven’t seen the tutorial on Tables, do that now or learn on the go. We need to edit the methods provided as part of the code that came with UITableViewController,​ primarily numberOfSectionsInTableView and numberOfRowsInSection. Make them look like this. So we have the objects in the array, but now we need to load them into a table view. If you haven’t seen the tutorial on Tables, do that now or learn on the go. We need to edit the methods provided as part of the code that came with UITableViewController,​ primarily numberOfSectionsInTableView and numberOfRowsInSection. Make them look like this.
 +
 +
 +{{:​ios-labs-f15:​screen_shot_2015-10-21_at_6.11.44_pm.png |}}
  
  
Line 451: Line 564:
    
 So it is saying that the cell in the storyboard should have that name. To make sure that that is the case, go to storyboard. Take a look at the table view controller. Select a cell and in the utilities panel, in the “Table view cell” section, give the identifier a name “nameCell” and make the change in the tableView function as well. So it is saying that the cell in the storyboard should have that name. To make sure that that is the case, go to storyboard. Take a look at the table view controller. Select a cell and in the utilities panel, in the “Table view cell” section, give the identifier a name “nameCell” and make the change in the tableView function as well.
 +
 +
 +{{:​ios-labs-f15:​screen_shot_2015-10-21_at_6.14.27_pm.png |}}
 +
  
 Build the project and hopefully everything should work. At this moment, we are not passing any data in between the scenes, so let’s do that. Build the project and hopefully everything should work. At this moment, we are not passing any data in between the scenes, so let’s do that.
  
-Passing Data+===== Passing Data ===== 
 I am not going to spend too much time on this, since we have covered this in detail in the last few labs. Let me just quickly go through the steps. I am not going to spend too much time on this, since we have covered this in detail in the last few labs. Let me just quickly go through the steps.
-Go to the NamesTableViewController.swift file and un comment the prepareForSegue function and make it look like the following+Go to the NamesTableViewController.swift file and un comment the prepareForSegue function and make it look like below. 
 + 
 +{{:​ios-labs-f15:​screen_shot_2015-10-21_at_6.18.16_pm.png |}}  
 + 
 +Alright, now run the app and hopefully it should work perfectly fine. Now, we will bring JSON and NSURLConnection in. But first, a short primer on both of them.
  
-Alright, now run the app and hopefully it should work perfectly fine. Now, we will bring JSON and NSURLConnection ​in. But first, a primer on both of them.+===== NSURLConnection ​=====
  
-NSURLConnection 
 NSURLConnection is a long and painful name for the object that handles iOS's internet connectivity. This class contains everything you need to access any remote API, and is commonly used to back model objects. Although not isolated because of MVC, it still relies on delegation to function. Remember that delegation is the act of nominating another object to react to certain changes in state through the implementation of delegate methods. The object that needs functionality calls the delegate methods on the assigned delegate object. NSURLConnection is a long and painful name for the object that handles iOS's internet connectivity. This class contains everything you need to access any remote API, and is commonly used to back model objects. Although not isolated because of MVC, it still relies on delegation to function. Remember that delegation is the act of nominating another object to react to certain changes in state through the implementation of delegate methods. The object that needs functionality calls the delegate methods on the assigned delegate object.
 +
 There are two parts to using NSURLConnection. There are two parts to using NSURLConnection.
-Create API call and receiving variable, fire API call +  - Create API call and receiving variable, fire API call 
-Implement delegate methods+  ​- ​Implement delegate methods 
 The delegate methods are called as NSURLConnection moves about its business. They are extremely boilerplate,​ and generally you'll find yourself copying and pasting them constantly instead of writing them from scratch. The delegate methods are called as NSURLConnection moves about its business. They are extremely boilerplate,​ and generally you'll find yourself copying and pasting them constantly instead of writing them from scratch.
 +
 There are 4 delegate methods: There are 4 delegate methods:
-didReceiveResponse- called when the first bits are received from a request +  - didReceiveResponse- called when the first bits are received from a request 
-didReceiveData- called as data flows in +  ​- ​didReceiveData- called as data flows in 
-connectionDidFinishLoading- called when the data has finished arriving +  ​- ​connectionDidFinishLoading- called when the data has finished arriving 
-connection:​didFailWithError- called if something goes back +  ​- ​connection:​didFailWithError- called if something goes back 
-Implement Delegate Methods + 
-Copy in the NSURLConnection Delegate methods: +==== Implement Delegate Methods ​==== 
-// MARK: NSURLConnection functions + 
-  func connection(connection:​ NSURLConnection,​ didReceiveResponse response: NSURLResponse) ​{ +{{ :ios-labs-f15:screen_shot_2015-10-21_at_6.29.38_pm.png |}}
-     +
-  } +
-   +
-  func connection(connection:​ NSURLConnection,​ didReceiveData data: NSData) ​{ +
-     +
-  } +
-   +
-  func connectionDidFinishLoading(connectionNSURLConnection) { +
-    let errorNSErrorPointer! = NSErrorPointer() +
-    let data: AnyObject? = NSJSONSerialization.JSONObjectWithData(self.data, options: nil, error: error)+
  
-    connection.cancel() 
-  } 
-  ​ 
-  func connection(connection:​ NSURLConnection,​ didFailWithError error: NSError) { 
-    println("​Error During Connection: \(error.description)"​) 
-  } 
 Note that the receiver object has been omitted in the code above (it should be right after the colon). This is the data object that the Connection class loads the data into and you have yet to make it. The circle in the screenshot above shows where it should be as a method parameter for the JSONDeserializer class. Note that the receiver object has been omitted in the code above (it should be right after the colon). This is the data object that the Connection class loads the data into and you have yet to make it. The circle in the screenshot above shows where it should be as a method parameter for the JSONDeserializer class.
 +
 A “receiver” object is simply an object that gets filled by another. In the case of NSURLConnection,​ we must use an NSMutableData object, which stores changeable binary data. A “receiver” object is simply an object that gets filled by another. In the case of NSURLConnection,​ we must use an NSMutableData object, which stores changeable binary data.
-You are asked to initialize this variable here. Initialization looks the same for every class, all of the time for the stock constructor,​ see previous lecture for hints! + 
-Making Receiver Object+**Making Receiver Object**
 Prepare to declare a new instance variable by first adding curly braces after the class ViewController:​ UIViewController,​ NSURLConnectionDataDelegate,​ NSURLConnectionDelegate { line in ViewController.swift Prepare to declare a new instance variable by first adding curly braces after the class ViewController:​ UIViewController,​ NSURLConnectionDataDelegate,​ NSURLConnectionDelegate { line in ViewController.swift
 Declare the variable between the braces. The declaration should look like this: var name: type! Declare the variable between the braces. The declaration should look like this: var name: type!
 +
 The variable should be of type NSMutableData. You may name it whatever you'd like The variable should be of type NSMutableData. You may name it whatever you'd like
 +
 Initialize the variable in viewDidLoad Initialize the variable in viewDidLoad
 Ok. We've implemented the methods and created the receiving variable. Two steps lie between us and talking to the internet: manipulating the data object previously created, and firing the API call. Again, the steps detailed below are almost always like this; I'm having you work through them manually not simply to get practice calling methods. The formation of the NSURL and NSURLRequest objects commonly look like this, treat them as one step and don't get boggled down trying to find meaning in the code. Ok. We've implemented the methods and created the receiving variable. Two steps lie between us and talking to the internet: manipulating the data object previously created, and firing the API call. Again, the steps detailed below are almost always like this; I'm having you work through them manually not simply to get practice calling methods. The formation of the NSURL and NSURLRequest objects commonly look like this, treat them as one step and don't get boggled down trying to find meaning in the code.
-Initiating API Call + 
-In didReceiveResponse,​ call the method .length on your data instance variable, passing 0 as an argument. This clears the data when a response comes through. +{{ :ios-labs-f15:screen_shot_2015-10-21_at_6.29.10_pm.png |}} 
-In didReceiveData,​ call appendData on your data instance variable, passing data to it (data is the argument for didReceiveData) + 
-Finally, put your data instance variable in the missing parameter slot in the connectionDidFinishLoading method, as pointed out in the screenshot above +We should be hooked up to the internet. As you can see from the post above, the URL we're headed to is http://​pages.cs.wisc.edu/​~achink/cs407.json . In order to check the results of your code, go there now and inspect the JSON that is returned. 
-Copy the following code into viewDidLoad + 
-   let urlNSURL = NSURL(string"​http://​pages.cs.wisc.edu/​~mihnea/​instagram.json"​)! +You might get an error which says that since the channel is insecure, ​JSON can't be retrieved. Search online for answers. HINT: Something has to be added to the info.plist file 
-    let req: NSURLRequest = NSURLRequest(URL:​ url) + 
-    let connection: NSURLConnection = NSURLConnection(request:​ req, delegate: self)! +===== JSON ===== 
-    connection.start() +
-We should be hooked up to the internet. As you can see from the post above, the URL we're headed to ishttp://​pages.cs.wisc.edu/​~mihnea/instagram.json . In order to check the results of your code, go there now and inspect the JSON that is returned. +
-JSON+
 If you are doing anything on the internet, JSONs are life. JSON stands for JavaScript Object Notation, and is the more-or-less standard language of moving raw data over the open internet. If you are doing anything on the internet, JSONs are life. JSON stands for JavaScript Object Notation, and is the more-or-less standard language of moving raw data over the open internet.
  
Line 531: Line 640:
  
 This dictionary has two keys that match two strings. This dictionary has two keys that match two strings.
- 
  
 JSON may look awful to you at first, but understanding them is absolutely essential to interacting with any server, ever.  JSON may look awful to you at first, but understanding them is absolutely essential to interacting with any server, ever. 
-  ​ 
- 
  
 +===== Conclusion =====
  
-Creating scenes and segues +Some of [[http://​www.raywenderlich.com/​ | Raywenderlich ]] tutorials are pretty good on the topic of JSON and Models. Hope you guys enjoyed today'​s session. The complete app can be found [[https://​github.com/​achinkulshrestha/​xApp]]
- +
-Adding class code +
- +
-Defining models +
- +
-Loading data +
- +
-Passing objects between scenes +
- +
-NSURLConnection +
- +
-JSON  +
- +
-Final note+
ios-labs-f15/start.1445471748.txt.gz · Last modified: 2015/10/21 18:55 by achin