- Create custom class files.
- Create, access, and set properties.
- Compose initializers.
- Write methods that interact with data stored in the instances of a custom class.
In the last Location Trivia lab you dealt with a representation of locations using NSDictionary
. Now, increase the power of your programming by storing this information in custom classes instead!
- Open the
locationTrivia-objects.xcworkspace
file.
Create the class files for FISLocation
that inherits from NSObject
. This class should have four properties:
- an
NSString
calledname
, - a
CGFloat
calledlatitude
, - a
CGFloat
calledlongitude
, and - an
NSMutableArray
calledtrivia
.
Create the class files for FISTrivium
that inherits from NSObject
. This class should have two properties:
- an
NSString
calledcontent
, and - an
NSUInteger
calledlikes
.
On the FISAppDelegate
class add a public property:
- an
NSMutableArray
calledlocations
.
On the FISAppDelegate
class, declare three methods:
allLocationNames
that takes no arguments and returns anNSArray
,locationNamed:
that takes oneNSString
argument calledname
and returns aFISLocation
object, andlocationsNearLatitude:longitude:margin:
that takes threeCGFloat
arguments calledlatitude
,longitude
, andmargin
, and returns anNSArray
.
Define these methods to return nil
.
On the FISLocation
class, declare five methods:
init
— the default initializer that will be overridden,initWithName:latitude:longitude:
— a designated initializer,stringByTruncatingNameToLength:
which takes oneNSUInteger
argument calledlength
and returns anNSString
,hasValidData
which takes no arguments and returns aBOOL
, andtriviumWithMostLikes
which takes no arguments and returns aFISTrivium
object.
Define the initializers to assign to self
a call of [super init]
and return self
.
Define the other three methods to return nil
.
On the FISTrivium
class, declare two initializers:
init
— the default initializer that will be overridden, andinitWithContent:likes:
— a designated initializer.
Define the initializers to assign to self
a call of [super init]
and return self
.
Run the tests with ⌘
U
to see that they fail.
-
Define
FISTrivium
's designated initializer. -
Override
FISTrivium
's default initializer to call the designated initializer. Default instances ofFISTrivium
should have an empty string for theircontent
and nolikes
.
Run the test in FISTriviumSpec.m
to check the FISTrivium
class.
-
Define
FISLocation
's designated initializer.
Hint: Don't forget to set thetrivia
property to the initialization of an empty mutable array. -
Override
FISLocation
's default initializer to call the designated initializer. Default instances ofFISLocation
should have an empty string for theirname
property Have it pass in an empty string forname
, and coordinates pointing to the intersection of the equator and the prime meridian (i.e. Latitude 0º, Longitude 0º).
-
Define the
stringByTruncatingNameToLength:
method to return a substring of the location's name shortened to the number of characters defined by thelength
argument. This method should handle alength
argument integer that exceeds the length of the location'sname
string; in such a case, it should just return the location's whole name.
Hint:NSString
has a handy method calledsubstringToIndex
. -
Define the
hasValidData
method to:
- return
NO
if thename
property is an empty string ornil
, - return
NO
if thelatitude
property is beyond-90.0
and90.0
, - return
NO
if thelongitude
property is beyond-180.0
and180.0
or if it is equal to-180.0
(since this longitude conceptually matches the 180º meridian), and - otherwise return
YES
.
- Define the
triviumWithMostLikes
method to:
- return
nil
if thetrivia
array is empty, otherwise - return the
FISTrivium
object in thetrivia
array that has the highest value for thelikes
property.
Hint: You can accomplish this with either afor in
loop & anif
statement, or by usingNSSortDescriptor
.
-
Define the
allLocationNames
method to return an array of all of thename
string properties of theFISLocation
objects held in thelocations
array property. -
Define the
locationNamed:
method to return aFISLocation
object whosename
string property matches the string submitted to thename
argument. If there isn't a match, this method should returnnil
.
Hint: You can accomplish with either afor in
loop &if
statement, or by usingNSPredicate
. -
Define the
locationsNearLatitude:longitude:margin:
method to return an array of all the locations whose coordinates are within ±margin
degrees from the given location.
Note: This is intended to be a simple implementation of local cartography. The test data only contains locations on Manhattan. You do NOT need to spend time programming for edge cases of detecting locations across the poles or across the 180º meridian. For actual mapping implementations, the Apple frameworkMapKit
would be used.
View Location Trivia Objects on Learn.co and start learning to code for free.