iPhone 程式設計入門(6) Object-C 摘要

x

x

以下是对 Object-C 的摘要:

 

(1) 1979 C with Class, 1984 C++, 1984 objc,然后经过 24 年的演化至今。

 

(2) C++ Fast but Static

  • static function dispatch
  • compile time decision
  • little runtime flexibility

(3) Object-C Dynamic to the Core

  • dynamic message dispatch
  • runtime decision making
  • runtime class extensions

(4) A comparative example of calling a method

  • C++: someObject->doSomething();
  • Java: someObject.doSomething();
  • Object-C: [someObject doSomething];

(5) Object-C Messaging

  • [Employee newEmployee];
  • [anEmployee setRaise:200];
  • [anEmployee setWork:work withDeadline:today];

(6) Object-C Selector (很像 VBA 的 IDispatch Interface)

  • [Employee newEmployee];  --> "newEmployee"
  • [anEmployee setRaise:200]; --> "setRaise:"

(7) Method Dispatch,Object-C 只能继承单一 class,每一种 class 都是 NSObject 的 child class

 

(8) Method Dispatch -- What if no one response to the message?

  • Runtime traverses class hierarchy again
  • Each object is given an opportunity to forward the message
  • Remote messaging works this way
  • Throws exception if no one handles the message

(9) How a Object-C class looks like

 

@interface Person : NSObject {

NSString *name;

int age;

NSMutableArray *parent;

}

 

+ (Person*) personWithName: (NSString *)name;    /* class only message */

- (id) initWithName: (NSString *) name;

 

@property(readwrite) int age;

@property(readwrite) (NSArray*) parents;

 

@end

 

(10) How to implement a class

 

#import "Person.h"  /* case sensitive */

 

@implementation Person

 

@synthesize age, parents;

 

/* initializing your instance */

 

- (id) init {

    if (self == [super init]) {

        name = @"New Person";

        parents = [[NSArray alloc] init];

    }

    return self;

}

 

/* cleaning up after ourselves */

 

- (void) dealloc {

    [name release];

    [parents release];

    [super dealloc];

}

 

- (NSString *) name {

  return name;

}

 

- (void) setName: (NSString*) newName {

    if (newName != name) {

          [newName retain];   /* same as COM interface addref() */

          [name release];         /* same as COM interface release() */

          name = newName;

    }

}

 

/* custom behavior of property */

- (void) setParents: (NSArray*) newParents {

    if (parents != newParents) {

        [parents release];

        parents = [newParents mutableCopy];

    }

}

 

@end

 

(11) Reference Counting

 

MJFoo *myFoo = [[MJFoo alloc] init];    /* reference count = 1 */

              [myFoo retain];                           /* reference count = 2 */

              [myFoo release];                         /* reference count = 1 */

              [myFoo release];                         /* reference count = 0, and memory is reclaimed  by system */

 

 

(12) Autorelease Pool -- a delayed release

 

  • uses the -autorelease message
  • sends -release to an object at some point in the future
  • often used by class convenience methods

 

Example:

      myString = [[NSString alloc] initWithString: @"foo"];

      myString = [NSString stringWithString: @"foo"];           /* autorelease string */

 

(13) Setter/ Getter Pattern

 

  • declare similar setters and getters each time
  • implement similar access pattern
  • more glue code usually means more bugs

(14) Properties -- Encapsulation Mad Easy

  • Quickly and easily create setter/getter pairs
  • Specify storage semantics
  • Easy to override for custom behavior

Example:

 

      @property(readwrite) float speed;

      @property(copy) NSArray *passengers;

      @property(nonatomic, copy) NSArray *passengers;

      @property(getter=isGoingFast) BOOL goingFast

 

Property attributes:

 

      readonly/readwrite

      assign/retain/copy

      nonatomic

      setter= / getter=

 

(15) Object-C Categories -- Mixing your own behavior

 

  • Seamlessly add methods to existing classes at runtime
  • Subclass to add storage or modify existing behavior

@implementation NSString (myAdditions)

 

- (NSString *) rot13 {

    ....

}

 

@end

 

(16) Object-C Protocols -- Multiple inheritance via Specifications

  • A list of method declarations
  • Classes conform to a protocol by implement this list
  • Identical to interface in Java

@protocol MyProtocol

 

- (void) foo: (int) x;

- (float) bar;

 

@end

 

 

Example:

 

#import "MyProtocol.h"

 

@interface Foo : NSObject

 

.....

 

@end

 

 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值