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

时间: 2024-10-09 00:52:10

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 background Solution: Using UIImage+ImageEffects to Create a Blurred Popup Layer Code Fragments: // create the layer self.layer = [CALayer layer]; self.layer

iOS 7 Programming Pushing the Limits

Book Description In some ways, iOS 7 is the most radical change to iOS since the software development kit (SDK) was released in iPhone OS 2. The press and blogosphere have discussed every aspect of the new "flat" user interface and what it means

[iOS翻译]《iOS 7 Programming Pushing the Limits》系列:你可能不知道的Objective-C技巧

简介: 如果你阅读这本书,你可能已经牢牢掌握iOS开发的基础,但这里有一些小特点和实践是许多开发者并不熟悉的,甚至有数年经验的开发者也是.在这一章里,你会学到一些很重要的开发技巧,但这仍远远不够,你还需要积累更多的实践来让你的代码更强力. /* 本文翻译自<iOS 7 Programming Pushing the Limits>一书的第三章“You May Not Know”,想体会原文精髓的朋友请支持原书正版. ——————(博客园.新浪微博)葛布林大帝 */ 目录: 一. 最好的命名实践

Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- Messenger

Messenger类实际是对Aidl方式的一层封装.本文只是对如何在Service中使用Messenger类实现与客户端的通信进行讲解,对Messenger的底层不做说明.阅读Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- AIDL了解如何使用Aidl的方式实现服务端与客户端的通信. 在Service中使用Messenger,大部分代码还是跟Android的消息机制打交道,具体一点就是跟Handler,Mes

Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- ApiWrapper

前面两片文章讲解了通过AIDL和Messenger两种方式实现Android IPC.而本文所讲的并不是第三种IPC方式,而是对前面两种方式进行封装,这样我们就不用直接把Aidl文件,java文件拷贝到客户端了,而是为客户端提供一个aar(Anroid Archive)包.通过这个aar包对AIDL或者Messenger进行封装,最终客户端就可以像调用一般的java类一样,而不用处理通信方面的内容.(实际上书中说是要打包成jar包,但是在新建的Java Library Module中,我没能成功

Android Programming: Pushing the Limits -- Chapter 3: Components, Manifests, and Resources

Android Components Manifest文件 Resource and Assets v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} Normal 0 false 7.8 磅 0 2 false false false EN-US ZH-CN X-N

Android Programming: Pushing the Limits -- Chapter 2: Efficient Java Code for Android

Android's Dalvik Java 与 Java SE 进行比较 Java代码优化 内存管理与分配 Android的多线程操作 Android’s Dalvik Java 与 Java SE 进行比较: @.Dalvik虚拟机是register-based machine:Java SE虚拟机是stack machine. @.从Android 2.2 版本开始引进JIT(Just In Time)编译器,之前是纯解释器. @.Android SDK 使用dx这个工具把Java SE s

Android Programming: Pushing the Limits -- Chapter 1: Fine-Tuning Your Development Environment

ADB命令 Application Exerciser Monkey Gradle ProGuard 代码重用 版本控制 静态代码分析 代码重构 开发者模式   ADB命令: @.adb help:查看adb命令. @.adb devices:列出所有连接的安卓设备和模拟器. @.adb push <local> <remote> 把计算机里的文件拷贝到设备中. adb push e:\test.xml /sdcard/files.ldb/ @.adb pull <remot

Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- AIDL

服务端: 最终项目结构: 这个项目中,我们将用到自定义类CustomData作为服务端与客户端传递的数据. Step 1:创建CustomData类 package com.ldb.android.example.aidl; import android.os.Parcel; import android.os.Parcelable; import android.util.Log; import java.util.ArrayList; import java.util.Date; impor