- Create variables.
- Use
NSLog()
to print a formatted string to the console. - Change the case of a string with the
uppercaseString
method. - Copy a string with the
stringWithString:
method. - Concatenate strings with the
stringByAppendingString:
method. - Concatenate an interpolated string with the
stringByAppendingFormat:
method. - Create a new interpolated string with the
stringWithFormat:
method.
This code-along lab will walk you through creating, manipulating, and concatenating (combining) strings while printing them with NSLog()
.
Open the objc-parrot.xcodeproj
file ($ open objc-parrot.xcodeproj
) and navigate to the FISAppDelegate.m
implementation file. You'll enter all of your code in the application:didFinishLaunchingWithOptions:
method body before the return YES
line.
Top-tip: If you wish to play around with Objective-C code snippets, this location in a blank program (or one of these early labs that we're supplying to you) is the best option for a "playground". Objective-C is not accurately supported by any REPL or by playgrounds like many other programming languages including Swift. It takes a full Xcode project to run code snippets.
- Print the string
@"Squawk!"
to the console by placing it directly inside anNSLog()
:
NSLog(@"Squawk!");
- Run the program with
⌘``R
, this will printSquawk!
.
- Print the string
@"Squawk!"
by submitting it to anNSLog()
as an argument for a format string with one specifier:
NSLog(@"%@", @"Squawk!");
- Run the program with
⌘``R
, this will print anotherSquawk!
.
- Copy the line above, but move the exclamation point (
!
) from the argument string into the format string:
NSLog(@"%@!", @"Squawk");
- Run the program with
⌘``R
, this will print a thirdSquawk!
.
- Copy the line above, but add a call of the
uppercaseString
method on the argument string@"Squawk"
so the returned (uppercased) string is interpolated into theNSLog()
's format string:
NSLog(@"%@!", [@"Squawk" uppercaseString]);
- Run the program with
⌘``R
, this will printSQUAWK!
.
- Create an
NSString
variable calledsquawk
and set it equal to a string literal@"squawk"
in all lowercase, then submit it to anNSLog()
as format argument:
NSString *squawk = @"squawk";
NSLog(@"%@", squawk);
- Run the program with
⌘``R
, this will printsquawk
.
- Now, set the
squawk
string variable equal to a call of theuppercaseString
method on itself, and print it again:
squawk = [squawk uppercaseString];
NSLog(@"%@", squawk);
- Run the program with
⌘``R
, this will printSQUAWK
.
- Now, add an
!
tosquawk
by setting it equal to a call ofstringByAppendingString:
on itself, the print it again:
squawk = [squawk stringByAppendingString:@"!"];
NSLog(@"%@", squawk);
- Run the program with
⌘``R
, this will printSQUAWK!
.
At the end of this section your console should print out:
Squawk!
Squawk!
Squawk!
SQUAWK!
squawk
SQUAWK
SQUAWK!
- Create four
NSString
variables;wind
,inString
,yer
, andsails
; and set them equal to, respectively,@"Wind"
,@"in"
,@"yer"
, and@"sails"
:
NSString *wind = @"Wind";
NSString *inString = @"in";
NSString *yer = @"yer";
NSString *sails = @"sails";
- Print the four strings using a format string with four specifiers separated by a space, and ending with an exclamation point (
!
):
NSLog(@"%@ %@ %@ %@!", wind, inString, yer, sails);
- Run the program with
⌘``R
, this will printWind in yer sails!
.
- Create a new
NSString
variable calledwindInYerSails
and use it to capture the return of callingstringWithFormat:
onNSString
. Submit to the method call a format string and format arguments identical to theNSLog()
above:
NSString *windInYerSails = [NSString stringWithFormat:@"%@ %@ %@ %@!", wind, inString, yer, sails];
- Print
windInYerSails
to the console:
NSLog(@"%@", windInYerSails);
- Run the program with
⌘``R
, this will print anotherWind in yer sails!
.
- Print the
wind
string by itself:
NSLog(@"%@", wind);
- Run the program with
⌘``R
, this will printWind
.
- Reuse
windInYerSails
to capture a call of thestringWithString:
method onNSString
withwind
supplied as the argument string, the printwindInYerSails
:
windInYerSails = [NSString stringWithString:wind];
NSLog(@"%@", windInYerSails);
- Run the program with
⌘``R
, this will print anotherWind
.
- Call the
stringByAppendingString:
method onwindInYerSails
with a string literal containing a space (@" "
) as the argument:
windInYerSails = [windInYerSails stringByAppendingString:@" "];
NSLog(@"%@", windInYerSails);
- Call the
stringByAppendingString:
method onwindInYerSails
a second time but withinString
submitted as the argument, then printwindInYerSails
:
windInYerSails = [windInYerSails stringByAppendingString:inString];
NSLog(@"%@", windInYerSails);
- Run the program with
⌘``R
, this will printWind in
.
- Call the
stringByAppendingFormat:
method onwindInYerSails
with a format string containing a space and one format specifier and theyer
string as the format argument, then printwindInYerSails
:
windInYerSails = [windInYerSails stringByAppendingFormat:@" %@", yer];
NSLog(@"%@", windInYerSails);
- Run the program with
⌘``R
, this will printWind in yer
.
- Call the
stringByAppendingFormat:
method onwindInYerSails
again but now with a format string containing a space, one format specifier, and an exclamation point (!
) and thesails
string as the format argument, then printwindInYerSails
again:
windInYerSails = [windInYerSails stringByAppendingFormat:@" %@!", sails];
NSLog(@"%@", windInYerSails);
- Run the program with
⌘``R
, this will printWind in yer sails!
.
At the end of this section your console should also print:
Wind in yer sails!
Wind in yer sails!
Wind
Wind
Wind in
Wind in yer
Wind in yer sails!
Mostly we figure that means "yes".
Code-Along III: Iago Is Molting
- Create five new
NSString
variables;look
,at
,me
,im
, andmolting
; and set them equal to, respectively;@"look"
,@"at"
,@"me"
,@"i'm"
, and@"molting"
.
NSString *look = @"look";
NSString *at = @"at";
NSString *me = @"me";
NSString *im = @"i'm";
NSString *molting = @"molting";
- Now print the five strings using an
NSLog()
with no characters other than the five required format specifiers:
NSLog(@"%@%@%@%@%@", look, at, me, im, molting);
- Run the program with
⌘``R
, this will printlookatmei'mmolting
.
- Now print the five strings again but insert a space between each specifier:
NSLog(@"%@ %@ %@ %@ %@", look, at, me, im, molting);
- Run the program with
⌘``R
, this will printlook at me i'm molting
.
- Now print the five strings again but with a comma (
,
) after the third format specifier and an exclamation point (!
) after the fifth format specifier:
NSLog(@"%@ %@ %@, %@ %@!", look, at, me, im, molting);
- Run the program with
⌘``R
, this will printlook at me, i'm molting!
.
- Lastly, print the five strings again, but call the
uppercaseString
method on each of them within theNSLog()
itself:NSLog(@"%@ %@ %@, %@ %@!", [look uppercaseString], [at uppercaseString], [me uppercaseString], [im uppercaseString], [molting uppercaseString]);
- Run the program with
⌘``R
, this will printLOOK AT ME, I'M MOLTING!
.
- Create a new
NSString
variable calledlookAt
and use it to capture the return of calling thestringByAppendingFormat:
method on thelook
string with a format string that has one space and one format specifier and theat
string as the format argument, then printlookAt
:
NSString *lookAt = [look stringByAppendingFormat:@" %@", at];
NSLog(@"%@", lookAt);
- Run the program with
⌘``R
, this will printlook at
.
- Create a new
NSString
variable calledlookAtMe
and use to capture the return of calling thestringByAppendingFormat:
method on thelookAt
string with a format string that has one space and one format specifier and theme
string as the format argument, then printlookAtMe
:
NSString *lookAtMe = [lookAt stringByAppendingFormat:@" %@", me];
NSLog(@"%@", lookAtMe);
- Run the program with
⌘``R
, this will printlook at me
.
- Use
lookAtMe
to capture the return of callinguppercaseString
onlookAtMe
(itself), then printlookAtMe
:
lookAtMe = [lookAtMe uppercaseString];
NSLog(@"%@", lookAtMe);
- Run the program with
⌘``R
, this will printLOOK AT ME
.
- Create a new
NSString
variable calledimMolting
and set it a call onNSString
of thestringWithFormat:
method supplied with a format string with two format specifiers separated by a space and theim
string andmolting
strings as the format arguments, then printimMolting
:
NSString *imMolting = [NSString stringWithFormat:@"%@ %@", im, molting];
NSLog(@"%@", imMolting);
- Run the program with
⌘``R
, this will printi'm molting
.
- Use
imMolting
to capture the return of callinguppercaseString
onimMolting
(itself), then printimMolting
:
imMolting = [imMolting uppercaseString];
NSLog(@"%@", imMolting);
- Run the program with
⌘``R
, this will printI'M MOLTING
.
- Create a new
NSString
variable calledlookAtMeImMolting
and use to capture the return of callingstringByAppendingFormat:
onlookAtMe
. Supply the format argument with a format string containing a comma (,
), a space, one format specifier, and an exclamation point (!
) andimMolting
as the format argument, the printlookAtMeImMolting
:
NSString *lookAtMeImMolting = [lookAtMe stringByAppendingFormat:@", %@!", imMolting];
NSLog(@"%@", lookAtMeImMolting);
- Run the program with
⌘``R
, this will printLOOK AT ME, I'M MOLTING!
.
- Create a new
NSString
variable callediagoShout
and set it equal to a call onNSString
of thestringWithFormat:
method with a format string with five format specifiers matching the lastNSLog()
from part A, and the five word strings as the format arguments:
NSString *iagoShout = [NSString stringWithFormat:@"%@ %@ %@, %@ %@!", look, at, me, im, molting];
NSLog(@"%@", iagoShout);
- Run the program with
⌘``R
, this will printlook at me, i'm molting!
.
- Now, print a call of
uppercaseString
oniagoShout
:
NSLog(@"%@", [iagoShout uppercaseString]);
- Run the program with
⌘``R
, this will printLOOK AT ME, I'M MOLTING!
.
- Use
iagoShout
to capture the return of callinguppercaseString
oniagoShout
(itself), then printiagoShout
:
iagoShout = [iagoShout uppercaseString];
NSLog(@"%@", iagoShout);
- Run the program with
⌘``R
, this will print anotherLOOK AT ME, I'M MOLTING!
.
At the end of this code-along, your console should print out:
lookatmei'mmolting
look at me i'm molting
look at me, i'm molting!
LOOK AT ME, I'M MOLTING!
look at
look at me
LOOK AT ME
i'm molting
I'M MOLTING
LOOK AT ME, I'M MOLTING!
look at me, i'm molting!
LOOK AT ME, I'M MOLTING!
LOOK AT ME, I'M MOLTING!
View Code-Along: Parrot on Learn.co and start learning to code for free.
View Code-Along: Parrot on Learn.co and start learning to code for free.
View Code-Along: Parrot on Learn.co and start learning to code for free.