iOS - 如何创建“实用程序”?所有控制器都可以调用的类 [英] iOS - how do I create a "utility" class that all my controllers can call
问题描述
我是iOS和obective-c的新手,所以我不太清楚如何最好地完成这个看似简单的任务。
I am new to iOS and obective-c so I am not too sure how to best accomplish this seemingly simple task.
我想要的是上课在伪代码中看起来像这样:
What I want is to make a class that looks like this in pseudocode:
class UtilityClass
{
// Have a method that I can pass parameters to
String doCalculation ( String art1 , String arg2 )
{
return arg1 + arg2;
}
}
我的不确定性是:
1)xCode似乎倾向于以相对平坦的方式布置我的文件结构。那么我应该创建一个utils目录并将此文件放在utils / fileName下吗?通常我习惯于至少拥有一些src目录,但到目前为止我没有被任何东西提示创建一个。
2)如何从我的控制器导入并调用此类/函数?
1) xCode seems to be inclined to lay out my file structure in a relatively flat way. So should I make a utils directory and have this file be under utils/fileName ? Usually I am kind of used to having at least some src directory, but so far I have not been prompted by anything to create one. 2) How do I import and call this class/function from my controllers?
谢谢,
Alex
Thanks, Alex
推荐答案
只需创建一个名为Utilities的新组,然后在其中创建您的类。比如,
Just create a new group called Utilities, and then create your class inside it. Like,
utils.h
utils.m
稍后在ViewController的头文件中添加。
Later in your ViewController's header file just add.
#import "utils.h"
如果这个utils类被非常胖的项目中的许多控制器使用的话,找到一个名为的文件,应该在支持文件组内。
if this utils class is used by many controllers in very fat project then, find a file called, Should be inside supporting files group.
YourAppName-Prefix.pch
在该文件中,您有一个这样的代码块,
In that file you have a code block like this,
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#endif
只需编辑此块并添加 utils。 h
这里引用,这样整个项目就可以创建utils对象而无需显式导入到自己的头文件中。
Just edit this block and add your utils.h
reference here, In this way your entire project can create utils object without explicitly importing into their own header.
像这样编辑。,
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "utils.h"
#endif
这篇关于iOS - 如何创建“实用程序”?所有控制器都可以调用的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!