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的更多相关文章
- iOS 7 Pushing the Limits Notes - create a layer like notification center's or control center's background
Problem: How to create a layer that looks like your notification center's or control center's backgr ...
- [iOS翻译]《iOS 7 Programming Pushing the Limits》系列:你可能不知道的Objective-C技巧
简介: 如果你阅读这本书,你可能已经牢牢掌握iOS开发的基础,但这里有一些小特点和实践是许多开发者并不熟悉的,甚至有数年经验的开发者也是.在这一章里,你会学到一些很重要的开发技巧,但这仍远远不够,你还 ...
- Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- ApiWrapper
前面两片文章讲解了通过AIDL和Messenger两种方式实现Android IPC.而本文所讲的并不是第三种IPC方式,而是对前面两种方式进行封装,这样我们就不用直接把Aidl文件,java文件拷贝 ...
- Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- Messenger
Messenger类实际是对Aidl方式的一层封装.本文只是对如何在Service中使用Messenger类实现与客户端的通信进行讲解,对Messenger的底层不做说明.阅读Android Prog ...
- Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- AIDL
服务端: 最终项目结构: 这个项目中,我们将用到自定义类CustomData作为服务端与客户端传递的数据. Step 1:创建CustomData类 package com.ldb.android.e ...
- Android Programming: Pushing the Limits -- Chapter 6: Services and Background Tasks
什么时候使用Service 服务类型 开启服务 后台运行 服务通信 附加资源 什么时候使用Service: @.任何与用户界面无关的操作,可移到后台线程,然后由一个Service来控制这个线程. 服务 ...
- Android Programming: Pushing the Limits -- Chapter 5: Android User Interface Operations
多屏幕 自定义View 多屏幕 @.Android 4.2 开始支持多屏幕. @.举例: public class SecondDisplayDemo extends Activity { priva ...
- Android Programming: Pushing the Limits -- Chapter 4: Android User Experience and Interface Design
User Stories Android UI Design 附加资源 User Stories: @.通过写故事来设计应用. @.每个故事只关注一件事. @.不同的故事可能使用相同的组件,因此尽早地 ...
- Android Programming: Pushing the Limits -- Chapter 3: Components, Manifests, and Resources
Android Components Manifest文件 Resource and Assets v\:* {behavior:url(#default#VML);} o\:* {behavior: ...
随机推荐
- ubuntu 挂起唤醒和声音偏小的问题
自从开始用ubuntu就遇到了声音偏小的问题,一直很让我头疼.还好插上耳机后勉强能用,也就没继续追究了. 可最近发现了一个更加严重的问题挂起后竟然无法唤醒,一直是黑屏的状态,必须强制关机再重启,这就蛋 ...
- VC6兼容性及打开文件崩溃问题解决
VC6虽然老,但是一些工程还非得用它打开,没办法…… 今天偶然用到,因为新装了系统,之前的问题又要重新解决一遍 在这记录下解决过程,方便以后查阅: 一.兼容问题: XP以上windows系统打开VC6 ...
- Python的16个“坑”
1. 不要使用可变对象作为函数默认值 代码如下: In [1]: def append_to_list(value, def_list=[]): ...: def_list.append(value) ...
- Linux进程间通信——使用共享内存
一.什么是共享内存 顾名思义,共享内存就是允许两个不相关的进程访问同一个逻辑内存.共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式.不同进程之间共享的内存通常安排为同一段物理内存. ...
- (phpmyadmin error)Login without a password is forbidden by configuration (see AllowNoPassword) in ubuntu
1.Go to /etc/phpmyadmin/config.inc.php and open it your favorite editor. 2.Search for below line of ...
- 第二百零七天 how can I坚持
都这么一大把年纪了,还没正事,哎.. mysql ifnull(expr,expr),oracle nvl();去null函数. 每天也没什么事. 哎,每天 也总有那么点事. 刘松.李承杰,都是些什么 ...
- Django 的 CSRF 保护机制(转)
add by zhj:假设用户登录了网站A,而在网站B中有一个CSRF攻击标签,点击这个标签就会访问网站A,如果前端数据(包括sessionid)都放在本地存储的话, 当在网站B点击CSRF攻击标签时 ...
- [翻译]创建ASP.NET WebApi RESTful 服务(7)
实现资源分页 本章我们将介绍几种不同的结果集分页方式,实现手工分页,然后将Response通过两个不同的方式进行格式化(通过Response的Envelop元数据或header). 大家都知道一次查询 ...
- HD2043猜密码
密码 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
- Type Encoding
[Type Encodings] The compiler encodes the return and argument types for each method in a character s ...