- Create variables.
- Call methods to manipulate strings.
- Concatenate strings with methods on
NSString
andNSMutableString
. - Print to the console with
NSLog()
supplied with interpolated strings.
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 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!
.
- 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:
squawk = [squawk capitalizedString];
- Then add an
!
tosquawk
by setting it equal to a call ofstringByAppendingString
on itself:
squawk = [squawk stringByAppendingString:@"!"];
- Now, print
squawk
to the console usingNSLog()
:
NSLog(@"%@", squawk);
- Run the program with
⌘``R
, this will printSquawk!
.
At the end of this code-along your console should print out:
Squawk!
squawk
Squawk!
- Create three
NSString
variables;deadMen
,tell
, andnoTales
; and set them equal to, respectively,@"Dead men"
,@"tell"
, and@"no tales"
:
NSString *deadMen = @"Dead men";
NSString *tell = @"tell";
NSString *noTales = @"no tales";
- Use the formatting ability of
NSLog()
to print these three string concatenated into a regular sentence, with a space between each format specifier and an exclamation point (!
) at the end:
NSLog(@"%@ %@ %@!", deadMen, tell, noTales);
- Run the program with
⌘``R
, this will printDead men tell no tales!
.
- Create a new
NSMutableString
variable calledpirateParrot
and set it equal to an+alloc
-init
of a newNSMutableString
:
NSMutableString *pirateParrot = [[NSMutableString alloc] init];
- Now, append
deadMen
topirateParrot
by calling theappendString:
method onpirateParrot
withdeadMen
supplied as the argument:
[pirateParrot appendString:deadMen];
- Run the program with
⌘``R
, this will printDead men
.
- Then, append
tell
andnoTales
topirateParrot
by calling theappendFormat:
method onpirateParrot
and supplying a formatted string that includes a space beforetell
, a space betweentell
andnoTales
, and an exclamation point (!
) at the end:
[pirateParrot appendFormat:@" %@ %@!", tell, noTales];
- Run the program with
⌘``R
, this will printDead men tell no tales!
.
At the end of this code-along, your console should print out:
Squawk!
squawk
Squawk!
Dead men tell no tales!
Dead men tell no tales!
Code-Along III: Iago Is Molting
- Create two new
NSString
variables callediagoLook
andiagoMolting
and respectively set them equal to the lowercase strings@"look at me"
and@"i'm molting"
:
NSString *iagoLook = @"look at me";
NSString *iagoMolting = @"i'm molting";
- Print a concatenation of the strings using an
NSLog()
:
NSLog(@"%@ %@", iagoLook, iagoMolting);
- Run the program with
⌘``R
, this will printlook at me i'm molting
.
- Make the strings uppercase by setting the strings equal to a call of the
uppercaseString
method to themselves, thenNSLog()
the strings again like above:
iagoLook = [iagoLook uppercaseString];
iagoMolting = [iagoMolting uppercaseString];
NSLog(@"%@ %@", iagoLook, iagoMolting);
- Run the program with
⌘``R
, this will printLOOK AT ME I'M MOLTING
.
- Create a new
NSString
variable callediagoShout
and set it equal a call ofNSString
'sstringWithFormat:
method supplied with a format string with two object format specifiers separated by a space and ending with an!
("exclamation point"), and the argumentsiagoLook
andiagoMolting
, then printiagoShout
using anNSLog()
:
NSString *iagoShout = [NSString stringWithFormat:@"%@ %@!", iagoLook, iagoMolting];
NSLog(@"%@", iagoShout);
- Run the program with
⌘``R
, this will printLOOK AT ME I'M MOLTING!
.
At the end of this code-along, your console should print out:
Squawk!
squawk
Squawk!
Dead men tell no tales!
Dead men tell no tales!
look at me i'm molting
LOOK AT ME I'M MOLTING!
LOOK AT ME I'M MOLTING!