ios 具有多个输入参数的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6589916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:38:02  来源:igfitidea点击:

Method with multiple input parameters

objective-ciosmethods

提问by C.Johns

I understand how to create my own methods that accept input parameters in objective-c but I have never actually created a method with more than one input parameter!

我了解如何在objective-c 中创建我自己的接受输入参数的方法,但我实际上从未创建过具有多个输入参数的方法!

From methods I have used with multiple input parameters each has a name along the lines of

从我与多个输入参数一起使用的方法中,每个方法都有一个名称

first:second:third:

第一第二第三:

and look like

看起来像

- (void)first:(NSString *)fname second:(NSString *)mname third:(NSString *)lname;

- (void)first:(NSString *)fname second:(NSString *)mname third:(NSString *)lname;

my question is when creating your own method with multiple input parameters do you have to create a name like first:second:third or can you just have something like C++ where you have the one name followed by a list of input parameter types followed by the parameter names... if I remember correctly.

我的问题是,当使用多个输入参数创建自己的方法时,您是否必须创建一个名称,如 first:second:third 或者您是否可以使用 C++ 之类的东西,其中您有一个名称,后跟输入参数类型列表,然后是参数名称...如果我没记错的话。

fullName:(NSString, NSString, NSString) fname, mname, lname;

回答by PengOne

No. A method must have the format as you described:

否。方法必须具有您描述的格式:

- (void)first:(NSString *)fname second:(NSString *)mname third:(NSString *)lname;

回答by Paul.s

You have to have the parameters interleaved with the method signature. It's ok because xcode has code completion and it can give you nice descriptive names about what your method is doing and what it requires.

您必须将参数与方法签名交错。没关系,因为 xcode 具有代码完成功能,它可以为您提供关于您的方法正在做什么和它需要什么的很好的描述性名称。

e.g.

例如

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

In the example above without even looking at the API for UIViewController you can get a pretty good understanding of how this method works and what it's params are. It is good practice to name your methods well to describe what they do (it can remove the need for most commenting if done well).

在上面的示例中,甚至无需查看 UIViewController 的 API,您就可以很好地了解此方法的工作原理以及它的参数是什么。很好的做法是为您的方法命名以描述它们的作用(如果做得好,它可以消除大多数评论的需要)。

You may well of course see a method written like this

你当然可能会看到这样写的方法

- (void)myMethodThatAcceptsARectangle:(float)x :(float)y :(float)w :(float)h;

But this will not be very clear in use as to what the parameters relate to:

但这在使用中不会很清楚这些参数与什么相关:

[self myMethodThatAcceptsARectangle:1.0f :1.0f :1.0f :1.0f];

So you should avoid this (I added it incase you ever see this and wonder what's happening).

所以你应该避免这种情况(我添加了它,以防你看到这个并想知道发生了什么)。

回答by Caleb

fullName:(NSString, NSString, NSString) fname, mname, lname;

Yes, you can do something like that. It'd look like this instead:

是的,你可以做这样的事情。它看起来像这样:

-(void)fullName:(NSString*)fname :(NSString*)mname :(NSString*)lname

and you'd call it like this:

你会这样称呼它:

[foo fullName:first :middle :last];

That largely defeats the point of Objective-C's method names, though, and the main reason to do something like that is to register your dislike of the normal Objective-C convention, or perhaps to get yourself kicked off whatever project you're working on.

然而,这在很大程度上违背了 Objective-C 方法名称的意义,这样做的主要原因是表明你不喜欢普通的 Objective-C 约定,或者让你自己开始从事你正在从事的任何项目.

回答by Iree

Another option could be variadic parameters. They're used to provide a variable amount of parameters, even though you wouldn't have a name on each one of them. e.g.

另一种选择可能是可变参数。它们用于提供可变数量的参数,即使您不会在每个参数上都有一个名称。例如

[NSString stringWithFormat:@"My name is %@ %@", @"John", @"Doe"];

It would be something like this:

它会是这样的:

- (void)names:(NSString *)names, ...;

Implementation, additional info

实施附加信息

回答by Balaji Ramakrishnan

Here's a simple example for Method with parameters.

这是带有参数的方法的简单示例。

- (void)methodName:(NSString *)parameterOne methodNameContinues:(NSString *)parameterTwo;

For Example,

例如,

-(void)showAlertMsg:(NSString *)message withTitle:(NSString *)title;

Here you can see, we've a prefix "withTitle" for the second parameter. We've to proceed the same for the other parameters too.

在这里你可以看到,我们有一个前缀“ withTitle”作为第二个参数。我们也必须对其他参数进行相同的处理。

回答by Mixstah

I can think of a perfectly good reason to use NSDictionary to pass arguments. I also believe it answers the question.

我能想到一个很好的理由来使用 NSDictionary 来传递参数。我也相信它回答了这个问题。

You can place all of the items in an NSDictionary then unpack them. This maybe useful if you have say a persitanceStore NSObject that you want to send a list of parameters to.

您可以将所有项目放在 NSDictionary 中,然后解压缩它们。如果您说要向其发送参数列表的 persistanceStore NSObject,这可能很有用。