方法参数中的指针 - Objective-C

发布于 2024-12-02 15:52:55 字数 191 浏览 0 评论 0原文

如何在方法中将指针作为参数传递? 例如:

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

其中 simpleString 是简单参数,pointerToArray 是指向数组的指针;

How to pass pointer as param in method?
for example:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

携君以终年 2024-12-09 15:52:55

在 Objective-C 中,字符串和数组都是类。正如您所看到的,它们已经可以通过指针访问。因此,您只需按照声明所示使用它们即可:

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

并且您可以像这样调用:

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

[yourClass dosomething:s :a];

FWIW,您的方法的名称是dosomething::。通常会表示每个参数,所以我会这样称呼它:

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

那么名称是 doSomethingWithString:array: ,在我看来,它更具可读性。你
调用它:

[yourClass doSomethingWithString:s array:a];

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];
携余温的黄昏 2024-12-09 15:52:55

像这样:(

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

将第二个“*”添加到参数类型中

,然后在您的方法中执行以下操作:

*pointerToArray = [NSMutableArray array];

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];
一页 2024-12-09 15:52:55

例如:

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

For example:

NSString *localSimpleString;
NSMutableArray *localArray;
[self dosomething:localSimpleString :pointerToArray];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文