2

What i am trying to do is save time when i writing my classes in objective C. If I define Properties in my .h file can i somehow make Xcode 5 autogetterate setters and getters for those properties.

Lets say this is my .h:

#import <UIKit/UIKit.h>

@interface PlayingCardView : UIView

@property (nonatomic) NSUInteger rank;
@property (nonatomic, strong) NSString* suit;
@property (nonatomic) BOOL faceUp;

@end

I would need Xcode to generate something like this:

#import "PlayingCardView.h"

@implementation PlayingCardView

- (void)setSuit:(NSString *)suit
{
    _suit = suit;
    [self setNeedsDisplay];
}

- (void)setRank:(NSUInteger)rank
{
    _rank = rank;
    [self setNeedsDisplay];
}

- (void)setFaceUp:(BOOL)faceUp
{
    _faceUp = faceUp;
    [self setNeedsDisplay];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

@end

So the setSuit.. and other setter would be auto generated/coded by Xcode.

Keep in mind that I am not looking for third party tools or scripts that would do this. I am wondering can Xcode do it natively? If yes, how?

Stefan Vasiljevic
  • 4,315
  • 1
  • 18
  • 17
  • you mean, ask Xcode to generate the getter & setter for you? – Raptor Mar 05 '14 at 04:39
  • Why do you need to call `setNeedsDisplay` if you don't implement `drawRect:`? `setNeedsDisplay` is kind of pointless if you don't have a `drawRect:` method to deal with the changes. – rmaddy Mar 05 '14 at 04:39
  • And based on the code you posted, there appears to be no reason to have explicit code for the getters, just the setters. – rmaddy Mar 05 '14 at 04:40
  • Do not "synthesis" properties in .m and you can access your properties with "_name" e.g "_faceUp" – Hamdullah shah Mar 05 '14 at 04:40
  • @Hamdullahshah using instance variables is not really the best way to go, you should always try to use properties, even when dealing with private variables. at least that's what apple recommends – jere Mar 05 '14 at 05:27
  • @Raptor yes! That is exactly what I mean. – Stefan Vasiljevic Mar 05 '14 at 17:11
  • @Merlevede that is an interesting solution. That is exactly what I am looking for. So Xcode does not have that feature? I have to use third party add-ons? – Stefan Vasiljevic Mar 05 '14 at 17:16
  • @StefanVasiljevic I think so! – Merlevede Mar 05 '14 at 17:31
  • It's Code Snippet. For more: http://stackoverflow.com/questions/28779480/how-to-implement-this-generate-setter-automatically – WangYudong Feb 28 '15 at 08:52

1 Answers1

2

xcode itself cant do this -- apple wants you to autosynthesize

BUT

there is a tool called accessorizer that can do this

note:I am in no way related/associated to the author of that app. I merely know of its existence :D

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135