- 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, anNSDictionarycalledlocationand anNSUIntegercalledlength; and returns anNSString. -
dictionaryForLocationWithName:latitude:longitude:that takes three arguments, anNSStringcalledname, and twoCGFloats calledlatitudeandlongitude; and returns anNSDictionary. -
namesOfLocations:that takes oneNSArrayargument calledlocationsand returns anNSArray. -
dictionaryIsValidLocation:that takes oneNSDictionaryargument and returns aBOOL. -
locationNamed:inLocations:that takes two arguments, anNSStringcallednameand anNSArraycalledlocations; 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 thelengthargument.
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 thenamekey in the location dictionaries in the submittedlocationsarray. -
dictionaryIsValidLocation:should returnYESonly if the submittedlocationdictionary 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 forlatitudefalls between -90.0 and 90.0, that the value forlongitudefalls between 180.0 and -180.0, and that the value fornameis not an empty string. However, there are no tests for these cases. -
locationNamed:inLocations:should return thelocationdictionary in the submittedlocationsarray with the matching value for thenamekey as the submittednamestring. If there are no matches, then it should returnnil.