3

I'm a student in a internship, and i'm learning Objective-C in order to develop an IOS application. They already had an existing base of code, but some part of the code give me problems.

As the previous developer isn't in the company anymore, and because no one else know about Objective-C, no one can answer some of my questions about how the application is built, so I can't determine if it is that i don't understand, or if it's just bad practices.

Here are these questions :

1°) In some classes, i found code like this :

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated] }

This code is useless, right ?

2°) In like 9/10 methods in the project, they return (void). Is it a common pratice in Obj-C (because everything is a pointer) ?

3°) Sometimes there is the interface declaration in both header and messages files. I guess it's because you want to declare only a part in header for a future include, and to have a "private" part. But in a file, i find the following code :

In header :

@interface WebViewController : UIViewController
    @properties ...
@end

In Msg file :

#import ...
@interface WebViewController ()

@end

@implementation WebViewController ...

What's the point declaring a void interface a second time in the msg file ?

4°) More, in another class, the interface is declared a second time too, but a method is defined (in the msg file). What's the point as the method is defined bellow, and is not declared in the header file ?

thank you in advance

  • These are some very basic questions that can be found with a little bit of research and searching on Stackoverflow. Have you done any research before hand? – Popeye Jun 16 '14 at 12:33
  • .m doesn't stand for "message", but for "implementation" files ;) – nburk Jun 16 '14 at 12:35
  • The first item: It's very easy to set a breakpoint in such a "pointless" method to figure out when it is called. Which makes it useful. Code gets written to make an app easier to debug, which is very important once your code grows. – gnasher729 Jun 16 '14 at 12:37
  • Please read https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html this document will answer all your questions. – Popeye Jun 16 '14 at 13:02

2 Answers2

4

Welcome to Objective-C :)

  1. Not necessarily. The super class may have specific behaviour defined in it's own method implementation that would cause an issue if you didn't call it. Overriding methods means the super classes own method won't be called by default. Added from comment: Of courser if you didn't override it then the superclass definition would get called just fine. There are 2 common reasons why you would find it overridden: a. It's in the Xcode template so it has always been there and not been removed b.it used to have other content but it was deleted and the method call left behind.

  2. Yes. Although you don't explicitly return void in the method, you do need to specify some return type. If you're not returning anything then void is the right value. It's found commonly in obj-c classes as the method may respond to being called by mutating an internal ivar or property and not require a return. Alternatively the result might be to send a notification so a return value is not needed. Increasingly the use of block-based completion handlers replaces explicit value return as a way to respond to the contents of a method.

  3. Yes, it's to give a private interface that you don't want exposed. In the case where there's nothing in the private interface it's probably there because it came with the template code from Xcode and no one removed it. You can ignore or remove.

  4. For the one you mention with a method, while it's not required to declare private methods in an interface it makes sense from the point of view of writing readable code (a strongly endorsed concept in obj-c). Since the compiler will remove any unnecessary code it makes no difference to declare it and makes the task of reading the code and understanding the class that much easier when you or someone else returns to it later. It's a also a good place to put documentation in comments as it groups it all together.

Hope that helps. Check out the Objective-C programming guides from Apple to for more best practice tips.

Cocoadelica
  • 3,006
  • 27
  • 30
  • I think that question #1 is basically "why declare an override function when all it does is call the parent version" – Peter M Jun 16 '14 at 12:24
  • @PeterM Yes, that's what i wanted to say :) – DadEap Purple Jun 16 '14 at 12:29
  • For the 2nd question, my question was more : Is having a lot of method return void common in obj-c : for example, i've been programming a lot in java, and it was rarely the case, most of them returned actually something. – DadEap Purple Jun 16 '14 at 12:31
  • 1
    Hey, yes with #1 if you don't need to do anything except call the parent version then you could not override at all. There are 2 common reasons why you would declare it: It's in the template so it has always been there and not been removed & it used to have other content but it was deleted and the method call left behind. In terms of the #2, yes I see a lot of void returning methods as it's common to mutate the class properties/ivars or perhaps to fire a notification rather than send a return value. – Cocoadelica Jun 16 '14 at 12:42
  • Great question btw, I like ones that make us look again at base assumptions. – Cocoadelica Jun 16 '14 at 12:42
  • (3) This is an Objective-C empty category, can be used for private declarations. – Rukshan Jun 16 '14 at 12:57
2

1). Yes you can delete this method

2). It depends upon your requirement, whether you want a return type or not. e.g.

- (BOOL)isEmptyOrNull:(NSString*)str;

3). These are called extensions, you can read more about it here http://rypress.com/tutorials/objective-c/categories.html

Extensions are used to hide the methods from out side world(by saying hide I mean you can't find those methods with your eyes)

4). Methods define in implementation file are not visible to a programmer, it's just like private methods in java but in ObjC there is no such thing like private method. Read this thread for private methods Best way to define private methods for a class in Objective-C

Community
  • 1
  • 1
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184