tags | language |
---|---|
OOP, properties, methods |
objc |
- Learn how Objects work
- Get better with Objective-C
- Create a new Person Class. Person should be an NSObject subclass.
- Add all of the required properties to the Person class
- Implement all of the required methods in the Person class
Properties provide a way to create instance variables and accessor methods (getters and setters) in a single step. Say for instance we created a class called cats. If Cats has a property (of type NSNumber) numberOfLegs, the cat class will automatically, behind the scenes generate two methods for us:
- (NSNumber *)numberOfLegs; // getter
- (void)setNumberOfLegs:(NSNumber *)numberOfLegs; // setter
We can use these methods to both set and get our class's properties. Let's say we create a Cat 'myCat' in our AppDelegates' didFinishLaunchingWithOptions method. We can set the cats number of legs and get the cats number of legs using the following code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Cat *myCat = [[Cat alloc] init];
[myCat setNumberOfLegs:@4]; //sets number of legs
NSNumber *numLegs = [myCat numberOfLegs]; // gets myCat's numberOfLegs and assigns it to a local variable 'numLegs'
}
But it gets better! Objective-C allows us to use dot notation to both get and set properties. I could (and in fact should), write the above code like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Cat *myCat = [[Cat alloc] init];
myCat.numberOfLegs = @4; //sets number of legs
NSNumber *numLegs = myCat.numberOfLegs; // gets myCat's numberOfLegs and assigns it to a local variable 'numLegs'
}
Last, remember that the classes you make are just like any other class such as NSString
or NSNumber
. You can add them to arrays like this:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
Cat *myCat = [[Cat alloc] init];
[myArray addObject:myCat];
Then we can get the cats the same way we are used to with arrays
Cat *retreivedCat = myArray[0];
Create the following properties in Person.h.
(NSNumber *)height
(NSNumber *)age
(NSString *)name
(BOOL) isFemale
(NSMutableArray *)friends
ex.
@interface Person : NSObject
@property (strong, nonatomic) NSNumber *height;
@property (nonatomic) BOOL isFemale; // notice how primitives are declared a little differently than objects
@end
- (NSNumber *)grow;
* Based on age/gender, grow a random amount of inches. * If it's a girl with age < 11 grow between 0 and 1 inch, age >= 11 and <=15 grow .5 to 2 inches. >15 grow 0 inches * If it's a boy with age < 12 grow between 0 and 1 inch, age >=12 and <=16 grow .5-3.5 inches. >16 grow 0 inches- (void)addFriends:(NSArray *)friends;
* Add the elements of the passed inNSArray
to ourfriends
property.- (NSString *)generatePartyList;
* Generate a list of friends for an upcoming party. It should look like this:@"eric, chris, al, avi, adam"
- (BOOL)removeFriend: (Person *)friend;
* Remove that friend from the array. * ReturnYES
orNO
if they were found or not found
- (NSArray *)removeFriends: (NSArray *)friends;
* Remove the friends listed in the passed in argument from our currentfriends
property. * Return an array of friends who were found and removed
Feel free to play with your new class outside of the tests in the AppDelegate. Here is a reminder how.
- Import the Person class in the AppDelegate's .m
- In didFinishLaunchingWithOptions, instantiate a new Person. ie.
Person *al = [[Person alloc] init];
- Set the person's age, height, name, and isFemale properties using dot notation. ie.
al.height = @72;
- Test each of the required methods and NSLog the output. ie.
NSLog(@"%@",[al grow]) // ex output 76;
- Create a few additional Person objects to test addFriends ie. ```[al addFriends:@[jon, chris, sally]]; //assuming i already created jon, chris and sally.
Here is a helper method for random floats
#define ARC4RANDOM_MAX 0x100000000
- (CGFloat)randomFloatBetweenNumber:(CGFloat)minRange andNumber:(CGFloat)maxRange
{
return ((float)arc4random() / ARC4RANDOM_MAX)
* (maxRange - minRange)
+ minRange;
}
Check out the documentation on NSMutableArray (Help -> Documentation and API Refrences). NSMutableArray has some great methods ie. addObject, addObjectsFromArray, removeObject, removeObjectsFromArray