CC's After Hours project on how to parse and display JSON data in Objective-C.
- Fork and Clone this repo.
- Open the
CDBCovid.xcodeproj
starter file. - Review the storyboard scene.
- Click on your (blue) project, open right panel, click on file inspector, and fill out the class prefix with 3 initials. (I use my initials - CDB)
- Review API results:
- For the world: https://covid19.mathdro.id/api/
- For a country: https://covid19.mathdro.id/api/countries/USA
- Create a new group called "Model". Then create a new cocoa touch class file, named
CDBCoronaData
and subclassed it fromNSObject
. - In the
CDBCoronaData.h
header file:- Create properties for confirmed, deaths, and recovered.
- Create methods.
- In the
CDBCoronaData.m
implementation file:- Copy/Paste your methods over from your header file.
- Write your boiler plate code for init and set your instance variables.
self = [super init]; // boiler plate code if (self) { // set instance variables _confirmed = confirmed; _deaths = deaths; _recovered = recovered; } return self;
- Set self with dictionary values.
self = [self initWithConfirmed:[confirmed intValue] deaths:[deaths intValue] recovered:[recovered intValue]];
- Create a new group called "Model Controller". Then create a new cocoa touch class file, named
CDBCoronaDataController
and subclassed it fromNSObject
. - In the
CDBCoronaDataController.h
header file:- Set your class forward declaration.
@class CDBCoronaData;
- Create a property for the model
- Create 3 methods:
getWorldData
,getCountryData
,initWithDictionary
- Set your class forward declaration.
- In the
CDBCoronaDataController.m
implementation file:- Import the model
CDBCoronaData.h
into the top of this file. - Copy/Paste methods over from your header file.
- In the
getWorldData
method:- Set up your baseURL, request, and data task. (Handle errors and data, create a model object/alloc the space/initalize it, and don't forget to resume!)
- In the
getCountryData
method:- Set up your baseURL, request, and data task. (Handle errors and data, create a model object/alloc the space/initalize it, and don't forget to resume!)
- In the
initWithDictionary
method:- Create a model object/alloc the space/initalize it, boiler plate code, set instance variable, and return self.
CDBCoronaData *coronaData = [[CDBCoronaData alloc] initWithDictionary: dictionary]; self = [super init]; // Boiler plate code if (self) { _coronaData = coronaData; // set instance variable } return self;
- Import the model
- Create a new group called "View Controller". Then create a new cocoa touch class file, named
CDBViewController
and subclassed it fromUIViewController
. - Set storyboard scene to this class.
- In the
CDBViewController.m
implementation file:- Import the model
CDBCoronaData.h
and the model controllerCDBCoronaDataController.h
into the top of this file. - Create a property for the model controller.
@property (nonatomic) CDBCoronaDataController *coronaDataController;
- Create outlets.
- Create action for search button.
- Create method
getWorldData
in interface.- (void)getWorldData;
- In
viewDidLoad
:- Allocate the space and initalize the
coronaDataController
- Call the
getWorldData
method
- Allocate the space and initalize the
- In
getWorldData
:- Call this function from the imported
CDBCoronaDataController
.
- Call this function from the imported
- In
searchButtonTapped
action:- When a user clicks the search button, dismiss the keyboard.
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
- If text on the searchTextField == "World", then call this class' function getWorldData
if ([self.searchTextField.text isEqualToString:@"World"]) { [self getWorldData];
- Otherwise set the country name label to the search term text, call the function getCountryData, and set the labels to the country's data for countryName/confirmed/deaths/recovered.
} else { self.countryNameLabel.text = self.searchTextField.text; [self.coronaDataController getCountryData:self.searchTextField.text completion:^(CDBCoronaData *coronaData) { dispatch_async(dispatch_get_main_queue(), ^{ self.countryNameLabel.text = self.searchTextField.text; self.confirmedLabel.text = [NSString stringWithFormat:@"%d", coronaData.confirmed]; self.deathsLabel.text = [NSString stringWithFormat:@"%d", coronaData.deaths]; self.recoveredLabel.text = [NSString stringWithFormat:@"%d", coronaData.recovered]; }); }]; }
- When a user clicks the search button, dismiss the keyboard.
- Import the model
- Hook up the outlets in the storyboard to the view controller file.
- Build and run the project.
- You can test different countries like "USA", "Italy", or "China", and see the country's confirmed, death, and recovered cases.
Please visit this repo.