iOS 7 Pushing the Limits - Good & Bad Namings in Cocoa

Cocoa is a dynamically typed language, and you can easily get confused about what type you are working with.
Collections (arrays, dictionaries, and so on) don’t have types associated with them, so it’s very easy to code
something accidentally like this:


NSArray *dates = @[@”1/1/2000”];
NSDate *firstDate = [dates firstObject];

 

This code compiles without a warning, but will crash with an unknown
selector exception.

 

Let‘s look at following code lines:

- (void)setURL:(NSString *)URL;              // Bad

- (void)setURLString:(NSString *)string;  // Good
- (void)setURL:(NSURL *)URL;                // Good

 

category methods

Because of the possibility of collisions, you should add a prefix to your category methods

Cocoa generally doesn’t use embedded underscores

A good use of categories is to provide utility methods to existing classes. When you do this, I recommend
naming the header and implementation files using the name of the original class plus the name of the
extension.

For example, you might create a simple PTLExtensions category on NSDate:

NSDate+PTLExtensions.h

@interface NSDate (PTLExtensions)
- (NSTimeInterval)ptl_timeIntervalUntilNow;
@end

NSDate+PTLExtensions.m

@implementation NSDate (PTLExtensions)
- (NSTimeInterval)ptl_timeIntervalUntilNow {
  return -[self timeIntervalSinceNow];
}
@end

 

iOS 7 Pushing the Limits - Good & Bad Namings in Cocoa,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。