Pitfalls caused by properties in iOS development

Source: Internet
Author: User

Pitfalls caused by properties in iOS development
Copy-modified NSMutableArray attribute (property) initialization

For attributes:

@property (nonatomic, copy) NSMutableArray *someArray;

If self. someArray is used during initialization:

self.someArray = [[NSMutableArray alloc] initWithCapacity:200];

When used:

[self.someArray addObject:name];

APP Crash. The key Error Info is as follows:

-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7f9c89701c20

The reason is that if the property modified through copy isself.someArray =The value assignment Initialization is implemented through the setter method synthesized by the system. Because the copy modifier is set, the returned result is actually an unchangeable array (NSArray). An error is returned when the addObject method is called.

InitializationOrAssignmentAnd make the following changes:

_someArray = [[NSMutableArray alloc] initWithCapacity:200];

The APP runs normally because:_ SomeArray is an instance variable, and the instance variable is not modified by copy. It still points to the defined NSMutableArray type. Therefore, it is still feasible to use the addObject method through self. someArray, because NSMutableArray type objects are obtained in the initialization and assignment phase.

Do you know the best solution? In factcopyModify modifierstrongBecause the variable array object is a container, as long as its element is not mutually held with the self object, causing memory leakage, there will be no problem.

@ Synthesize @ correct use of dynamic

In the previous article Objective-C 2.0 basic points induction, the property is analyzed, but the functions of the keywords @ property, @ synthesize, and @ dynamic are not compared and parsed, this part will be analyzed in detail, but the problem is caused by the atomic modifier.

Keyword description

@ Property pre-compiled command (keyword) declares the getter and setter methods of the attribute @ synthesize to generate the attribute accessors: getter and setter Methods @ dynamic prohibits the compiler from generating getter/setter, you can use custom or dynamic binding during runtime: Mainly used in Core Data

Atomic is an atomic operation. As an attribute modifier, @ synchronized (self) or lock in the getter/setter Method Produced by the compiler only allows atomic access.

If you only customize one property accessor (one of setter/getter), the following error occurs: warning: writable atomic property 'name' cannot pair a synthesized getter...

If both are defined, @ synthesize name = _ name;If both of them are customized, the system will not synthesize the property accessors. In this case, you need to display the instance variable name that calls @ synthesize to define the property so that you can use the instance variable of the property.

Practice code:

TestAtomic class

TestAtomic. h

#import 
  
   @interface TestAtomic : NSObject@property (atomic, copy) NSString *name;@end
  

TestAutomic. m

#import TestAtomic.h@implementation TestAtomic@synthesize name = _name;- (NSString *)name{    if (![_name isEqualToString:@]) {        return _name;    }    else {        return nil;    }}- (void)setName:(NSString *)name{    @synchronized(self) {        if (![_name isEqualToString:name]) {            _name = name;        }    }}@end

TestB class, inheriting TestAtomic class
TestB. h

#import TestAtomic.h@interface TestB : TestAtomic@property (atomic, copy) NSString *Bname;- (instancetype)initWithSuperName;- (void)print;@end

TestB. m

#import TestB.h@implementation TestB- (instancetype)initWithSuperName{    self = [super init];    if (self) {        [self setName:@this is A];    }    return self;}- (void)print{    NSLog(@%@ --- %@, self.Bname, self.name);}@end

Main function:

#import 
  
   #include TestAtomic.h#include TestB.hint main(int argc, const char * argv[]){    @autoreleasepool {        TestB *testB = [TestB new];        testB.name = @this is good;        //testB = [testB initWithSuperName];        testB.Bname = @this is B;        [testB print];    }return 0;}
  
Reference resources

NSMutableArray Problems

Error: writable atomic property cannot pair a synthesized setter/getter

Authoritative introduction to property and synthesize in iphone Development

Implementation Analysis of atomic in iOS

@ Synthesize: difference between @ dynamic and @ dynamic

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.