简体   繁体   English

带参数的iPhone SDK函数/方法

[英]iPhone SDK function/method with arguments

I'm creating an app in which I do the same thing multiple times. 我正在创建一个应用,其中我多次执行相同的操作。 I'm going for as little code as possible and I don't want to repeat myself. 我要尽可能减少代码,并且我不想重复自己。

I need the PHP equivalant of a reusable function, where I set an input, and the function will return the output. 我需要一个可重用函数的PHP等效项,在其中设置输入,然后该函数将返回输出。

The app is a URL shortener, so the input as you may assume is a URL, and the output is the short URL. 该应用程序是一个URL缩短器,因此您可能假设的输入是URL,而输出是短URL。

What's the best way to go about this? 最好的方法是什么? I know I can code the same thing twice, but I hate to repeat myself. 我知道我可以对同一件事进行两次编码,但是我不想重复自己。

Hopefully I'm getting my point across. 希望我能阐明我的意思。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 -Giles -Giles

Apologies if I'm oversimplifying your question, but are you asking how to define an Objective-C method? 抱歉,如果我过于简化您的问题,但是您是否在问如何定义Objective-C方法? If so, you're going to have to learn Objective-C, there's no way around it: 如果是这样,您将必须学习Objective-C,没有办法解决:

The Objective-C Programming Language - Object Messaging Objective-C编程语言 - 对象消息传递

You can do a lot of great things with no code on the iPhone and Mac platforms, but it's hard to imagine completing any useful application without writing some code. 你可以做很多伟大的事情与iPhone和Mac平台的代码,但很难想象如果没有编写一些代码完成任何有用的应用程序。

Example

- (float)multiplyThisFloatByTwoPointFive:(float)numberToMultiply
{
     return numberToMultiply * 2.5;
}

To call it: 调用它:

[self multiplyThisFloatByTwoPointFive:3.7];

Libraries 图书馆

If you mean "I want to put these non-class-specific methods somewhere I can access them universally", eg, a library, you can do this in two ways: 如果您的意思是“我想将这些非特定于类的方法放在可以普遍访问的地方”(例如,一个库),则可以通过以下两种方式进行:

  1. Create some sort of "library" class like "MySpecialMathClass" (not to be confused with "my special education math class") or "MyAppAnimationTricks" and put your methods there (you'd define them as +someMethod: not -someMethod:, class- not instance-method, per Objective-C). 创建某种“库”类,例如“ MySpecialMathClass”(不要与“我的特殊教育数学类”相混淆)或“ MyAppAnimationTricks”,然后将您的方法放在那里(将它们定义为+ someMethod:not -someMethod :,按照Objective-C的类而不是实例方法)。
  2. If they only ever deal with primitives (such as int, float, double ...), consider just making them C functions, rather than Objective-C methods. 如果它们只处理基元(例如int,float,double ...),请考虑使它们成为C函数,而不是Objective-C方法。

I am pretty much a newbie at this, but you can certainly write functions (as opposed to methods) in Objective C. 我几乎是个新手,但是您当然可以在Objective C中编写函数(相对于方法)。

I wrote several utility functions recently. 我最近写了几个实用程序函数。 I created an NSObject file to place them in, but it has no ivars or methods declared, just the functions. 我创建了一个NSObject文件来放置它们,但是它没有声明任何ivars或方法,只有函数。 Then implement the functions in the .m file, and import the .h file into any class file where you want the functions. 然后,在.m文件中实现这些功能,并将.h文件导入到您需要这些功能的任何类文件中。 You can obviously call these from anywhere in any .m file that has imported the function file. 您显然可以从导入了功能文件的任何.m文件中的任何位置调用它们。

John Doner 约翰·多纳

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM