今天讲Objective-C的整型类和集合类。
Values and Collections
Although Objective-C is an object-oriented programming language, it is a superset of C, which means you can use any of the standard C scalar (non-object) types like int, float and char in Objective-C code. There are also additional scalar types available
in Cocoa and Cocoa Touch applications, such as NSInteger, NSUInteger and CGFloat, which have different definitions depending on the target architecture.
Scalar types are used in situations where you just don’t need the benefits (or associated overheads) of using an object to represent a value. While strings of characters are usually represented as instances of the NSString class, numeric values are often stored
in scalar local variables or properties.
It’s possible to declare a C-style array in Objective-C, but you’ll find that collections in Cocoa and Cocoa Touch applications are usually represented using instances of classes like NSArray or NSDictionary. These classes can only be used to collect Objective-C
objects, which means that you’ll need to create instances of classes like NSValue, NSNumber or NSString in order to represent values before you can add them to a collection.
The previous chapters in this guide make frequent use of the NSString class and its initialization and class factory methods, as well as the Objective-C @"string" literal, which offers a concise syntax to create an NSString instance. This chapter explains how
to create NSValue and NSNumber objects, using either method calls or through Objective-C value literal syntax.
Basic C Primitive Types Are Available in Objective-C
Each of the standard C scalar variable types is available in Objective-C:
int someInteger = 42;
float someFloatingPointNumber = 3.1415;
double someDoublePrecisionFloatingPointNumber = 6.02214199e23;
as well as the standard C operators:
int someInteger = 42;
someInteger++; // someInteger == 43
int anotherInteger = 64;
anotherInteger--; // anotherInteger == 63
anotherInteger *= 2; // anotherInteger == 126
If you use a scalar type for an Objective-C property, like this:
@interface XYZCalculator : NSObject
@property double currentValue;
@end
it’s also possible to use C operators on the property when accessing the value via dot syntax, like this:
@implementation XYZCalculator
- (void)increment {
self.currentValue++;
}
- (void)decrement {
self.currentValue--;
}
- (void)multiplyBy:(double)factor {
self.currentValue *= factor;
}
@end
Dot syntax is purely a syntactic wrapper around accessor method calls, so each of the operations in this example is equivalent to first using the get accessor method to get the value, then performing the operation, then using the set accessor method to set the value to the result.