iOS应用开发最佳实践:编写高质量的Objective-C代码
本文转载至 http://www.cocoachina.com/industry/20131129/7445.html
点标记语法 属性和幂等方法(多次调用和一次调用返回的结果相同)使用点标记语法访问,其他的情况使用方括号标记语法。 良好的风格 : view.backgroundColor = [UIColor orangeColor]; [UIApplication sha

void *ptr = &value + 10 * 3;
NewType a = (NewType)b;
for (int i = 0; i < 10; i++) {
doCoolThings();
}
数组和字典类型的字面值的方括号两边各放置一个空格。
NSArray *theShit = @[ @1, @2, @3 ];
NSArray *theShit = @[
@"Got some long string objects in here.",
[AndSomeModelObjects too],
@"Moar strings."
];
NSDictionary *keyedShit = @{
@"this.key": @"corresponds to this value",
@"otherKey": @"remoteData.payload",
@"some": @"more",
@"JSON": @"keys",
@"and": @"stuff",
};

if (user.isHappy) {
//Do something
}
else {
//Do something else
}
if (!error) {
return success;
}
if (!error)
return success;
NSError *error;
if (![self trySomethingWithError:&error]) {
// Handle Error
}
NSError *error;
[self trySomethingWithError:&error];
if (error) {
// Handle Error
}
@interface CustomModelViewController : TTViewController <
TTModelDelegate,
TTURLRequestDelegate
> {
[object/class thing+condition];
[object/class thing+input:input];
[object/class thing+identifer:input];
realPath = [path stringByExpandingTildeInPath];
fullString = [string stringByAppendingString:@"Extra Text"];
object = [array objectAtIndex:3];
// 类方法
newString = [NSString stringWithFormat:@"%f",1.5];
newArray = [NSArray arrayWithObject:newString];
recipients = [email recipientsSortedByLastName];
newEmail = [CDCEmail emailWithSubjectLine:@"Extra Text"];
emails = [mailbox messagesReceivedAfterDate:yesterdayDate];
[object adjective+thing];
[object adjective+thing+condition];
[object adjective+thing+input:input];
capitalized = [name capitalizedString];
rate = [number floatValue];
newString = [string decomposedStringWithCanonicalMapping];
subarray = [array subarrayWithRange:segment];
-sortInfo // 是返回排序结果还是给info做排序
-refreshTimer // 返回一个用于刷新的定时器还是刷新定时器
-update // 更新什么,如何更新
-currentSortInfo // "current" 清楚地修饰了名词SortInfo
-refreshDefaultTimer // refresh是一个动词。
-updateMenuItemTitle // 一个正在发生的动作
color = [NSColor colorWithCalibratedHue: 0.10
saturation: 0.82
brightness: 0.89
alpha: 1.00];
//MyViewController.h文件
@interface MyViewController : UIViewController<
UITalbeViewDataSource,
UITableViewDelegate> {
@private:
UITableView *_myTableView; // 私有实例变量
}
// 内部使用的属性
@property (nonatomic,strong) NSNumber *variableUsedInternally;
- (void)sortName; // 只用于内部使用的方法
@end
//MyViewController.m文件使用类扩展
@interface MyViewController()<
UITalbeViewDataSource,
UITableViewDelegate> {
UITableView *_myTableView;
// 外部需要访问的实例变量声明为属性,不需要外部访问的声明为实例变量
NSNumber * variableUsedInternally;
}
// 从Xcode4.3开始,可以不写方法的前置声明,Interface Builder和Storyboard仍然可以找到方法的定义
@end
- (void) setTitle: (NSString *) aTitle;
- (void) setName: (NSString *) newName;
- (id) keyForOption: (CDCOption *) anOption
- (NSArray *) emailsForMailbox: (CDCMailbox *) theMailbox;
- (CDCEmail *) emailForRecipients: (NSArray *) theRecipients;
for (i = 0; i < count; i++) {
oneObject = [allObjects objectAtIndex: i];
NSLog (@"oneObject: %@", oneObject);
}
NSEnumerator *e = [allObjects objectEnumerator];
id item;
while (item = [e nextObject])
NSLog (@"item: %@", item);
@interface RNCSection: NSObject
@property (nonatomic) NSString *headline;
@end
@interface RNCSection : NSObject {
NSString *headline;
}
@interface RNCSection : NSObject {
NSString *headline;
}
@property (nonatomic) NSString *headline;
@end
@interface RNCSection: NSObject
@property (nonatomic) NSString *headline;
@end
NSString *accountName;
NSMutableArray *mailboxes;
NSArray *defaultHeaders;
BOOL userInputWasUpdated;
NSString *accountNameString;
NSMutableArray *mailboxArray;
NSArray *defaultHeadersArray;
BOOL userInputWasUpdatedBOOL;
NSImage *previewPaneImage;
NSProgressIndicator *uploadIndicator;
NSFontManager *fontManager; // 基于类名命名
NSDictionary * keyedAccountNames;
NSDictionary * messageDictionary;
NSIndexSet * selectedMailboxesIndexSet;
/**
* The maximum size of a download that is allowed.
*
* If a response reports a content length greater than the max * will be cancelled. This is helpful for preventing excessive memory usage.
* Setting this to zero will allow all downloads regardless of size.
*
* @default 150000 bytes
*/
@property (nonatomic) NSUInteger maxContentLength;
- (instancetype)init {
self = [super init]; // or call the designated initalizer
if (self) {
// Custom initialization
}
return self;
}
NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul"];
NSDictionary *productManagers = @{@"iPhone" : @"Kate", @"iPad" : @"Kamal", @"Mobile Web" : @"Bill"};
NSNumber *shouldUseLiterals = @YES;
NSNumber *buildingZIPCode = @10018;
NSArray *names = [NSArray arrayWithObjects:@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul", nil];
NSDictionary *productManagers = [NSDictionary dictionaryWithObjectsAndKeys: @"Kate", @"iPhone", @"Kamal", @"iPad", @"Bill", @"Mobile Web", nil];
NSNumber *shouldUseLiterals = [NSNumber numberWithBool:YES];
NSNumber *buildingZIPCode = [NSNumber numberWithInteger:10018];
CGRect frame = self.view.frame;
CGFloat x = CGRectGetMinX(frame);
CGFloat y = CGRectGetMinY(frame);
CGFloat width = CGRectGetWidth(frame);
CGFloat height = CGRectGetHeight(frame);
CGRect frame = self.view.frame;
CGFloat x = frame.origin.x;
CGFloat y = frame.origin.y;
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
static NSString * const RNCAboutViewControllerCompanyName = @"The New York Times Company";
static const CGFloat RNCImageThumbnailHeight = 50.0;
#define CompanyName @"The New York Times Company"
#define thumbnailHeight 2
#ifndef NS_ENUM
#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
#endif
typedef NS_ENUM(NSInteger, RNCAdRequestState) {
RNCAdRequestStateInactive,
RNCAdRequestStateLoading
};
@interface NYTAdvertisement ()
@property (nonatomic, strong) GADBannerView *googleAdView;
@property (nonatomic, strong) ADBannerView *iAdView;
@property (nonatomic, strong) UIWebView *adXWebView;
@end
if (!someObject) {
}
if (someObject == nil) {
}
if (isAwesome)
if (![someObject boolValue])
if ([someObject boolValue] == NO)
if (isAwesome == YES) // Never do this.
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
iOS应用开发最佳实践:编写高质量的Objective-C代码的更多相关文章
- iOS应用开发最佳实践
<iOS应用开发最佳实践> 基本信息 作者: 王浩 出版社:电子工业出版社 ISBN:9787121207679 上架时间:2013-7-22 出版日期:2013 年8月 开本:16 ...
- iOS应用开发最佳实践系列一:编写高质量的Objective-C代码
本文由海水的味道编译整理,转载请注明译者和出处,请勿用于商业用途! 点标记语法 属性和幂等方法(多次调用和一次调用返回的结果相同)使用点标记语法访问,其他的情况使用方括号标记语法. 良好的 ...
- 借助 SublimeLinter 编写高质量的 JavaScript & CSS 代码
SublimeLinter 是前端编码利器——Sublime Text 的一款插件,用于高亮提示用户编写的代码中存在的不规范和错误的写法,支持 JavaScript.CSS.HTML.Java.PHP ...
- 编程精粹:编写高质量的C语言代码———笔记一
第一章 假想的编译程序 要记得对空语句进行处理,最好使用NULL使其明显可见 char * strcpy(char* pchTo, char* pchFrom) { char* pchStart = ...
- 编程精粹--编写高质量C语言代码(4):为子系统设防(一)
通常,子系统都要对事实上现细节进行隐藏,在进行细节隐藏的同一时候.子系统为用户提供了一些关键入口点. 程序猿通过调用这些关键的入口点来实现与子系统的通信.因此假设在程序中使用这种子系统而且在其调用点加 ...
- iOS书摘之编写高质量iOS与OS X代码的52个有效方法
来自<Effective Objective-C 2.0编写高质量iOS与OS X代码的52个有效方法>一书的摘要总结 一.熟悉Objective-C 了解Objective-C语言的起源 ...
- 编写高质量代码:改善Java程序的151个建议(第一章:JAVA开发中通用的方法和准则)
编写高质量代码:改善Java程序的151个建议(第一章:JAVA开发中通用的方法和准则) 目录 建议1: 不要在常量和变量中出现易混淆的字母 建议2: 莫让常量蜕变成变量 建议3: 三元操作符的类型务 ...
- 编写高质量代码:Web前端开发修炼之道(一)
最近老大给我们买来一些技术方面的书籍,其实很少搬着一本书好好的完整的看完过,每每看电子档的,也是打游击式的看看这章,瞅瞅那章,在那5本书中挑了一本比较单薄的<编写高质量代码web前端开发修炼之道 ...
- 《编写高质量代码--Web前端开发修炼之道》读书笔记
前言 这两周参加公司的新项目,采用封闭式开发(项目成员在会议室里开发),晚上加班到很晚,所以没时间和精力写原创博客了,今天就分享下这篇<编写高质量代码--Web前端开发修炼之道>读书笔记吧 ...
随机推荐
- 关于Future.cancel(mayInterruptIfRunning)方法的参数的问题
mayInterruptIfRunning设成false话,不允许在线程运行时中断,设成true的话就允许. 可以参考下面的代码来理解,如果设为false的话,会打印到99999,如果设成true的话 ...
- Linux yum操作无效的解决方法
1.没网,试着:ping www.baidu.com 如果显示没有连接的话,就说明没网,也就无法使用yum 命令. 2.ping通了的话,还是是用不了yum命令,说明是yum镜像没有了,那么就得下载一 ...
- 从构建分布式秒杀系统聊聊验证码 给大家推荐8个SpringBoot精选项目
前言 为了拦截大部分请求,秒杀案例前端引入了验证码.淘宝上很多人吐槽,等输入完秒杀活动结束了,对,结束了...... 当然了,验证码的真正作用是,有效拦截刷单操作,让羊毛党空手而归. 验证码 那么到底 ...
- 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】
[101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, check whether ...
- mysql-ubuntu14.04彻底卸载mysql
删除mysql的数据文件 sudo rm /var/lib/mysql/ -R 删除mysql的配置文件 sudo rm /etc/mysql/ -R 自动卸载mysql(包括server和clien ...
- DataURL与File,Blob,canvas对象之间的互相转换的Javascript (未完)
canvas转换为dataURL (从canvas获取dataURL) var dataurl = canvas.toDataURL('image/png'); var dataurl2 = canv ...
- Atitit.减少http请求数方案
Atitit.减少http请求数方案 1. 原理与方法1 1.1. -------jsp1 1.2. "index/js.txt";2 1.3. connReduceDync2 1 ...
- [svc]linux常用手头命令-md版-2017年11月12日 12:31:56
相关代码 curl命令-网站如果3次不是200或301则报警 curl -o /dev/null -s -w "%{http_code}" baidu.com -k/--insec ...
- Tomcat启动时报 java.lang.OutOfMemoryError: Java heap space
见效的解决方法如下: 在myeclipse中修改jvm启动的参数 打开Myeclipse -->windows-->preference-->myeclipse->serv ...
- spring-test使用介绍
一.首先引入spring的jar文件到项目中,我采用maven管理项目依赖的jar包: <properties> <spring.version>4.0.0.RELEASE&l ...