User Tools

Site Tools


ios-labs-f15:start

General Information

These are the lecture slides from the first three weeks.

Week 1 Slides

Week 2 Slides

Week 3 Slides

Week 4 Slides

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 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

Intermediate

Advanced

Lab 1 - Sept 11 2015

  • Setup Xcode on your machine with the latest version of Swift.
  • Get familiar with icons, options available in Xcode.

Lab 2 - Sept 14 2015

  • Finish Introductory and Core Syntax lessons of “Swift Essential training” on Lynda.com. Lynda.com is free for UW Madison students.
  • Get familiar with Xcode playgrounds for running and testing swift code.

Lab 3 - Sept 25 2015

This lab will cover the basics of the iOS development process. By the end of the it we hope that you should have foundational skills to begin iOS development yourself. However, this is only a brief primer!

In this lab, there are 4 things we will mostly deal with: The programming environment Xcode HelloWorld

You may notice as you make progress through the tutorial that your IDE colors do not match those from the screenshots. That's my fau407/f15/lt: I can't stand a white background on my IDE's, so my Xcode color scheme is set to “Dusk.” If you'd like to change yours to match, instructions are included below.

Changing IDE Colors

Go to Xcode > Preferences > Colors Select “Dusk” or “Midnight”

Development Environment

Xcode is the IDE used for all iOS development. Although 3rd party IDE's exist, you should learn and appreciate Xcode, its fast, slick, and optimized for iOS. Because Apple has vertical control over their products (i.e. they have control over their products from hardware to operating system to application) Xcode can make a lot of menial tasks very easy and integrates well into the development cycle.

Creating a new Project

  1. Open the program to begin. We will start by creating a new project through File > New
  2. Enter a name for your new project. Make sure “iPhone” is selected under the “Devices” dropdown. Enter a name for the Company Identifier (contents don't matter right now)
  3. The next panel provides some preconfigurations for your new project. Don't worry about them for now– make sure the highlighted options are selected

407/f15/

Xcode Interface

On the top left you'll see a play button. This runs your application in the Simulator. Top right contains six buttons. The three on the left represent panes currently open in Xcode. The three buttons on the right allow you to change editor types. Go explore, if you haven't!

Click on the Main.Storyboard file in the project navigator in the left pane so your screen looks like the screenshot below. This is a storyboard, which lays out the interface of your application.

Run your application to make sure everything works.

InterfaceBuilder

InterfaceBuilder (commonly abbreviated as IB) is Xcode's integrated interface-building tool. It is a WYSIWYG (what-you-see-is-what-you-get) editor that allows you to drag and drop Views onto the screen easily. One of the nicest features of Xcode, InterfaceBuilder allows you to deal with the View creation process without ever having to write a single line of code. The file that InterfaceBuilder operates on is called the storyboard.

IB can look a little complicated, but there are only a two broad kinds of items that appear on the screen.

  1. View Controllers- every long rectangle on the screen represents a ViewController. These rectangles are the only things that can “float” in IB's main window
  2. and 3. Arrows- arrows in IB represent relationships between ViewControllers. The arrow on the left of the image above identifies the first ViewController to appear when the app launches. Arrows between ViewControllers are called segues and represent a transition between ViewControllers.

That's all fine and dandy, but you haven't actually learned what Views and Controllers actually are. One of the ways in which iOS is structured is called MVC, which stands for Model, View, Controller. This is just a way of thinking of different objects in code and keeping them separate.

With pure MVC is that every object in your program would fall into one of three roles. It's either a Model or a View or a Controller. Now this doesn't mean you only have three objects. You could have hundreds, but each one of them is either in the role of a Model or a View or a Controller. Now in practice, you don't always create perfectly clean, perfectly independent objects. In iOS a core building block is creating something called a ViewController class, which as it might sound, wants to claim elements of both view and controller, although as you'll see, there is still a separation.

The Controller side of this, your behavior, is in Swift code files, whereas your pure View objects, like buttons and labels, can be arranged in a storyboard or an XIB file, which are the files that will give us the visual interface builder. But what you have to define very well is the relationship between them. It's not automatic.

Views are interface items that interact directly with the user by displaying data or receiving button clicks. Controllers are each responsible for a different “scene” in the app, each which serves a particular function. A Settings scene , a Home scene , and a Contact scene each have different controllers. Controllers own views, and each gets their own setup of them in InterfaceBuilder. They also, obviously, control.

The following are examples of views in the pictures below

  • Button
  • Image
  • Table

The following are examples of view controllers in the pictures below

  • Settings
  • General
  • Autolock

Todays Hello, World! Problem

Its useful to do a quick Hello, World starter just to get your feet wet. We will code two variations to get an idea how InterfaceBuilder works.

Click on the ViewController.swift file on the project navigator. Type the following line before the closing brace in the viewDidLoad method.

println(“Hello, World!”) Run your program by clicking the play button in the top left corner. Check the console for your message!

Ok, that was pretty basic! println() is a library function call that logs the message within the quotes to the console. This call takes in a String object.

In this next step, we're going to make a Hello, World button and label on the main interface.

Adding Views to the Root View Controller

  1. Select the Main.storyboard file in the project navigator
  2. Ensure the Object Library is selected in the bottom right of the screen (circled)
  3. Select the ViewController on the Interface Builder.
  4. Scroll through the list of objects to find a Label, or type “label” in the search box on the bottom right corner
  5. Double-click on the view controller on the storyboard to ensure it is in focus. Drag the label onto the top of the view controller
  6. Repeat the last two steps with a Button, placing it below the label

This is what your view controller should look like after the last steps. We have two minor changes to make to the format and then you will hook up the views you just added to code.

Formatting Views

  • Change the title of the button to “Greetings! Welcome to CS 407.” To change the title of the button, double-click on it and type in the new title.
  • Change the width of the label to accommodate our greeting.

At this point you can run your app again and see the views laid out on the root view. Unfortunately, since the views are not connected to code, they don't do anything terribly interesting.

What does this mean? Remember, MVC splits objects into different types, keeping their code and interaction seperate. Each type of object's source code shouldn't ever mix with other types of object source code. Views don't merge into controllers and vis-versa. You have already seen Views (the label and the button) and Controllers (the rectangle in the middle of the screen, and the file you wrote “Hello, World!” in.) Controllers are the masters of the program, so to make Views do anything interesting we have to connect the two.

We connect Views to Controllers using a simple drag and drop. There are two kinds of connections you can make:

  • IBOutlet - gives the owning view controller a reference to the view. It can change the content, properties, and functionality. This is used for non-interactive views that need to be changed dynamically.
  • IBAction - establishes a callback from a view to a view controller. When some action happens, the view calls a method in the controller. This is used for responsive functionality.

IB stands for InterfaceBuilder. It is an established convention in iOS to prefix your objects with 2 letters that tell the programmer from what library they come from.

When the user presses the button, we want to change the text of the label to “Welcome to CS407!” In order to do this, we need access to the label through an IBOutlet, and we need to know when the button is pressed through an IBAction. In order to make connections, we have to use the Assistant Editor to see the storyboard and the code

Opening Assistant Editor

To open the assistant editor, press the second button from the left in the top right corner. The icon looks like a two intersecting circles.

Creating Connections

  • Create an outlet from the view to the controller by right clicking on the label (or pressing control), dragging to the right, and releasing inside of the braces. A small notice will appear when you are in the right section.
  • Name the label labelHello, make sure the connection values is set to “outlet” and click connect in the popup that appears
  • Create an action from the button to the controller.
  • Ensure the Connection field reads Action, not Outlet. Name the action buttonHello.

When the button is pressed, we want to change the text of the label. This is done by setting the button's text property. Your code should look like this

React to buttonHello Callback

Place the following statement in between the braces of the buttonHello(sender: UIButton) method labelHello.text = “Welcome to CS407!“

Run your program! If it works, go crazy.

Ok. That was a lot of unexplained steps in quick succession. If this seems painful, don't worry. Its really quite simple, and at this point you've performed all the steps you'll ever need to build almost any iOS interface! If you can't quite understand what just happened, don't worry. You can just repeat the previous steps over and over to build your interfaces, and eventually things will click. We will discuss a little more of the theory of InterfaceBuilder in later classes.

CHALLENGE 1

Add 3 views to your view controller

  • A text box
  • Label
  • Button

Create an app such that when the button is clicked the label changes to “Hi, <Text Box Content>”. Essentially we want a functionality, where we type something into the Text Field, then tap the Button and change the contents of the Label

Show this to the TAs.

Auto Layout

In Xcode “Why is the default-provided canvas square?” Well, as was told to us in the class, it wasn't a square from the beginning.

In previous versions of Xcode, this Default Storyboard Scene had the basic proportions of an iPhone in Portrait mode. Now We have things like the iPhone 6, the iPhone 6 Plus, the 5, the 4s. A Square is simply the most generic starting point. Therefore we now need to build interfaces which are responsive to a change in screen size and position. This is where auto layout comes in.

We discussed auto layout in detail in class. Before we dive into a challenge problem, here are some tricks to remember while working on auto layout.

1. Auto layout is all about constraints. There are two ways to edit/add constraints, one is by using the buttons present in the bottom right.

second, by pressing control and dragging the pointer towards the direction where you want to add constraints.

2. Use “Document outline” to understand the hierarchy.

3. If you see the color “orange” something is wrong with the layout. The issue navigator can give you more details. “Blue” is good.

CHALLENGE 2

  • Start a new single view application project.
  • Create a view which looks like the image shown below. It has the following view objects - Slider control, 4 buttons on each corner, text box, text view

You challenge is to add constraints to all the view objects, and make them work for different screen sizes and orientation.

Conclusion

If this seemed a little slow, don't worry. The fun is just starting. Covering iOS development is a little weird for most people at first, because they have to deal with a new language, IDE, and set of conventions like MVC. Hold on tight!

Lab Homework 1 posted on Piazza - 25 Sep 2015

Problem 1 (Quiz App) - Due 2nd October In this week's lecture, we learned about views and Auto layout. The challenge is about building a Quiz app which has a minimum of three view controllers. Two scenes containing questions and the final scene containing result.

We want you to create two question types:

1. Image based question - You give an image which contains the question and the user has to put the answer in a text box and submit. The user would then move to the next question. Note: The user moves to the next question even if the answer is wrong.

2. Text based question - Show the question’s text to the user. The user enters the answer in a text box and then submits.

You can add more question types if you like.

Once both (or more) the questions have been answered, show a summary which displays the number of correct answers.

We will be grading (loosely) on the following things: show us the app at the beginning of Fridays lab. 1. Interactions (Buttons, labels and text box) 2. Auto layout

Lab 5 - Oct 2 2015

Please follow instruction in the following pdf

lab.pdf

Lab 6 - Oct 5 2015 - HW 2 Posted

Homework 2 - Personalized Calendar App Hey Guys,

By this time, you have learnt quite a bit about view controllers, interactions and layout design.

For HW2, we want you to make a personalized calendar app. When Xuan, Mickey and I were thinking about this homework, our version of the calendar app had a simple layout, something similar to what we see on Google Calendar. Unlike the previous homework, this time we want you to create your own personalized design for the app.

The only requirement is that the App should support these three basic operations.

  1. Viewing events on a particular day.
  2. Adding new events.
  3. Deleting events.

You can add more functionalities to app if you want but the requirements given above must be implemented.

There are two steps to this homework: 1. Submit your detailed app designs by 12th October. Your “Detailed App designs” should include scene layout, specific view objects you intend to use and also different types of controllers that would be part of your app. One easy way would be to create all the detailed scenes on the storyboard in Xcode. Please submit your designs and talk about your thought process as a REPLY TO THIS POST (Follow up discussions). If you wish to add an image, please upload it on Box (Preferred) and provide a link to it

2. The final submission is due on oct 23rd. Please make sure to put the code on Github. The evaluation would be done in the lab itself.

Late Penalty 5 points would be deducted for each late day used. Late clock starts ticking after 3:45 PM on Oct 23rd.

Grading Criteria We are NOT going to grade on “How cool the app looks”. Primarily we are looking for the following things:

  1. Interaction and Functionality (45%) - Make sure the 3 operations mentioned above work perfectly.
  2. Layout (45%) - The app should use Auto layout.
  3. Creativity (10%)

Tutorial - A Primer on Segues

I have observed a lot of students still struggling with Segues in the labs, so here is a much more detailed explanation on Segues and how to use them. I have also added some links to relevant resources in the end.

Typically, you need just one storyboard per app. This storyboard comprises of two important ideas, scenes and segues. With a single view app, you begin with just one scene and viewcontroller and then create a new scene for each screen in your app.

Now, we need a way to transition between two scenes, how do we do that? That is where segues come into picture. You describe segues between the relevant scenes. Segues define nothing but how the visual transitions would look like on switching view controllers, like do you want them to move in from the right, do a flip, fly from the left etc.

Segues as what you have seen till now, like when you were developing the mighty Quiz app only works if a user interacts with the screen in some way. One can also trigger a segue programmatically, say in response to a gesture or a change in accelerometer.

Let's directly get into action. I am going to make a 3 scene app and use segues to show transition between them.

Initially in the story board, we will begin with one scene. This scene loads when the application begins. You add new scenes by finding the view controllers in your object library and dragging one onto the canvas. Zoom out, so that you can see the entire storyboard. Select a view controller in the object library and then drag it inside the storyboard, doesn't matter where you position these. I have positioned the three VC side by side.

When the transition happens I want to be able to see the difference between these 3 VC, so I am going to change the background color for two of them. To do that Zoom Back in and go to the attributes inspector tab. It looks like a tuxedo on the top right. Change the background to whatever you want.

Next, let's add segueing between them. The segue needs to be triggered using something, let's use a button to do that. Find the button in the object library, drag that into the first and second VC. Change the text to Next VC coz that's exactly what we intend to do. I haven't put any button on the third screen. I just want the transition from the first VC to the second on button click and then go from the second to the third.

How do we do this, to make segues we don't need to worry about IB action or anything like that. I simply take the first view controller, click on the button, press the control key and drag to the second view controller while holding the key down. Let go! You would see a pop up come up. With iOS 8, segues have become adaptive which means iOS is going to be intelligent in detecting device and screen size. Let's stick to the show segue for the time being. You would observer a little arrow show up in between the two VC. Do exactly the same for the 2nd and 3rd view controller.

Ok, now do Command R. Let's just run this. Click on the button, you would see them move. Voila! This is how segues work.

Here are the screenshots

Tutorial - UITableViewController

This will be a simple tutorial for UITableViewController and UITableViewCell

1.first drag one UITableViewController on to the storyboard, set it as initial view controller if u start with a template and don't what to do segue

2. Now lets understand what we have here, so the controller you have here is a UITableViewController, its similar to UIViewController you have been using before, but only with a pre connected UITableView which will fill the entire UIViewController.

You can defiantly achieve the same thing with UIViewController+ UITableView, the difference is:

UITableViewController : table will be full screen, but delegate are pre linked.

UIViewController+ UITableView : table can resize, has to manually link delegate and add delegate code

but remember they are different controllers so make sure when u create your swift choose the right one

3. When you double click the white area of the prototype cells, it will do the following, this is called a UITableViewCell, and it serves as template and can be reused.

Lets drag a label in here, remember you also have to do auto layout inside the cell

now lets check on the delegate, because we are using UITableViewController they are pre linked

There are two delegate u need to link if you are doing UIViewController+ UITableView

UITableViewDelegate UITableViewDataSource

4. create the swift files we need, make sure to choose the right subclass

UITableViewController

UITableViewCell

Link UITableViewController

Link UITableViewCell

One More Thing!!! Make sure to set a identifier for your UITableViewCell

5. Swift code for UITableViewCell

Create and IBOutlet for the label that we created, so that we can change it in the UITableViewController

6. Swift code for UITableViewController

numberOfSectionsInTableView: how many section you want in your tableview (Need at least 1) numberOfRowsInSection: how many rows we want (be sure to be correspond to ur data)

I created a var items = [“text 1”,”text 2“,”text 3“,”text 4“,”text 5“] so that I have some fake data

now uncomment the next function, and do this

make sure the identifier is what you have setup earlier.

7. Run it and…

DONE!!

Lab 7 - Models, JSON, Tables and more - 23rd October 2015

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. 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.

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.

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.

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).

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).

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.

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.

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. 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.

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.

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.

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.

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.

Go to file menu →New→File, and inside the iOS source section, pick the Cocoa Touch Class. 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 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.

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.

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.

Go to the NamesTableViewController.swift, and create an array of names.

This creates an empty array. We'll start adding to it in a minute.

The second scene (DisplayViewController) doesn’t need to display mulitple Names, but it does need property (description) for a single name object. So, we need to pass the current name instance whose information will be displayed.

So into the DisplayViewController, add a var currentName.

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.

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. The displayViewController class should look like this.

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.

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.

It just says that we are returning one section, row count and what to display in each cell.

In one of the methods, we have the important cellForRowAtIndexPath. It's currently commented out, so let me uncomment that with the starting and ending comments here. It's dequeueing a reusable cell with the identifier “reuseIdentifier”.

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.

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

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 below.

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.

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.

There are two parts to using NSURLConnection.

  1. Create API call and receiving variable, fire API call
  2. 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.

There are 4 delegate methods:

  1. didReceiveResponse- called when the first bits are received from a request
  2. didReceiveData- called as data flows in
  3. connectionDidFinishLoading- called when the data has finished arriving
  4. connection:didFailWithError- called if something goes back

Implement Delegate Methods

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.

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 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

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.

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.

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

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.

the left of the console shows a graphical hierarchy of objects.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.

Two things make up a JSON:

Arrays- a set of objects, denoted by square brackets [] Dictionaries- keys that pair with objects, denoted by brackets, with a colon separating the key/value pairs. ex: [key : value] Primitives- strings, integers, booleans, etc Sounds simple, except that the two data structures can be infinitely nested within each other, as you can see from the sample JSON linked above.

When NSURLConnection returns the data, we use NSJSONDeserializer to convert the binary data into a dictionary. To navigate the JSON and extract useful information, you move through constituent dictionaries and arrays by using objectForKey and objectAtIndex, respectively.

For example, to retrieve the dictionary under the key “names,” you would call data.objectForKey(“names”) which returns a dictionary that looks like: {“name”:“John”, “Description”:”HOHO”},

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.

Conclusion

Some of 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

ios-labs-f15/start.txt · Last modified: 2015/10/30 12:04 by mbarboi