Objective-c语法快速记忆法

转自http://www.raywenderlich.com/4872/objective-c-cheat-sheet-and-quick-reference

 

Class Header (.h)

#import "AnyHeaderFile.h"

@protocol ClassNameDataSource, ClassNameDelegate;

//声明协议:
@protocal ClassNameDataSource <NSObject>

    @optional//选择实现

    - (NSUInteger)numberOfItemsInCarousel:(ClassName*)carousel;

    @required//必须实现

     -(id) copywithzone:(NSZone *) zone; //方法列表
@end

 

@protocal ClassNameDelegate <NSObject>

    @optional//选择实现

    - (NSUInteger)numberOfItemsInCarousel:(ClassName*)carousel;

    @required//必须实现

     -(id) copywithzone:(NSZone *) zone; //方法列表
@end

 

 

@interface ClassName : SuperClass {

//declare instance variables

 

    id<ClassNameDelegate> delegate;

    id<ClassNameDataSource> dataSource;

}

// define properties

//define methods (including any

//custom initializers)

@end

 

Class Implementation (.m)

 

#import "YourClassName.h"

 

@implementation ClassName

 

// synthesize properties

 

// implement methods (including any

//custom initializers, and dealloc)

@end

 

Defining Methods

- (anytype)doIt;

- (anytype)doItWithA:(anytype)a;

- (anytype)doItWithA:(anytype)a andB:(anytype)b;

 

Implementing Methods

 

- (anytype)doItWithA:(anytype)a andB:(anytype)b {

     // Do something with a and b...

      return retVal;

}

Dealloc

- (void)dealloc {

 

  // Release any retained variables...

    [super dealloc];

}

 

Creating a Class Instance

 

ClassName * myClass = [[ClassName alloc] init];

// myClass valid until you call:

[myClass release]; // Don’t need w/ ARC

ClassName * myClass = [[[ClassName alloc] init]autorelease];

// myClass valid until next run loop

 

Calling a Method

[myClass doIt];

[myClass doItWithA:a];

[myClass doItWithA:a andB:b];

 

Defining Properties

@property (attribute1, attribute2) propertyName;

 

retain | Call retain on assign

assign | Normal assign(default)

copy   | Make copy on assign

nonatomic |Make not threadsafe

readwrite | Create getter&setter(default)

readonly | Create just getter

 

Synthesizing Properties

 

@synthesize propertyName;

@synthesize propertyName =_myInstanceVariable;

Using Properties

[myClass setPropertyName:a];

myClass.propertyName = a; // alternative

a = [myClass propertyName];

a = test.propertyName; // alternative

 

Declaring Variables

anytype myVariable

int | 1,2,500,10000

float |   1.5,3.14,578.234

double  |

BOOL |  YES,NO,TRUE, FALSE

ClassName * |  NSString *,NSArray *,etc

id  | Can hold ref to any object

Custom Initializer Example

 

- (id)initWithParam:(anytype)param {

      if ((self = [super init])) {

          self.propertyName = param;

      }

     return self;

}

NSString Quick Examples

NSString *personOne = @"Ray";

NSString *personTwo = @"Shawn";

NSString *combinedString =[NSString stringWithFormat: @"%@: Hello, %@!", personOne, personTwo];

NSLog(@"%@", combinedString);

NSString *tipString = @"24.99";

float tipFloat = [tipString floatValue];

NSArray Quick Examples

NSMutableArray *array = [NSMutableArray arrayWithObjects:personOne, personTwo, nil];

[array addObject:@"Waldo"];

NSLog(@"%d items!", array.count);

for (NSString *person in array) {

  NSLog(@"Person: %@", person);

}

NSString *waldo = [array objectAtIndex:2];

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/jiangshiyong/archive/2012/09/04/2669813.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值