OBJECTIVE-C 基礎教學3/5 codeschool




OBJECTIVE-C 基礎教學3/5 codeschool

//  if/else判斷句

bool mrHiggieIsMean = yes;

if (mrHiggieIsMean) {
  NSLog(@"Confirmed: he is super mean");
}


----------


BOOL mrHiggieIsMean = [mrHiggie areYouMean];

if (mrHiggieIsMean) {
  NSLog(@"Confirmed: he is super mean");
}  else {
  NSLog(@"No, actually he's really nice");
}


----------


| Meanness Range |               Log String                |

| 0-3            | Mr. Higgie is on the nice side          
| 4-7            | Mr. Higgie is sorta nice but not really 
| 8-10           | Mr. Higgie is definitely mean           

NSNumber *meannessScale = [mrHiggie meannessScale];

if([meannessScale intValue] < 4) {
  NSLog(@"Mr. Higgie is on the nice side");
} else if([meannessScale intValue] > 4 && [meannessScale intValue] <8) {
  NSLog(@"Mr. Higgie is sorta nice but not really");
} else {
  NSLog(@"Mr. Higgie is definitely mean");
}


----------

//再增加一個else if

NSString *hat = [mrHiggie currentHat];

if([hat isEqualToString:@"Sombrero"]) {
  NSLog(@"Ese es un muy buen sombrero");
} else if ([hat isEqualToString:@"Fedora"]) {
  NSLog(@"Mr. Higgie was an iPhone before there was iPhone");
} else if ([hat isEqualToString:@"AstronautHelmet"]) {
  NSLog(@"Mr. Higgie was an iPhone before there was iPhone");
} else {
  NSLog(@"Mr. Higgie is currently hatless");
}


----------

// 練習Switch
NSInteger day = getDayOfWeek();

switch (day) {
  case 1:
  case 2:
  case 3:
  case 4: {
    [mrHiggie setCurrentHat:@"Fedora"];
    break;
  }
  case 5: {
    [mrHiggie setCurrentHat:@"Sombrero"];
    break;
  }
  case 6:
  case 7: {
    [mrHiggie setCurrentHat:@"AstronautHelmet"];[mrHiggie setCurrentHat:@"Sombrero"];
    break;
  }
}

NSLog(@"Mr. Higgie is wearing: %@", [mrHiggie currentHat]);


----------

//case練習

DayOfWeek day = getDayOfWeek();

switch (day) {
    case DayOfWeekMonday:
    case DayOfWeekTuesday:
    case DayOfWeekWednesday:
    case DayOfWeekThursday: {
        [mrHiggie setCurrentHat:@"Fedora"];
        break;
    }
    case DayOfWeekFriday: {
        [mrHiggie setCurrentHat:@"Sombrero"];
        break;
    }
    case DayOfWeekSaturday:{
          [mrHiggie setCurrentHat:@"enum"];
    }
    case DayOfWeekSunday: {
        [mrHiggie setCurrentHat:@"AstronautHelmet"];
        break;
    }
}

NSLog(@"Mr. Higgie is wearing: %@", [mrHiggie currentHat]);


----------
NSArray *newHats = @[@"Cowboy", @"Conductor", @"Baseball"];

for (NSString *hat in newHats) {

  NSLog(@"Trying on new %@ hat", hat);

  if([mrHiggie tryOnHat:hat]) {
    NSLog(@"Mr. Higgie loves it");
  } else {
    NSLog(@"Mr. Higgie hates it");
  }
}


----------

NSDictionary *funnyWords = @{
  @"Schadenfreude": @"pleasure derived by someone from another person's misfortune.",
  @"Portmanteau": @"consisting of or combining two or more separable aspects or qualities",
  @"Penultimate": @"second to the last"
};

for (NSString *word in funnyWords) {
  NSString *definition = funnyWords[word];
  NSLog(@"%@ is defined as %@", word, definition);
}


----------

NSDictionary *newHats = @{
  @"Cowboy": @"White",
  @"Conductor": @"Brown",
  @"Baseball": @"Red"
};

for (NSString *hat in newHats){

  NSString *color = newHats[hat];

  NSLog(@"Trying on new %@ %@ hat", color, hat);

  if([mrHiggie tryOnHat:hat withColor:color]){
    NSLog(@"Mr. Higgie loves it");
  }else{
    NSLog(@"Mr. Higgie hates it");
  }
}


----------
void (^myFirstBlock)(void) = ^{
  NSLog(@"Hello from inside the block");
};

myFirstBlock();
myFirstBlock();


----------
void (^myFirstBlock)(NSString *) = ^(NSString *message){
  NSLog(@"Block message is: %@", message);
};

myFirstBlock(@"Hello");
myFirstBlock(@"World");


----------
NSArray *newHats = @[@"Cowboy", @"Conductor", @"Baseball", 
  @"Beanie", @"Beret", @"Fez"];

[newHats enumerateObjectsUsingBlock:
  ^(NSString *hat, NSUInteger index, BOOL *stop){
    NSLog(@"Trying on hat #%lu: %@", index+1, hat);
  }
];



Comments

Popular posts from this blog

MEGA 暫存檔案刪除

IOS GCD多執行緒

利用CMD指令強制刪除