THEO FAR .. my FAVORITE "ObjC Add-On" is ObjectMatcher
objswitch(someObject)
objcase(@"one") { // Nesting works.
objswitch(@"b")
objcase(@"a") printf("one/a");
objcase(@"b") printf("one/b");
endswitch // Any code can go here, including break/continue/return.
}
objcase(@"two") printf("It's TWO."); // Can omit braces.
objcase(@"three", // Can have multiple values in one case.
nil, // nil can be a "case" value.
[self self], // "Case" values don't have to be constants.
@"tres", @"trois") { printf("It's a THREE."); }
defaultcase printf("None of the above."); // Optional default must be at end.
endswitch
VÀ nó hoạt động với không dây, TOO ... trong vòng, thậm chí!
for (id ifNumericWhatIsIt in @[@99, @0, @"shnitzel"])
objswitch(ifNumericWhatIsIt)
objkind(NSNumber) printf("It's a NUMBER.... ");
objswitch([ifNumericWhatIsIt stringValue])
objcase(@"3") printf("It's THREE.\n");
objcase(@"99") printf("It's NINETY-NINE.\n");
defaultcase printf("some other Number.\n");
endswitch
defaultcase printf("It's something else entirely.\n");
endswitch
It's a NUMBER.... It's NINETY-NINE.
It's a NUMBER.... some other Number.
It's something else entirely.
tốt nhất của tất cả, có SO vài {...}
's, :
' s, và ()
's
Nguồn
2013-10-25 15:56:00
Không, chuyển đổi chỉ hoạt động trên các loại int/bool/char/etc. – chown
câu hỏi này hơi giống với câu hỏi này chỉ được đăng một giờ trước (http: // stackoverflow.com/questions/8161319/cao-performant-goal-c-lựa chọn thay thế-to-the-switch-tuyên bố-cho-đối tượng) –
Có một số cách khác để làm điều đó. Ví dụ, tải một mảng với các giá trị và tìm kiếm một kết quả phù hợp trong mảng. Không có hiệu quả khủng khiếp, nhưng chúng làm giảm sự sao chép mã. –