Video Tutorial: Objective-C Data Types: Bool

Learn how to use true/false boolean types in Objective-C, including some common gotchas to keep in mind. By Ray Wenderlich.

Leave a rating/review
Save for later
Share

Contents

Hide contents

Video Tutorial: Objective-C Data Types: Bool

1 min

Challenge

Your challenge is to add this code to your project:

// Add this function to the top of AppDelegate.m
static BOOL different (int thing1, int thing2) {
    return thing1 - thing2;
}

// Add this method inside application:didFinishLaunchingWithOptions
if (different(1, 2) == YES) {
    NSLog(@"Different!");
} else {
    NSLog(@"Not different.");
}

Run your code on both 32-bit iOS and 64-bit iOS. It should correctly display “Different” on one, but not the other. Why?

Then replace your if statement with the following:

if (different(1, 2)) {
    NSLog(@"Different!");
} else {
    NSLog(@"Not different.");
}

Run this on both 32-bit and 64-bit iOS. Why is this version better practice than the previous?

Download solution

Helpful links

Errata

  • I misspoke around the 18 second mark – I meant to say “lowercase bool” but said “lowercase boolean”.

Contributors

Over 300 content creators. Join our team.