Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

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.
 
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.
 
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!
 
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.
 
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?
 
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!
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.