我正在制作一个基于服务器的应用程序。每次我向数据库发出请求时,我都必须输入所有服务器连接代码。是否有可能以某种方式重用它?在 php 中,您通常有一个名为 dbConnect.php(或类似的文件)的文件,您可以在每次想要连接时调用该文件。
例如,我想替换它,我一直使用它:
- (void)doSomething
{
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: url];
__weak ASIHTTPRequest *request_b = request;
[request setDelegate: self];
[request addRequestHeader:@"Content-Type" value:@"text/html; charset=utf-8;"];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
[request setTimeOutSeconds: 10.0f];
[request setCachePolicy: ASIDoNotWriteToCacheCachePolicy | ASIDoNotReadFromCacheCachePolicy];
//Set the variables here
[request startAsynchronous];
}
...类似于:
- (void)doSomething
{
LoadServerCode; //This loads all the server code as above
//Set variables
[request startAsynchronous];
}
提前致谢
编辑:
澄清一点。假设我有一些我经常使用的方法,例如以特殊方式创建 UILabel 或 UIView ... 不必子类化并最终得到一堆类,而是有一个类称为MyConstructionMethods 什么的……所以如果我想在应用程序的一些不同的地方创建一个标签,我可以输入:
MyGreenLabel; //Done, the label is created and added to the view
... 代替:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
label.backgroundColor = [UIColor greenColor];
[self.view addSubview: label];