問題描述
我是 iOS 和 obctive-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 似乎傾向于以相對扁平的方式布置我的文件結構.那么我應該創(chuàng)建一個 utils 目錄并將這個文件放在 utils/fileName 下嗎?通常我習慣了至少有一些 src 目錄,但到目前為止,我還沒有被任何東西提示創(chuàng)建一個.2) 如何從我的控制器導入和調(diào)用這個類/函數(shù)?
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?
謝謝,亞歷克斯
推薦答案
只需創(chuàng)建一個名為 Utilities 的新組,然后在其中創(chuàng)建您的類.喜歡,
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 類被很多控制器在非常胖的項目中使用,那么,找到一個名為的文件,應該在支持文件組內(nèi).
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
引用,這樣您的整個項目就可以創(chuàng)建 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 - 如何創(chuàng)建“實用程序"?我的所有控制器都可以調(diào)用的類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!