今天讲Objective-C中定义的除了C语言定义的以外的基本类型。
Objective-C Defines Additional Primitive Types
The BOOL scalar type is defined in Objective-C to hold a Boolean value, which is either YES or NO. As you might expect, YES is logically equivalent to true and 1, while NO is equivalent to false and 0.
Many parameters to methods on Cocoa and Cocoa Touch objects also use special scalar numeric types, such as NSInteger or CGFloat.
For example, the NSTableViewDataSource and UITableViewDataSource protocols (described in the previous chapter) both have methods requesting the number of rows to display:
@protocol NSTableViewDataSource <NSObject>
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView;
...
@end
These types, like NSInteger and NSUInteger, are defined differently depending on the target architecture. When building for a 32-bit environment (such as for iOS), they are 32-bit signed and unsigned integers respectively; when building for a 64-bit environment (such as for the modern OS X runtime) they are 64-bit signed and unsigned integers respectively.
It’s best practice to use these platform-specific types if you might be passing values across API boundaries (both internal and exported APIs), such as arguments or return values in method or function calls between your application code and a framework.
For local variables, such as a counter in a loop, it’s fine to use the basic C types if you know that the value is within the standard limits.