How do I read Objective C methods
I think the biggest problem I'm having with understanding programmi开发者_开发问答ng is understanding what a particular method does. For example
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
When I look at the above method I know that it returns a boolean and that the method receives the following.
- NSTableView
- NSTableColumn
- NSInteger
However, I don't understand what I need to provide to use the method correctly. Do I just return a boolean?
It comes down to a saying: code tells you how, comments tell you why.
If you are writing a method, you need to know why you want the method and document it accordingly, a.k.a. comments. If you are overriding a method, then you would hope the producer of the method would document about what the method does.
If you're diving into iPhone development without a bit of programming background, you should at least read up the Introduction to The Objective-C Programming Language. If you want to find out what a particular method does in your code, you can always right click the method name and choose "Find Text in Documentation" to read more about it.
This method will be called by the table (or something) on your delegate (the class you are implementing this method in) when the table needs to know if a certain row and column can be edited. You just need to return YES
or NO
to indicate if you want to let it be edited.
This is an example implementation:
-(BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
return NO;
}
This will mean the table can never be edited.
A more complex implementation like this would let only the first row be editable:
-(BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
if (rowIndex == 0)
return YES;
else
return NO;
//This can be shortened to:
//return rowIndex == 0;
}
The name of the method tableView:shouldEditTableColumn:row: should give you a clue that it is about editing rows and columns in a table view. You could probably guess that, since it is returning a boolean it's called to determine with a table view should edit the supplied row of the supplied column. However, that would just be a guess, so at this point (if I hadn't been using that method only yesterday and thus know exactly what it is for) I would start googling for it (or using Xcode search if I had access to Xcode).
A useful tip for Googling Cocoa documentation is to search only on site:developer.apple.com so we'll try site:developer.apple.com tableView:shouldEditTableColumn:row:. The top hit in this case is NSTableViewDelegate which is the one we want.
精彩评论