- Apply what you've learned about collections to correctly store some real-world information.
- Verify that a dictionary contains the expected keys.
- Retrieve a stored dictionary by the value for a key.
We're going to work towards building an app to allow users to share trivia facts about locations around the city. As users walk around, they can be notified of nearby trivia items. Before we get to work with trivia, we need to handle the data about nearby locations.
In this exercise, we're going to start by handling some data about a few locations in New York City. We're going to save the location's name, latitude, and longitude as values in a dictionary. In Objective-C, a manual declaration of such a dictionary would look like this:
NSDictionary *flatironSchool = @{ @"name": @"The Flatiron School",
@"latitude": @34.432,
@"longitude": @-23.67 };
Navigate to the FISAppDelegate.h
header file and declare the following methods. Set them to return nil
in the FISAppDelegate.m
implementation file. Run the tests to see that most of them fail.
-
stringByTruncatingNameOfLocation:toLength:
that takes two arguments, anNSDictionary
calledlocation
and anNSUInteger
calledlength
; and returns anNSString
. -
dictionaryForLocationWithName:latitude:longitude:
that takes three arguments, anNSString
calledname
, and twoCGFloat
s calledlatitude
andlongitude
; and returns anNSDictionary
. -
namesOfLocations:
that takes oneNSArray
argument calledlocations
and returns anNSArray
. -
dictionaryIsValidLocation:
that takes oneNSDictionary
argument and returns aBOOL
. -
locationNamed:inLocations:
that takes two arguments, anNSString
calledname
and anNSArray
calledlocations
; and returns anNSDictionary
.
Now, write out the method bodies for each method one by one. Run the tests each time you finish a method to check your work. Reference each of the tests to know what they're expecting.
-
stringByTruncatingNameOfLocation:toLength:
should return a string containing the beginning of the submitted location's name with the number of letters specified in thelength
argument.
Hint: Look up thesubstringToIndex:
method onNSString
. -
dictionaryForLocationWithName:latitude:longitude:
should return a dictionary containing the three argument values stored to keys of the argument names (@"name"
,@"latitude"
,@"longitude"
). -
namesOfLocations:
should return an array containing all of the values for thename
key in the location dictionaries in the submittedlocations
array. -
dictionaryIsValidLocation:
should returnYES
only if the submittedlocation
dictionary has exactly three keys by the names of@"name"
,@"latitude"
, and@"longitude"
. If any of these conditions fail, the method should returnNO
.
Advanced: Write additional checks to determine that the value forlatitude
falls between -90.0 and 90.0, that the value forlongitude
falls between 180.0 and -180.0, and that the value forname
is not an empty string. However, there are no tests for these cases. -
locationNamed:inLocations:
should return thelocation
dictionary in the submittedlocations
array with the matching value for thename
key as the submittedname
string. If there are no matches, then it should returnnil
.