Share
Embed
Transcript
18 slides
-
General rules For methods that represent actions an object •
takes, start the name with a verb. E.g.: • - (void)invokeWithTarget:(id)target; • - (void)selectTabViewItem:(NSTabViewItem *)tabViewItem • -
General rules If the method returns an attribute of the
• receiver, name the method after the attribute. E.g.: • - (NSSize)cellSize; Right. - (NSSize)calcCellSize; Wrong. - (NSSize)getCellSize; Wrong. -
General rules Make the word before the argument describe •
the argument. E.g.: • - (id)viewWithTag:(NSInteger)aTag; Right. - (id)taggedView:(int)aTag; Wrong. -
General rules Don’t use “and” to link keywords that are
• attributes of the receiver. E.g.: • - (int)runModalForDirectory:(NSString *)path file:(NSString *) name types:(NSArray *)fileTypes; Right. - (int)runModalForDirectory:(NSString *)path andFile:(NSString *)name andTypes:(NSArray *)fileTypes; Wrong. -
Accessor Methods Accessor methods are those methods that set and
return • the value of a property of an object. If the property is expressed as a noun, the format is: • - (NSString *)title; • - (void)setTitle:(NSString *)aTitle; • If the property is expressed as an adjective, the format is: • - (BOOL)isEditable; • - (void)setEditable:(BOOL)flag; • -
Accessor Methods If the property is expressed as a verb,
the format is: • - (BOOL)showsAlpha; • - (void)setShowsAlpha:(BOOL)flag; • The verb should be in the simple present tense. • - (void)setAcceptsGlyphInfo:(BOOL)flag; Right. - (BOOL)acceptsGlyphInfo; Right. - (void)setGlyphInfoAccepted:(BOOL)flag; Wrong. - (BOOL)glyphInfoAccepted; Wrong. -
Accessor Methods You may use modal verbs (verbs preceded by
“can”, “should”, • “will”, and so on) to clarify meaning, but don’t use “do” or “does”. - (BOOL)canHide; • - (BOOL)doesAcceptGlyphInfo; • -
Accessor Methods Use “get” only for methods that return objects
and values • indirectly. You should use this form for methods only when multiple items need to be returned. E.g.: • - (void)getLineDash:(float *)pattern count:(int *)count phase:(float • *)phase; -
Delegate Methods Start the name by identifying the class of
the object • that’s sending the message. E.g.: • - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row; • - (BOOL)application:(NSApplication *)sender openFile:(NSString • *)filename; -
Delegate Methods Use “did” or “will” for methods that are
invoked to • notify the delegate that something has happened or is about to happen. E.g.: • - (void)browserDidScroll:(NSBrowser *)sender; • - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow • *)window; -
Naming notifications [Name of associated class] • + [Did |
Will] • + [UniquePartOfName] • + Notification • E.g.: • NSApplicationDidBecomeActiveNotification NSWindowDidMiniaturizeNotification NSTextViewDidChangeSelectionNotification NSColorPanelColorDidChangeNotification -
Coding Style Position of “*”: • NSString *varName; • Method
declaration • - (void)doSomethingWithString:(NSString *)theString{ • } A space after -/+ space No space for first argument -
Coding Style Method name shorter than arguments: - (void)short:(GTMFoo *)theFoo
longKeyword:(NSRect)theRect evenLongerKeyword:(float)theInterval{ } At least 4 space indentation • Align vertically • -
Coding Style For constent like (#define, enums, const), use ‘k’
as prefix: • E.g.: kInvalidHandle • #Import/#Include • Import objectiveC; • Include c/c++ • Import whole framework even when only need a few files • #import <Foundation/NSArray.h> • #import <Foundation/Foundation.h> • -
Coding Style BOOL trap: • BOOL defined as unsigned char
• DO NOT compare BOOL with YES/NO • BOOL great = [foo isGreat]; • If(great == YES) • If(great) • -
Reference Coding guidelines for Cocoa • https://developer.apple.com/library/ios/#documentation/Cocoa /Conceptual/CodingGuidelines/CodingGuidelines.html#//apple_ ref/doc/uid/10000146-SW1 Daniel’s
Objective-C Coding Style http://www.slideshare.net/AVking/daniels-objective-c-coding- style-guidelines?from=embed