- Accomplish the same tasks using literals, instance methods, and class methods.
Fork and clone this lab.
Open the objc-classy-methods.xcodeproj
file. Navigate to FISAppDelegate.m
and enter the following code-alongs into the application:didFinishLaunchingWithOptions:
method.
- Create a new
NSString
variable calledkatherine
and use the string literal to set it to@"Katherine"
:
NSString *katherine = @"Katherine";
- Create a new
NSString
variable calledkatherineHepburn
and use it to capture the return of calling thestringByAppendingString:
method onkatherine
with@" Hepburn"
supplied as the argument string (don't forget the leading space):
NSString *katherineHepburn = [katherine stringByAppendingString:@" Hepburn"];
- Use
NSLog()
to printkatherineHepburn
to the console:
NSLog(@"%@", katherineHepburn);
This should print: Katherine Hepburn
.
- Create a new
NSString
variable calledjames
and use the string literal to set it to@"James"
:
NSString *james = @"James";
- Create a new
NSString
variable calledjamesStewart
and set use it to capture the return of calling thealloc
andinitWithFormat:
method pair onNSString
. Supply the argument with a format string with two object specifiers (%@
) separated by a single space, thejames
string as the first specified object, and@"Stewart"
as the second specified object:
NSString *jamesStewart = [[NSString alloc] initWithFormat:@"%@ %@", james, @"Stewart"];
- Use
NSLog()
to printjamesStewart
to the console:
NSLog(@"%@", jamesStewart);
This should print: James Stewart
.
- Create a new
NSString
variable calledqueen
and use the string literal to set it to@"Queen"
:
NSString *queen = @"Queen";
- Create a new
NSString
variable calledqueenElizabethII
and use it to capture the return of calling thestringWithFormat:
class method called onNSString
itself. Supply the argument with a format string with three object specifiers (%@
) separated by a single space, thequeen
string as the first specified object,@"Elizabeth"
as the second specified object, and the Roman numerals@"II"
as the third specified object:
NSString *queenElizabethII = [NSString stringWithFormat:@"%@ %@ %@", queen, @"Elizabeth", @"II"];
- Use
NSLog()
to printqueenElizabethII
to the console:
NSLog(@"%@", queenElizabethII);
This should print: Queen Elizabeth II
.
- Create a new
NSArray
variable calledclassyThings
and use the array literal to set it to an array containing the strings@"monocle"
,@"top hat"
, and@"martini glass"
:
NSArray *classyThings = @[ @"monocle", @"top hat", @"martini glass" ];
- Use
NSLog()
to printclassyThings
to the console:
NSLog(@"%@", classyThings);
This should print:
(
monocle,
"top hat",
"martini glass"
)
- Create a new
NSArray
variable calledclassyPeople
and use it to capture the return of calling thealloc
andinitWithObjects:
method pair onNSArray
. Supply the argument with a list of the three full-name string objects from Code-Along I, end the list withnil
:
NSArray *classyPeople = [[NSArray alloc] initWithObjects:@"Katherine Hepburn", @"James Stewart", @"Queen Elizabeth II", nil];
- Use
NSLog()
to printclassyPeople
to the console:
NSLog(@"%@", classyPeople);
This should print:
(
"Katherine Hepburn",
"James Stewart",
"Queen Elizabeth II"
)
- Create a new
NSArray
variable calledclassyDrinks
and use it to capture the return of calling thearrayWithObjects:
class method onNSArray
itself. Supply the argument with a list of strings;@"Old Fashioned"
,@"Churchill Martini"
,@"Prosecco"
; and end the list withnil
:
NSArray *classyDrinks = [NSArray arrayWithObjects:@"Old Fashioned", @"Churchill Martini", @"Prosecco", nil];
- Use
NSLog()
to printclassyDrinks
to the console:
NSLog(@"%@", classyDrinks);
This should print:
(
"Old Fashioned",
"Churchill Martini",
Prosecco
)
1 — Create a new NSDictionary
variable called classyByLiteral
and use the dictionary literal to set it to a dictionary containing three keys; @"classy things"
, @"classy people
", and @"classy drinks"
; which each point to an empty array literal:
NSDictionary *classyByLiteral = @{ @"classy things" : @[],
@"classy people" : @[],
@"classy drinks" : @[]
};
2 — Populate the value arrays with the following strings:
- for
@"classy things"
, populate its value array with@"monocle"
,@"top hat"
, and@"martini glass"
, - for
@"classy people"
, populate its value array with@"Katherine Hepburn"
,@"James Stewart"
, and@"Queen Elizabeth II"
, and - for
@"classy drinks"
, populate its value array with@"Old Fashioned"
,@"Churchill Martini"
, and@"Prosecco"
:
NSDictionary *classyByLiteral = @{ @"classy things" : @[ @"monocle" ,
@"top hat" ,
@"martini glass" ],
@"classy people" : @[ @"Katherine Hepburn" ,
@"James Stewart" ,
@"Queen Elizabeth II" ],
@"classy drinks" : @[ @"Old Fashioned" ,
@"Churchill Martini" ,
@"Prosecco" ]
};
3 — Use NSLog()
to print classyByLiteral
to the console:
NSLog(@"%@", classyByLiteral);
This should print, in some order:
{
"classy drinks" = (
"Old Fashioned",
"Churchill Martini",
Prosecco
);
"classy people" = (
"Katherine Hepburn",
"James Stewart",
"Queen Elizabeth II"
);
"classy things" = (
monocle,
"top hat",
"martini glass"
);
}
- Create a new
NSDictionary
variable calledclassyByInit
and use to capture the return of calling thealloc
andinitWithObjectsAndKeys:
method pair onNSDictionary
. Use the array variables from Code-Along II to supply the argument with a list of the three arrays each followed by the associated key string to create a dictionary that matches the one above:
NSDictionary *classyByInit = [[NSDictionary alloc] initWithObjectsAndKeys:classyThings, @"classy things", classyPeople, @"classy people", classyDrinks, @"classy drinks", nil];
Top-tip: Be careful to list the "object" before the "key". This is inverted from the order ofkey : object
when using the dictionary literal.
- Use
NSLog()
to printclassyByInit
to the console:
NSLog(@"%@", classyByInit);
This should print a dictionary matching the one from section A. Verify that the keys and objects are not inverted:
// correct key : value order
{
"classy drinks" = (
"Old Fashioned",
"Churchill Martini",
Prosecco
);
"classy people" = (
"Katherine Hepburn",
"James Stewart",
"Queen Elizabeth II"
);
"classy things" = (
monocle,
"top hat",
"martini glass"
);
}
// incorrect key : value order
{
(
monocle,
"top hat",
"martini glass"
) = "classy things";
(
"Katherine Hepburn",
"James Stewart",
"Queen Elizabeth II"
) = "classy people";
(
"Old Fashioned",
"Churchill Martini",
Prosecco
) = "classy drinks";
}
1 — Create a new NSDictionary
variable called classyByClass
and use it to capture the return of calling the dictionaryWithObjects:forKeys:
class method on NSDictionary
itself. Supply the first argument with an array literal containing the three arrays from Code-Along II, and supply the second argument with an array containing the key string literals that match the keys used in sections A and B. Make sure to keep them in the correct order!:
NSDictionary *classyByClass = [NSDictionary dictionaryWithObjects:@[classyThings, classyPeople, classyDrinks]
forKeys:@[@"classy things", @"classy people", @"classy drinks"]];
2 — Use NSLog()
to print classyByClass
to the console:
NSLog(@"%@", classyByClass);
This should print another dictionary that matches the dictionaries from sections A and B:
{
"classy drinks" = (
"Old Fashioned",
"Churchill Martini",
Prosecco
);
"classy people" = (
"Katherine Hepburn",
"James Stewart",
"Queen Elizabeth II"
);
"classy things" = (
monocle,
"top hat",
"martini glass"
);
}