iOS Deallocate nonatomic copy setter

igorland

macrumors newbie
Original poster
Mar 4, 2014
24
0
Hello, World!

I have some experience in Java and C++ and this is my first experience with Objective C and iOS development. So, please bear with me if my question is silly.

I am trying to create non-atomic copy accessors, and I read everywhere that the object should be released at the end. So, if you could help me understand whether I am doing it properly, I would appreciate it. Will the following be correct?

Code:
    @interface ClassA: NSObject
    {
       NSString* stringA;
    }


    @property (nonatomic, copy) NSString* stringA;

    @implementation ClassA

    @synthesize stringA;

    -(void) setStringA: (NSString*) stringInput {
       if(stringA != stringInput) {
	      [stringA release];
	      stringA = [stringInput copy];
       }
    }

    -(void) dealloc {
        [stringA release];
        [super dealloc];
    }
I am looking for a confirmation whether I need to deallocate stringA in the dealloc method at the end and whether I did it correctly.

Many thanks.
 

hollersoft

macrumors member
Feb 10, 2013
94
53
Can't help much except to suggest you ditch the manual memory management and use ARC. You could just call -copy on the input string, set your property and you're done.

You don't need @synthesize these days either. And I would not recommend having the property and instance variable with the exact same name - that's confusing. Stick with the standard convention where @property stringA is auto-synthesized using an instance variable _stringA.
 

igorland

macrumors newbie
Original poster
Mar 4, 2014
24
0
Thanks a lot, guys!

hollersoft. I am working on a project for iPad/iPhone. Unless something has been changed recently, as far as I understand, it does not have ARC, therefore I need to release it manually.

Will the following be better?

Code:
@synthesize stringA = _stringA;

    -(void) setStringA: (NSString*) stringInput {
       if(_stringA != stringInput) {
	      [_stringA release];
	      _stringA = [stringInput copy];
              _stringA = [_stringA uppercaseString]; // reason for customized       setter
       }
    }
Thank you so much!
 

xStep

macrumors 68010
Jan 28, 2003
2,001
98
Less lost in L.A.
iOS does support ARC. Actually it depends on the Xcode version you're using. If you are going to post your app to the App Store, then you'll have to use Xcode 5 and at least SDK7.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,427
4,428
Pale blue comma
hollersoft. I am working on a project for iPad/iPhone. Unless something has been changed recently, as far as I understand, it does not have ARC, therefore I need to release it manually.
Transitioning to ARC Release Notes:
ARC is supported in Xcode 4.2 for OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5. Weak references are not supported in OS X v10.6 and iOS 4.​

Which version of Xcode are you using?
Which version of iOS are you targeting?
 

igorland

macrumors newbie
Original poster
Mar 4, 2014
24
0
Which version of Xcode are you using?
Xcode 5.0

Which version of iOS are you targeting?
I guess, the latest, or at least 6. Not sure if someone is still using anything lower than that.

Thanks for pointing to the statement. I guess I need to be careful when googling for this kind of things.

In light of the change, would you recommend synthesizing the setter (since I need to capitalize the ivar) and deleting the dealloc method in the end?

Thank you!
 

mfram

macrumors 65816
Jan 23, 2010
1,056
138
San Diego, CA USA
If you're calling uppercaseString, then I don't see why you need to call copy. uppercaseString is going to give you an uppercase copy anyway.