(转)编写高质量的OC代码
点标记语法
void *ptr = &value + 10 * 3;
NewType a = (NewType)b;
for (int i = 0; i <</span> 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 <</span>
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<</span>
UITalbeViewDataSource,
UITableViewDelegate> {
@private:
UITableView *_myTableView; // 私有实例变量
}
// 内部使用的属性
@property (nonatomic,strong) NSNumber *variableUsedInternally;
- (void)sortName; // 只用于内部使用的方法
@end
//MyViewController.m文件使用类扩展
@interface MyViewController()<</span>
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;
@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;
}
(转)编写高质量的OC代码的更多相关文章
- HTML Inspector – 帮助你编写高质量的 HTML 代码
HTML Inspector 是一款代码质量检测工具,帮助你编写更优秀的 HTML 代码.HTML Inspector 使用 JavaScript 编写,运行在浏览器中,是最好的 HTML 代码检测工 ...
- iOS应用开发最佳实践系列一:编写高质量的Objective-C代码
本文由海水的味道编译整理,转载请注明译者和出处,请勿用于商业用途! 点标记语法 属性和幂等方法(多次调用和一次调用返回的结果相同)使用点标记语法访问,其他的情况使用方括号标记语法. 良好的 ...
- 编程精粹--编写高质量C语言代码(3):自己设计并使用断言(二)
接着上一遍文章<<编程精粹--编写高质量C语言代码(2):自己设计并使用断言(一)>>,继续学习怎样自己设计并使用断言,来更加easy,更加不费力地自己主动寻找出程序中的错误. ...
- 怎样编写高质量的java代码
代码质量概述 怎样辨别一个项目代码写得好还是坏?优秀的代码和腐化的代码区别在哪里?怎么让自己写的代码既漂亮又有生命力?接下来将对代码质量的问题进行一些粗略的介绍.也请有过代码质量相关经验的朋友 ...
- 怎样编写高质量的 Java 代码
代码质量概述 怎样辨别一个项目代码写得好还是坏?优秀的代码和腐化的代码区别在哪里?怎么让自己写的代码既漂亮又有生命力?接下来将对代码质量的问题进行一些粗略的介绍.也请有过代码质量相关经验的朋友提出宝贵 ...
- 编程精粹--编写高质量C语言代码(4):为子系统设防(一)
通常,子系统都要对事实上现细节进行隐藏,在进行细节隐藏的同一时候.子系统为用户提供了一些关键入口点. 程序猿通过调用这些关键的入口点来实现与子系统的通信.因此假设在程序中使用这种子系统而且在其调用点加 ...
- 如何编写高质量的js代码--底层原理
转自: 如何编写高质量的 JS 函数(1) -- 敲山震虎篇 本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/7lCK9cHmunvYlbm ...
- 如何编写高质量的C#代码(一)
从"整洁代码"谈起 一千个读者,就有一千个哈姆雷特,代码质量也同样如此. 想必每一个对于代码有追求的开发者,对于"高质量"这个词,或多或少都有自己的一丝理解.当 ...
- 编写高质量的 Java 代码
代码质量概述 代码质量所涉及的5个方面,编码标准.代码重复.代码覆盖率.依赖项分析.复杂度分析.这5方面很大程序上决定了一份代码的质量高低. 我们分别来看一下这5方面:编码标准:这个想必都很清楚,每个 ...
随机推荐
- bzoj 1494 生成树计数
坑了好多天的题,终于补上了 首先发现 \(i\) 这个点和 \(i-k\) 之前的点没有边,所以 \(i-k\) 之前的点肯定联通,只要处理中间 \(k\) 个点的联通状态就好了.我们用最小表示法,\ ...
- Django的模型与字段
Django的模型,包含字段field和操作方法,每个模型在数据库中映射为一张表. 基本原则: 每个model在django中是一个Python类 每个model都是django.db.models. ...
- Asp.net开发必备51种代码
1.//弹出对话框.点击转向指定页面 Response.Write("<script>window.alert('该会员没有提交申请,请重新提交!')</script> ...
- SQL Server 2008 转换为 SQL 2005 数据库 脚本生成
Tips: 本文讨论如何把数据库从SQL Server 2008版本降低到2005,因为在本地开发是以SQL Server 2008 Express Edition版本进行的,而主机提供商现在提供的M ...
- Android Studio你必须学会的快捷键(Eclipse转AS必看)
前言:从Eclipse转到Android Studio之后,一开始把keymap设置成Eclipse,却发现有些常用的快捷键都失效了,大概是冲突了.想了下,觉得与其重新设置快捷键,不如去适应AS的快捷 ...
- python打开文件可以有多种模式
一.python打开文件可以有多种模式,读模式.写模式.追加模式,同时读写的模式等等,这里主要介绍同时进行读写的模式r+ python通过open方法打开文件 file_handler = open( ...
- MFC技术积累——基于MFC对话框类的那些事儿3
3.3.2 创建图形画刷来实现位图加载 1.首先在Resource View中导入一幅位图,位图大小96×96像素: 2.其次在主对话框中添加一个静态文本资源,ID号是IDC_BITMAPAREA,添 ...
- block的优势
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Blocks/Articles/bxOvervie ...
- osx launchpad图标的删除
安装了个parallels desktop之后,OSX中的launchpad中的图标多了不少,可是好多都不是我自己想要的,我们该怎么删除或者修改呢,下面介绍一些方法: ①直接操作Appications ...
- shell脚本,awk实现每个数字加1.
[root@localhost add]# cat file [root@localhost add]# cat file|awk '{for(i=1;i<=NF;i++){$i+=1}}1' ...