开发者

Pointers in method params - objective-c

How to pass pointer as param in method?开发者_StackOverflow社区 for example:

-(void) dosomething:(NSString *) simpleString :(NSMutableArray *) pointerToArray;

where simpleString is simple param, and pointerToArray is pointer to an array;


In Objective-C, strings and arrays are both classes. As you can see, they are already accessed through pointers. So you simply use them as the declaration says:

-(void) dosomething:(NSString *) simpleString :(NSMutableArray *) pointerToArray;

And you invoke like:

NSString *s = @"Hello, world";
NSMutableArray *a = [NSMutableArray arrayWithObjects: @"Hello", @"silly", @"example", nil];

[yourClass dosomething:s :a];

FWIW, the name of your method is dosomething::. It is customary to denote each parameter, so I would call it:

-(void) doSomethingWithString:(NSString *)greeting array:(NSMutableArray *)strings;

then the name is doSomethingWithString:array: which is much more readable, IMO. You invoke it with:

[yourClass doSomethingWithString:s array:a];


Like this:

-(void) dosomething:(NSString *) simpleString :(NSMutableArray **) pointerToArray;

(Add a second '*' to the parameter type

In your method, you then do something like:

*pointerToArray = [NSMutableArray array];


For example:

NSString *localSimpleString;
NSMutableArray *localArray;
[self dosomething:localSimpleString :pointerToArray];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜