我的技术博客经常被流氓网站恶意爬取转载。请移步原文:http://www.cnblogs.com/hamhog/p/3575599.html,享受整齐的排版、有效的链接、正确的代码缩进、更好的阅读体验。
在.h文件中:(写在@interface之前)
@class SomeClass;
在.m文件中:
#import "SomeClass.h"
NSNumber* intNumber = @5;
NSNumber* floatNumber = @5.2f;
NSNumber* doubleNumber = @5.27413;
NSNumber* expressionNumber = @(x*y);
NSNumber* someNumber = [NSNumber numberWithInt:5];
NSArray* animals = @[@"hamster", @"dog", @"cat"];
NSString* hamster = animals[0];
NSMutableArray* mutableArray = [@[@"hamster", @"dog", @"cat"]mutableCopy];
mutableArray[0] = @"hamster";
不要用:
NSArray* animals = [NSArray arrayWithObjects:@"hamster",@"cat",@"dog", nil];
NSString* hamster = [animals objectAtIndex:0];
NSDictionary* animalData =
@{@"name" : @"Tom",
@"age" : @2};
NSString* name = animalData[@"name"];
animalData[@"name"] = @"Jerry";
在.m文件中:(写在@implementaion之前)
static const double kSomeNumberConstant = 5.274;
static NSString* const kSomeStringConstant = @"hamster";
加字母k作为前缀。
在.h文件中:(写在@interface之前)
extern NSString* const HAMMyClassSomeStringConstant;
在.m文件中:(写在@implementation之前)
NSString* const HAMMyClassSomeStringConstant = @"hamster";
加类名作为前缀。
定义:
enum HAMAnimalState : NSInteger {
HAMAnimalStateSleeping,
HAMAnimalStateEating,
HAMAnimalStateDoingNothing,
};
typedef enum HAMAnimalState HAMAnimalState;
其中指定类型的 : NSInteger 可加可不加。加了的可以前向声明。
使用:
HAMAnimalState state = HAMAnimalStateSleeping;
定义:
enum HAMAnimalAbility {
HAMAnimalAbilityNone = 0,
HAMAnimalAbilitySing = 1 << 0,
HAMAnimalAbilityDance = 1 << 1,
HAMAnimalAbilityTalk = 1 << 2,
HAMAnimalAbilityRun = 1 << 3,
}
typedef enum HAMAnimalAbility HAMAnimalAbility;
使用:
//ability to both sing and dance
HAMAnimalAbility ability = HAMAnimalAbilitySing | HAMAnimalAbilityDance;
if (ability & HAMAnimalAbilitySing){
//has ability to sing
}
[Effective Objective-C 读书笔记] 第1章 几条基本写法 (2~5条),布布扣,bubuko.com
[Effective Objective-C 读书笔记] 第1章 几条基本写法 (2~5条)
原文:http://www.cnblogs.com/hamhog/p/3575599.html