Effective Objective-C 2.0 Learning Record (2)

Use literal syntax for NSString, NSNumber, NSArray, NSDictionary

NSString *str = "Lawrence"; 
//优于
NSString *str = [[]NSString alloc] initWithString:@"Lawrence"];

NSNumber *one = @1; 
//优于 
NSNumber *one = [NSNumber numberWithInteger: 1];

NSArray *array = @[@"one", @"two", @"three"];
NSDictionary *dictionary = @[@"firstKey": @"first", @"secondKey": @"second", @"thirdKey": @"third"];

Values ​​in arrays and dictionaries cannot have nil values.
Objects created using literal syntax are immutable. If you want a mutable version, you need to make a copy.

NSMutableArray *mutableArray = [@[@1, @2, @3, @4] mutableCopy];

Try not to use #define to define constants, try not to define constants
const in header files only to modify the variables on the right (basic data variable p, pointer variable *p)

Use enum variables to represent status, options, status codes

Attribute Traits
1. Atomicity, atomic/nonatomic, atomicity. In concurrent programming, if an operation has integrity, other parts of the system cannot view the temporary results of its intermediate processes, but can only see the initial value and end value, then this Operations are atomic.
By locking, atomicity is ensured. In iOS, atomic usage will have a large performance overhead, and it cannot be done to ensure thread safety.
2. Read/write permissions, readwrite and readonly
3. Memory management semantics

a) assign, setting method, only for scalar type (such as CGFloat, NSInteger, etc.)

b) strong, has a relationship, after setting a new value for this property, the setting method will first retain the new value, release the old value, and then set the new value up

c) weak, non-owning relationship, when a new value is set, neither the new value is retained nor the old value is released. When the object to which the attribute refers is destroyed, the value of the attribute will also be nil out.

d) unsafe_unretained

e) copy, owns the relationship, the setting method does not retain the new value, but copies the new value. When the property type is NSString, this feature is often used to ensure its encapsulation.
4. Method name, getter=

It's very simple, if there is an NSMutableString, and now use it to assign a value to a retain modified NSString, then just point the NSString to the location pointed to by the NSMutableString, and add one to the NSMUtbaleString counter. At this time, if the NSMutableString is modified, it will also cause NSString In principle, this is not allowed. If it is a copy-modified NSString object, when assigning a value to it with NSMutableString, a deep copy will be performed, and a copy of the content will also be copied, both of which point to different locations. Even if the value of NSMutableString is changed, the value of NSString will not change. Therefore, copy is used for safety, to prevent the value of the latter from changing due to the modification of the former when NSMutableString is assigned to NSString.

In the object memory, try to directly access instance variables, attribute access, self.instance, direct access, _instance
use direct access when reading, write data using attribute access
lazy loading, must be accessed through attributes

- (Person *)person {
    if (!_person) {
        _person = [Person new];
        ...
    }
    return _person;
}

object equality,

//You need to implement -isEqual: and -hash methods for your Request class.
-(BOOL)isEqualTo:(Object *)object;  -(NSUInteger)hash;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344572&siteId=291194637