Things to note when writing OC code: objects, messages, run cycles

1. Understand the concept of attributes and analyze them when creating attributes.
@interface ViewController ()

@property NSString *name;
@property NSString *message;

@end

Equivalent to:

@interface ViewController ()

-(NSString *)name;
-(void)setName:(NSString *)name;
-(NSString *)message;
-(void)setMessage:(NSString *)message;

@end

(1)Attribute characteristics:

①Atomicity: nonatomic (non-atomic), atomic (atomicity);

②Read and write permissions: read only, read and write (readwrite);

③Memory management: scalar type (assign, unsafe_unretained), ownership relationship (strong), non-ownership relationship (weak), copy (do not retain new values, only copy)

@interface ViewController ()
  
@property (nonatomic, readwrite, strong) UIView *view;

@end

(2)Note:

① You can use @prooerty to define attributes.

② Correct definition of semantics through traits.

③When developing iOS, you should use the nonatomic attribute. Atomic performance will be affected.

2. Try to directly access the instance object inside the object.

(1) Called within the object, use the direct access form (self.name) when reading variables. Use attributes when setting variables (_name = name).

(2) In the initialization and dealloc method, use instance variables to read data.

(3) In the case of lazy initialization, data needs to be read through attributes. ( _name = name )

3. Identity of objects

(1) Checking the equality of objects requires providing isEqual and hash methods.

(2) The same object has the same hash code. But objects with the same hash code are not necessarily equivalent.

(3) Develop a specific monitoring plan and the probability of hash code collision.

4. "Family Mode" Shadow Hidden Implementation Details

(1) Determine whether the NSArray instance and NSMutableArray are equivalent:

    id mtuarray = [NSMutableArray class];
    if([mtuarray isKindOfClass:[NSArray class]])
    {
        NSLog(@"mtuarray是NAString第一种~~~");
    }

(2) Subclasses should inherit from the abstract base class in the class family.

(3) Subclasses should define their own data storage methods.

(4) Subclasses should override methods that should be overridden in the superclass document.

Guess you like

Origin blog.csdn.net/weixin_42357849/article/details/122132947