首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

IOS4 note 三

2012-07-19 
IOS4 note 3Pointer Parameters and the Address Operator?NSString *s @Hello, world!?s is actuall

IOS4 note 3

Pointer Parameters and the Address Operator

?

NSString *s = @"Hello, world!";

?

s is actually an NSString* --- a pointer toan NSString

?

Sometimes, however, a function expects as aparameter a pointer to something, but

what you’ve got is not a pointer but thething itself. Thus, you need a way to create a

pointer?to? that? thing. The?solution? is? the?address operator? (K&R 5.1),which? is?an

ampersand before the name of the thing.

For example, there’s an NSString method forreading from a file into an NSString, which

is declared like this:

?

+ (id)stringWithContentsOfFile:(NSString*)path

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??encoding:(NSStringEncoding)enc

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?error:(NSError**)error

Now, never mind what an id is, and don’tworry about the Objective-C method dec-

laration?syntax.? Just? consider?the? types? of?the? parameters.? The?first? one? is? an

NSString*; that’s no problem, as everyreference to an NSString is actually a pointer to

an NSString. An NSStringEncoding turns outto be merely an alias to a primitive data

type, an NSUInteger, so that’s no problemeither. But what on earth is an NSError**?

By all logic, it looks like an NSError**should be a pointer to a pointer to an NSError.

And that’s exactly what it is. This methodis asking to be passed a pointer to a pointer

to an NSError. Well, it’s easy to declare apointer to an NSError:

NSError* myError;

But how can we obtain a pointer to that?With the address operator! So our code might

look, schematically, like this:

NSString* myPath = // something or other;

NSStringEncoding myEnc = // something orother;

NSError* myError = nil;

NSString* result = [NSStringstringWithContentsOfFile: myPath

????????????????????????????????????????????encoding: myEnc

?????????????????????????????????????? ?????????error: &myError];

The?important? thing? to notice?is? the ampersand. Because myError? is a pointer?to an

NSError, &myError? is a pointer?to a pointer? to an NSError,which? is?just what we’re

expected to provide. Thus, everything goesswimmingly.

This device lets Cocoa effectively returntwo results from this method call. It returns a

real result, which we have captured byassigning it to the NSString pointer we’re calling

result. But if there’s an error, it alsowants to set the value of another object, an NSError object; the idea is thatyou can then study that NSError object to find out what went wrong. (Perhapsthe file wasn’t where you said it was, or it wasn’t stored in the encoding youclaimed it was.) By passing a pointer to a pointer to an NSError, you give themethod free rein to do that. Before the call to stringWithContentsOfFile:,myError was uninitialized; during the call to stringWithContentsOfFile:, Cocoacan,? if?it? likes, repoint the pointer,thus giving myError an actual value.

So the idea is that you first check resultto see whether it’s nil. If it isn’t, fine; it’s the string you asked for. Ifit is, you then study the NSError that myError is now pointing to, to learnwhat went wrong. This pattern is frequently used in Cocoa.

?

?

?

You can use the address operator to createa pointer to any named variable. A C function

is technically a kind of named variable, soyou can even create a pointer to a function!

This is an example of when you’d use thename of the function without the parentheses:

you aren’t calling the function, you’retalking about it. For example, &square is a pointer to the square function.

热点排行