SAP DEV
SAP DEV

Reputation: 111

Automatic code generated

By analogy, in Eclipse IDE and many others, when I declare some private attributes of the class, I can generate the getters and setters methods automatically. It's very useful for example, to declare with about 20 attributes and I finally have all my methods created.

Have we got the same functionality in xCode IDE, I mean ... When I am coding in the ".h" some declarations methods, could I have automatically the declaration in the ".m" ....

e.g:

In ".h", the code bellow:

- (NSString *)loadMessageLabelFr:(id)sender;

In ".m", the code 'desired' generated with a menu functionality:

- (NSString *)loadMessageLabelFr:(id)sender{

}

Upvotes: 1

Views: 77

Answers (3)

Sulthan
Sulthan

Reputation: 130142

I know what you mean - many times, after declaring 10 methods in my header, I wanted them somehow transferred to my implementation file.

However...

This is not Java, the objects and file organization is completely different

  1. You don't have to define the method and it is still a valid use case. There are classes which don't have specific method definitions and handle the method calls in doesNotRecognizeSelector:

  2. You can add method definitions at runtime

  3. You can define methods from one header in multiple implementation files.

By these reasons (and many others), generating method headers in the implementation files could have more cons than pros.

The simplest Xcode solution are

  1. Copy-paste method headers from the .h to the .m.
  2. Use autocompletion, writing the beginning of the header, e.g. - (BOOL) will allow you to autocomplete faster.

Upvotes: 1

TotoroTotoro
TotoroTotoro

Reputation: 17622

Getters and setters are generated implicitly by the compiler for your class properties. E.g. having

@property int count;

implicitly gives you the following things:

1) an ivar named _count, which stores the value of the property.

2) a getter and a setter

- (int)count
{
    return _count;
}

- (void)setCount:(int)count
{
    _count = count;
}

This code is inserted at compile time, and so you don't see it in your .m file. It is nice because this way your code is not cluttered by trivial getters and setters. If you want your getter or setter to do something else than just set or return a value, you need to define the method the usual way, e.g.:

- (void)setCount:(int)count
{
    NSLog(@"Count set to %d", count);
    _count = count;
}

Upvotes: 3

Wain
Wain

Reputation: 119031

How are you defining your instance variables?

The answer to your question (for instance variables) is 'no, but do you really need them'... Use properties and the accessor methods are created for you during compilation. In practice you need to explicitly implement very few accessor methods.

For the public methods, the answer is just no, but, you can copy the method definitions and then just add the brackets so not a lot of work (you don't even need to remove the semi-colon).

Also, and this works for both variables and publicly defined methods, start typing:

- a

and Xcode will offer auto completions of the line for you with appropriate method names.

Upvotes: 0

Related Questions