@interface Tiger : NSObject
@property (nonatomic) int age;
@end
@implementation Tiger
@end
//"when" guards are plain conditions
//joined with the type checks
NSString *describe(id obj) {
if ([obj isKindOfClass:[NSNumber class]]
&& [obj intValue] > 1000000) {
return @"Big number";
}
if ([obj isKindOfClass:[NSString class]]
&& [obj length] < 256) {
return @"Short string";
}
if ([obj isKindOfClass:[Tiger class]]
&& [obj age] > 12) {
return @"Old tiger";
}
return @"Unknown type";
}
Tiger *tiger = [Tiger new];
tiger.age = 15;
NSString *result = describe(tiger);
//result is "Old tiger"