标签:
“我要成为一个高产的开发人员。”“想要混的好,就得多努力。”
写这些东西是因为毕竟看了书,但是看书看过去之后,也许印象不是很深刻,有些东西现在也理解不了,那我就把我理解的,现在就可以用到的东西,简单的写出来就好,让自己今后看到就能明白其中的意思。
还有就是锻炼一下表达,编辑能力,慢慢的提升自己,随时随地的都要有一个锻炼的心。
最后当然就是为了和大家分享一下,大家一起讨论讨论其实也满有必要的。
欢迎大家批评指教!
NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"object", @"key", nil];
NSDictionary *dict2 = @{@"key": @"object"};
NSArray *array1 = [[NSArray alloc] initWithObjects:@"object", nil];
NSArray *array2 = @[@"object1",@"object2",@"object1"];
NSString *string1 = [[NSString alloc] initWithString:@"string"];
NSString *string2 = @"string";
NSNumber *number1 = [[NSNumber alloc] initWithInt:1];
NSNumber *number2 = @1;
其中NSArray,NSDictionary还有对应的取值操作:
NSString *value1 = [dict1 objectForKey:@"key"];
NSString *value2 = dict2[@"key"];
NSString *str1 = [array1 objectAtIndex:1];
NSString *str2 = array2[1];
使用字面量的语法主要有两点好处:
使用字面量的语法创建的变量都是不可变的,这个时候我们需要将其复制一份:
NSMutableArray *array3 = [array2 mutableCopy];
在开发过程中,当我们需要固定某一个常量时,比如播放动画的时间,发送通知的name参数时,我们也许会这样做:
#define ANIMATION_DURATION 0.3
#define kPushNotificationName @"pushNotificationName"
但是使用预处理指令会存在这样几个问题:
那么对于这样的情况,我们可以使用如下的方式进行定义:
static const NSTimeInterval kAnimationDuration = 0.3;
一般来说使用该方法直接定义的变量,一般是用在 实现文件(.m文件)中,即当前文件可用,尽量不要将它定义到头文件(.h 文件)中。那么类似于 NSNotification 定义时用到的 name 参数,用来标记通知名称的常量不太适合直接用这样的方法来定义,因为一般来说 NSNotification 我们需要在 Post(发送通知),addObserver (添加通知进行接收)都要用到 name 常量,并且可能很多处都需要用到该常量,那么这个时候我们就需要采用如下的方法了:
// .h 文件中
#import <Foundation/Foundation.h>
@interface LoginManager : NSObject
extern NSString *const loginManagerPushNotification;
@end
// .m 文件中
#import "LoginManager.h"
NSString *const loginManagerPushNotification = @"LoginManagerPushNotification";
@implementation LoginManager
@end
最后说明一下,我们关于一些常量我们应该尽量避免使用 #define 预处理指令,而不是说不使用 #define 预处理指令,一些像这样的:
// 屏幕的尺寸
#define kScreenBounds ([[UIScreen mainScreen] bounds])
#define kScreenWidth (kScreenBounds.size.width)
#define kScreenHeight (kScreenBounds.size.height)
// 生成对应颜色
#define lUIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// 系统版本判断
#define kSysVer ([[[UIDevice currentDevice] systemVersion] floatValue])
#define iOS9Later (kSysVer >= 9.0f)
#define iOS8Later (kSysVer >= 8.0f)
#define iOS7Later (kSysVer >= 7.0f)
#define iOS6Later (kSysVer >= 6.0f)
#define iOS7 (kSysVer >= 7.0f && kSysVer < 8.0f)
// 系统当前语言
#define lLanguages [[NSLocale preferredLanguages] objectAtIndex:0]
我们还是可以使用的。
由于Objective-C 基于 C 语言,所以 C 语言的有的功能他都有。其中之一就是枚举类型:enum 。在以一系列常量来表示错误状态码或可组合的选项时,极宜使用枚举为其命名。有关枚举的语法,我在这就不多说了。
一般最简单的用法是:
typedef enum : NSInteger {
MessageUpdateSuccess,
MessageUpdateFailed
} MessageUpdateStatus;
当然我们也可以制定底层数据类型,就像这样:
typedef enum : NSInteger {
MessageUpdateSuccess = 1,
MessageUpdateFailed,
} MessageUpdateStatus;
就我个人而言目前以上两种用法就已经满足我的需求了,但是还有这样的一种情况,我现在悟的不是很明白,就姑且贴出来给大家看看吧。
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
此外系统还有定义了一些辅助的宏,用这样宏来指定保存枚举值的底层数据类型。这个我还是不是很明白,可能是目前还一直用不到的关系吧,这里我就不说了。
iOS开发中的那些的约定俗成(1)————《编写高质量iOS与OS X代码的52个有效方法》读书笔记(第一章)
标签:
原文地址:http://blog.csdn.net/ll845876425/article/details/51935875