Objective-C开发指南--3

 
    • 动态识别(Dynamic types)
      • 这里有一些用于 Objective-C 动态识别的 methods(说明部分采中英并列,因为我觉得英文比较传神,中文怎么译都怪):

-(BOOL) isKindOfClass: classObj

is object a descendent or member of classObj
此对象是否是 classObj 的子孙或一员

-(BOOL) isMemberOfClass: classObj

is object a member of classObj
此对象是否是 classObj 的一员

-(BOOL) respondsToSelector: selector

does the object have a method named specifiec by the selector
此对象是否有叫做 selector 的 method

+(BOOL) instancesRespondToSelector: selector

does an object created by this class have the ability to respond to the specified selector
此对象是否是由有能力响应指定 selector 的对象所产生

-(id) performSelector: selector

invoke the specified selector on the object
唤起此对象的指定 selector

      • 所有继承自 NSObject 都有一个可回传一个 class 物件的 class method。这非常近似于 Java 的 getClass() method。这个 class 对象被使用于前述的 methods 中。
      • Selectors 在 Objective-C 用以表示讯息。下一个范例会秀出建立 selector 的语法。
      • 基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。
      • main.m

§                             #import "Square.h"

§                             #import "Rectangle.h"

§                             #import <stdio.h>

§                              

§                             int main( int argc, const char *argv[] ) {

§                                 Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20];

§                                 Square *sq = [[Square alloc] initWithSize: 15];

§                              

§                                 // isMemberOfClass

§                              

§                                 // true

§                                 if ( [sq isMemberOfClass: [Square class]] == YES ) {

§                                     printf( "square is a member of square class\n" );

§                                 }

§                              

§                                 // false

§                                 if ( [sq isMemberOfClass: [Rectangle class]] == YES ) {

§                                     printf( "square is a member of rectangle class\n" );

§                                 }

§                              

§                                 // false

§                                 if ( [sq isMemberOfClass: [NSObject class]] == YES ) {

§                                     printf( "square is a member of object class\n" );

§                                 }

§                              

§                                 // isKindOfClass

§                                 

§                                 // true

§                                 if ( [sq isKindOfClass: [Square class]] == YES ) {

§                                     printf( "square is a kind of square class\n" );

§                                 }

§                              

§                                 // true

§                                 if ( [sq isKindOfClass: [Rectangle class]] == YES ) {

§                                     printf( "square is a kind of rectangle class\n" );

§                                 }

§                              

§                                 // true

§                                 if ( [sq isKindOfClass: [NSObject class]] == YES ) {

§                                     printf( "square is a kind of object class\n" );

§                                 }

§                              

§                                 // respondsToSelector

§                              

§                                 // true

§                                 if ( [sq respondsToSelector: @selector( setSize: )] == YES ) {

§                                     printf( "square responds to setSize: method\n" );

§                                 }

§                              

§                                 // false

§                                 if ( [sq respondsToSelector: @selector( nonExistant )] == YES ) {

§                                     printf( "square responds to nonExistant method\n" );

§                                 }

§                              

§                                 // true

§                                 if ( [Square respondsToSelector: @selector( alloc )] == YES ) {

§                                     printf( "square class responds to alloc method\n" );

§                                 }

§                              

§                                 // instancesRespondToSelector

§                              

§                                 // false

§                                 if ( [Rectangle instancesRespondToSelector: @selector( setSize: )] == YES ) {

§                                     printf( "rectangle instance responds to setSize: method\n" );

§                                 }

§                              

§                                 // true

§                                 if ( [Square instancesRespondToSelector: @selector( setSize: )] == YES ) {

§                                     printf( "square instance responds to setSize: method\n" );

§                                 }

§                              

§                                 // free memory

§                                 [rec release];

§                                 [sq release];

§                                 

§                                 return 0;

}

      • output

§                             square is a member of square class

§                             square is a kind of square class

§                             square is a kind of rectangle class

§                             square is a kind of object class

§                             square responds to setSize: method

§                             square class responds to alloc method

square instance responds to setSize: method

    • Categories
      • 当你想要为某个 class 新增 methods,你通常会扩充(extend,即继承)它。然而这不一定是个完美解法,特别是你想要重写一个 class 的某个功能,但你却没有原始码时。Categories 允许你在现有的 class 加入新功能,但不需要扩充它。Ruby 语言也有类似的功能。
      • 基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。
      • FractionMath.h

§                             #import "Fraction.h"

§                              

§                             @interface Fraction (Math)

§                             -(Fraction*) add: (Fraction*) f;

§                             -(Fraction*) mul: (Fraction*) f;

§                             -(Fraction*) div: (Fraction*) f;

§                             -(Fraction*) sub: (Fraction*) f;

@end

      • FractionMath.m

§                             #import "FractionMath.h"

§                              

§                             @implementation Fraction (Math)

§                             -(Fraction*) add: (Fraction*) f {

§                                 return [[Fraction alloc] initWithNumerator: numerator * [f denominator] +

§                                                                             denominator * [f numerator]

§                                                          denominator: denominator * [f denominator]];

§                             }

§                              

§                             -(Fraction*) mul: (Fraction*) f {

§                                 return [[Fraction alloc] initWithNumerator: numerator * [f numerator]

§                                                          denominator: denominator * [f denominator]];

§                              

§                             }

§                              

§                             -(Fraction*) div: (Fraction*) f {

§                                 return [[Fraction alloc] initWithNumerator: numerator * [f denominator]

§                                                          denominator: denominator * [f numerator]];

§                             }

§                              

§                             -(Fraction*) sub: (Fraction*) f {

§                                 return [[Fraction alloc] initWithNumerator: numerator * [f denominator] -

§                                                                             denominator * [f numerator]

§                                                          denominator: denominator * [f denominator]];

§                             }

@end

      • main.m

§                             #import <stdio.h>

§                             #import "Fraction.h"

§                             #import "FractionMath.h"

§                              

§                             int main( int argc, const char *argv[] ) {

§                                 // create a new instance

§                                 Fraction *frac1 = [[Fraction alloc] initWithNumerator: 1 denominator: 3];

§                                 Fraction *frac2 = [[Fraction alloc] initWithNumerator: 2 denominator: 5];

§                                 Fraction *frac3 = [frac1 mul: frac2];

§                              

§                                 // print it

§                                 [frac1 print];

§                                 printf( " * " );

§                                 [frac2 print];

§                                 printf( " = " );

§                                 [frac3 print];

§                                 printf( "\n" );

§                              

§                                 // free memory

§                                 [frac1 release];

§                                 [frac2 release];

§                                 [frac3 release];

§                              

§                                 return 0;

}

      • output

1/3 * 2/5 = 2/15

      • 重点是 @implementation 跟 @interface 这两行:@interface Fraction (Math) 以及 @implementation Fraction (Math).
      • (同一个 class)只能有一个同名的 category,其它的 categories 得加上不同的、独一无二的名字。
      • Categories 在建立 private methods 时十分有用。因为 Objective-C 并没有像 Java 这种 private/protected/public methods 的概念,所以必须要使用 categories 来达成这种功能。作法是把 private method 从你的 class header (.h) 档案移到 implementation (.m) 档案。以下是此种作法一个简短的范例。
      • MyClass.h

§                             #import <Foundation/NSObject.h>

§                              

§                             @interface MyClass: NSObject

§                             -(void) publicMethod;

@end

      • MyClass.m

§                             #import "MyClass.h"

§                             #import <stdio.h>

§                              

§                             @implementation MyClass

§                             -(void) publicMethod {

§                                 printf( "public method\n" );

§                             }

§                             @end

§                              

§                             // private methods

§                             @interface MyClass (Private)

§                             -(void) privateMethod;

§                             @end

§                              

§                             @implementation MyClass (Private)

§                             -(void) privateMethod {

§                                 printf( "private method\n" );

§                             }

@end

      • main.m

§                             #import "MyClass.h"

§                              

§                             int main( int argc, const char *argv[] ) {

§                                 MyClass *obj = [[MyClass alloc] init];

§                              

§                                 // this compiles

§                                 [obj publicMethod];

§                              

§                                 // this throws errors when compiling

§                                 //[obj privateMethod];

§                              

§                                 // free memory

§                                 [obj release];

§                                 

§                                 return 0;

}

      • output

public method

    • Posing
      • Posing 有点像 categories,但是不太一样。它允许你扩充一个 class,并且全面性地的扮演(pose)这个 super class。例如:你有一个扩充 NSArray 的 NSArrayChild 物件。如果你让 NSArrayChild 扮演 NSArray,则在你的程序代码中所有的 NSArray 都会自动被替代为 NSArrayChild。
      • 基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。
      • FractionB.h

§                             #import "Fraction.h"

§                              

§                             @interface FractionB: Fraction

§                             -(void) print;

§                             @end

      • FractionB.m

§                             #import "FractionB.h"

§                             #import <stdio.h>

§                              

§                             @implementation FractionB

§                             -(void) print {

§                                 printf( "(%i/%i)", numerator, denominator );

§                             }

@end

      • main.m

§                             #import <stdio.h>

§                             #import "Fraction.h"

§                             #import "FractionB.h"

§                              

§                             int main( int argc, const char *argv[] ) {

§                                 Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];

§                              

§                                 // print it

§                                 printf( "The fraction is: " );

§                                 [frac print];

§                                 printf( "\n" );

§                              

§                                 // make FractionB pose as Fraction

§                                 [FractionB poseAsClass: [Fraction class]];

§                              

§                                 Fraction *frac2 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];

§                              

§                                 // print it

§                                 printf( "The fraction is: " );

§                                 [frac2 print];

§                                 printf( "\n" );

§                              

§                                 // free memory

§                                 [frac release];

§                                 [frac2 release];

§                              

§                                 return 0;

}

      • output

§                             The fraction is: 3/10

The fraction is: (3/10)

      • 这个程序的输出中,第一个 fraction 会输出 3/10,而第二个会输出 (3/10)。这是 FractionB 中实作的方式。
      • poseAsClass 这个 method 是 NSObject 的一部份,它允许 subclass 扮演 superclass。
    • Protocols
      • Objective-C 里的 Protocol 与 Java 的 interface 或是 C++ 的 purely virtual class 相同。
      • 基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。
      • Printing.h

§                             @protocol Printing

§                             -(void) print;

@end

      • Fraction.h

§                             #import <Foundation/NSObject.h>

§                             #import "Printing.h"

§                              

§                             @interface Fraction: NSObject <Printing, NSCopying> {

§                                 int numerator;

§                                 int denominator;

§                             }

§                              

§                             -(Fraction*) initWithNumerator: (int) n denominator: (int) d;

§                             -(void) setNumerator: (int) d;

§                             -(void) setDenominator: (int) d;

§                             -(void) setNumerator: (int) n andDenominator: (int) d;

§                             -(int) numerator;

§                             -(int) denominator;

@end

      • Fraction.m

§                             #import "Fraction.h"

§                             #import <stdio.h>

§                              

§                             @implementation Fraction

§                             -(Fraction*) initWithNumerator: (int) n denominator: (int) d {

§                                 self = [super init];

§                              

§                                 if ( self ) {

§                                     [self setNumerator: n andDenominator: d];

§                                 }

§                              

§                                 return self;

§                             }

§                              

§                             -(void) print {

§                                 printf( "%i/%i", numerator, denominator );

§                             }

§                              

§                             -(void) setNumerator: (int) n {

§                                 numerator = n;

§                             }

§                              

§                             -(void) setDenominator: (int) d {

§                                 denominator = d;

§                             }

§                              

§                             -(void) setNumerator: (int) n andDenominator: (int) d {

§                                 numerator = n;

§                                 denominator = d;

§                             }

§                              

§                             -(int) denominator {

§                                 return denominator;

§                             }

§                              

§                             -(int) numerator {

§                                 return numerator;

§                             }

§                              

§                             -(Fraction*) copyWithZone: (NSZone*) zone {

§                                 return [[Fraction allocWithZone: zone] initWithNumerator: numerator

§                                                                        denominator: denominator];

§                             }

@end

      • Complex.h

§                             #import <Foundation/NSObject.h>

§                             #import "Printing.h"

§                              

§                             @interface Complex: NSObject <Printing> {

§                                 double real;

§                                 double imaginary;

§                             }

§                              

§                             -(Complex*) initWithReal: (double) r andImaginary: (double) i;

§                             -(void) setReal: (double) r;

§                             -(void) setImaginary: (double) i;

§                             -(void) setReal: (double) r andImaginary: (double) i;

§                             -(double) real;

§                             -(double) imaginary;

@end

      • Complex.m

§                             #import "Complex.h"

§                             #import <stdio.h>

§                              

§                             @implementation Complex

§                             -(Complex*) initWithReal: (double) r andImaginary: (double) i {

§                                 self = [super init];

§                              

§                                 if ( self ) {

§                                     [self setReal: r andImaginary: i];

§                                 }

§                              

§                                 return self;

§                             }

§                              

§                             -(void) setReal: (double) r {

§                                 real = r;

§                             }

§                              

§                             -(void) setImaginary: (double) i {

§                                 imaginary = i;

§                             }

§                              

§                             -(void) setReal: (double) r andImaginary: (double) i {

§                                 real = r;

§                                 imaginary = i;

§                             }

§                              

§                             -(double) real {

§                                 return real;

§                             }

§                              

§                             -(double) imaginary {

§                                 return imaginary;

§                             }

§                              

§                             -(void) print {

§                                 printf( "%_f + %_fi", real, imaginary );

§                             }

@end

      • main.m

§                             #import <stdio.h>

§                             #import "Fraction.h"

§                             #import "Complex.h"

§                              

§                             int main( int argc, const char *argv[] ) {

§                                 // create a new instance

§                                 Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];

§                                 Complex *comp = [[Complex alloc] initWithReal: 5 andImaginary: 15];

§                                 id <Printing> printable;

§                                 id <NSCopying, Printing> copyPrintable;

§                              

§                                 // print it

§                                 printable = frac;

§                                 printf( "The fraction is: " );

§                                 [printable print];

§                                 printf( "\n" );

§                              

§                                 // print complex

§                                 printable = comp;

§                                 printf( "The complex number is: " );

§                                 [printable print];

§                                 printf( "\n" );

§                              

§                                 // this compiles because Fraction comforms to both Printing and NSCopyable

§                                 copyPrintable = frac;

§                              

§                                 // this doesn't compile because Complex only conforms to Printing

§                                 //copyPrintable = comp;

§                              

§                                 // test conformance

§                              

§                                 // true

§                                 if ( [frac conformsToProtocol: @protocol( NSCopying )] == YES ) {

§                                     printf( "Fraction conforms to NSCopying\n" );

§                                 }

§                              

§                                 // false

§                                 if ( [comp conformsToProtocol: @protocol( NSCopying )] == YES ) {

§                                     printf( "Complex conforms to NSCopying\n" );

§                                 }

§                              

§                                 // free memory

§                                 [frac release];

§                                 [comp release];

§                              

§                                 return 0;

}

      • output

§                             The fraction is: 3/10

§                             The complex number is: 5.000000 + 15.000000i

Fraction conforms to NSCopying

      • protocol 的宣告十分简单,基本上就是 @protocol ProtocolName (methods you must implement) @end。
      • 要遵从(conform)某个 protocol,将要遵从的 protocols 放在 <> 里面,并以逗点分隔。如:@interface SomeClass <Protocol1, Protocol2, Protocol3>
      • protocol 要求实作的 methods 不需要放在 header 档里面的 methods 列表中。如你所见,Complex.h 档案里没有 -(void) print 的宣告,却还是要实作它,因为它(Complex class)遵从了这个 protocol。
      • Objective-C 的接口系统有一个独一无二的观念是如何指定一个型别。比起 C++ 或 Java 的指定方式,如:Printing *someVar = ( Printing * ) frac; 你可以使用 id 型别加上 protocol:id <Printing> var = frac;。这让你可以动态地指定一个要求多个 protocol 的型别,却从头到尾只用了一个变数。如:<Printing, NSCopying> var = frac;
      • 就像使用@selector 来测试对象的继承关系,你可以使用 @protocol 来测试对象是否遵从接口。如果对象遵从这个接口,[object conformsToProtocol: @protocol( SomeProtocol )] 会回传一个 YES 型态的 BOOL 对象。同样地,对 class 而言也能如法炮制 [SomeClass conformsToProtocol: @protocol( SomeProtocol )]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值