码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-08-14 19:45:49      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   strong   for   ar   

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,布布扣,bubuko.com

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

标签:style   blog   color   os   io   strong   for   ar   

原文地址:http://www.cnblogs.com/davidgu/p/3912842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!