davetron5000
davetron5000

Reputation: 24831

How to organize code within on Objective-C class/file?

New to iPhone development, not new to software development in general. I want my Objective-C/iPhone source code to look "unsurprising" to someone else reading it.

Wondering what is the accepted practice for code organization within one class?

As an example, I have a view controller like so:

@interface SomeViewController : UIViewController 
    <UIPickerViewDelegate, 
    UIPickerViewDataSource, 
    UITextFieldDelegate> {
}

What is the accepted practice regarding the ordering/placement of the methods for these protocols within the .m file? I know that it doesn't technically matter, but, as I said, I want someone reading my code to be unsurprised at how I've organized it.

As an aside, if implementing these sorts of protocols on my view controllers is considered a bad practice, please let me know in a comment, and I'll ask another question for that (or just point me to an existing one)

Upvotes: 1

Views: 923

Answers (2)

vodkhang
vodkhang

Reputation: 18741

I think the best practice is this :

1/ Put all the methods in the same protocol near each other

2/ Put the #pragma at the top of that block

#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
  // some code
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
}

#pragma mark UIPickerViewDelegate

So, when people use XCode to see the list of methods, they will know where to look for a method in a particular protocol:)

Upvotes: 2

bdrister
bdrister

Reputation: 448

While the Apple sample code is certainly not perfect, it at least is a good model for being "unsurprising" in how you organize your source. Remember, every other iPhone programmer out there will have seen a lot of Apple sample code already, so they'll already be familiar with how it's structured.

Upvotes: 0

Related Questions