xCode / 在类中声明静态方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3953720/
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-09-14 19:50:47  来源:igfitidea点击:

xCode / declaring Static Methods in class

iphoneobjective-cxcodeipadstatic-methods

提问by Arcadian

Anyone know how I can declare a static method for an xcode class and then use it in my project?

任何人都知道如何为 xcode 类声明一个静态方法,然后在我的项目中使用它?

I want to create a Common.h class and then do something like this in one of .m file

我想创建一个 Common.h 类,然后在 .m 文件之一中做这样的事情

Common.MyStaticMethod();

Common.MyStaticMethod();

I dont want to have to instantiate and instance of Common

我不想实例化和实例化 Common

回答by taskinoor

You will declare a class level method in Objective-C by using "+" before the method declaration.

您将在 Objective-C 中通过在方法声明前使用“+”来声明类级方法。

// in Common.h
+ (void)myStaticMethod;

// call is from anywhere
[Common myStaticMethod]

That is, a "-" before method declaration means instance methods and "+" means class level method which are not related with any particular instance of the class.

也就是说,方法声明前的“-”表示实例方法,“+”表示与类的任何特定实例无关的类级方法。