• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

iOS - 创建“实用程序”所有控制器都可以调用的类

用户头像
it1352
帮助1

问题说明

我是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

正确答案

#1

只需创建一个名为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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhccjeik
系列文章
更多 icon
同类精品
更多 icon
继续加载