iOS应用开发最佳实践:编写高质量的Objective-C代码
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开 页 ...
- 判断某值是否属于枚举类中的值_编写高质量可维护的代码之优化逻辑判断
if else.switch case 是日常开发中最常见的条件判断语句,这种看似简单的语句,当遇到复杂的业务场景时,如果处理不善,就会出现大量的逻辑嵌套,可读性差并且难以扩展. 编写高质量可维护的代 ...
- 编写高质量可维护的代码:优雅命名
大家好,我是若川.今天分享一篇关于如何命名更优雅的文章. 点击下方卡片关注我.加个星标,或者查看源码等系列文章.学习源码整体架构系列.年度总结.JS基础系列 本文首发于政采云前端团队博客:编写高质量可 ...
- 如何编写高质量的易语言代码?
什么是一个高效的软件?一个高效的软件不仅应该比实现同样功能的软件运行得更快,还应该消耗更少的系统资源.程序的时间效率是指运行速度,空间效率是指程序占用内存或者外存的状况.另外程序的效率我们也可以分为全 ...
- pythonic风格_如何编写高质量的Pythonic风格代码
每一种语言都有它的编程风格,打个比方就像各种菜系,粤菜,川菜,淮扬菜每一种都有自己的独特风格.Python的开发者用Pythonic这个词来形容那种符合python风格的代码.这种pythonic风格 ...
- 编写高质量代码:改善Java程序的151个建议(第1章:JAVA开发中通用的方法和准则___建议1~5)...
The reasonable man adapts himself to the world; The unreasonable one persists in trying ...
- iOS 编写高质量Objective-C代码(六)
级别: ★★☆☆☆ 标签:「iOS」「Block」「Objective-C」 作者: MrLiuQ 审校: QiShare团队 前言: 这几篇文章是小编在钻研<Effective Objecti ...
- 13个有用的javascript开发人员工具,用于编写高质量的代码
Today I'm going to show you 13 well-know and popular tools to write better and cleaner JavaScript co ...
- iOS 编写高质量Objective-C代码(八)
前言: 这几篇文章是小编在钻研<Effective Objective-C 2.0>的知识产出,其中包含作者和小编的观点,以及小编整理的一些demo.希望能帮助大家以简洁的文字快速领悟原作 ...
最新文章
- 机器学习是科学还是“炼金术”?
- 强化学习4——无模型控制model-free control (On-Policy learning:Sarsa和Off-policy learning:Q-learning)
- 游戏市场阴影下的手游厂商,和他们无法触碰的未来
- 1463. Happiness to People!
- Spring学习11-Spring管理各种数据源
- 谈谈JDK8中的字符串拼接
- 不加载执行js_前端性能优化:preload 预加载页面资源
- CentOS6修改/etc/fstab文件造成系统无法启动的问题
- Vc数据库编程基础1
- Ubuntu安装GCC 3.4
- 无线射频专题《IEEE 802.11协议讲解4@可调参数,性能与兼容性考虑》
- 台式计算机识别不了鼠标,USB无线鼠标失灵电脑检测不到无法识别怎么办
- A00工具类学习之参考文献写法
- 杨老师课堂之基于注解的SSH整合案例
- 东原仁知服务招股书失效:主营业务严重依赖迪马集团,曾多次更名
- 基于微服务架构的云平台总体设计
- 手机游戏的分析,设计,思考
- 饱和气压与温度的关系_饱和水蒸汽的压力与温度的关系介绍
- 计算机网络水晶头博客,两台电脑组成局域网(交叉线)的水晶头制作详解
- ZZULIOJ:1141: 进制转换