Encapsulating Data

Encapsulating Data

The synthesized methods follow specific naming conventions:

  • The method used to access the value (the getter method) has the same name as the property. The getter method for a property called firstName will also be called firstName.
  • The method used to set the value (the setter method) starts with the word “set” and then uses the capitalized property name.The setter method for a property called firstName will be called setFirstName:.

  If you want to use a different name for an accessor method, it’s possible to specify a custom name by adding attributes to the property. In the case of Boolean properties (properties that have a YES or NO value), it’s customary for the getter method to start with the word “is.” The getter method for a property called finished, for example, should be called isFinished. 

1 @property (readonly, getter=isFinished) BOOL finished;

  The default behavior for a writeable property is to use an instance variable called _propertyName. You Can Customize Synthesized Instance Variable Names:

1 @implementation YourClass
2 @synthesize propertyName = instanceVariableName;
3 @end

  You should always access the instance variables directly from within an initialization method because at the time a property is set, the rest of the object may not yet be completely initialized. 

Categories

  It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods. You can write your own accessor methods in the category implementation, but you won’t be able to keep track of a value for that property unless it’s already stored by the original class.

  If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime.

Represent Other Values Using Instances of the NSValue Class

  The NSNumber class is itself a subclass of the basic NSValue class, which provides an object wrapper around a single value or data item. In addition to the basic C scalar types, NSValue can also be used to represent pointers and structures.

  The NSValue class offers various factory methods to create a value with a given standard structure, which makes it easy to create an instance to represent, for example, an NSRange, like the example from earlier in the chapter:

1 NSString *mainString = @"This is a long string";
2 NSRange substringRange = [mainString rangeOfString:@"long"];
3 NSValue *rangeValue = [NSValue valueWithRange:substringRange];

  It’s also possible to create NSValue objects to represent custom structures. If you have a particular need to use a C structure (rather than an Objective-C object) to store information, like this:

1 typedef struct {
2     int i;
3     float f;
4 } MyIntegerFloatStruct;

  you can create an NSValue instance by providing a pointer to the structure as well as an encoded Objective-C type. The @encode() compiler directive is used to create the correct Objective-C type, like this:

1 struct MyIntegerFloatStruct aStruct;
2 aStruct.i = 42;
3 aStruct.f = 3.14;
4  
5 NSValue *structValue = [NSValue value:&aStruct
6                              withObjCType:@encode(MyIntegerFloatStruct)];
原文地址:https://www.cnblogs.com/tekkaman/p/3551139.html