0

我正在制作一个基于服务器的应用程序。每次我向数据库发出请求时,我都必须输入所有服务器连接代码。是否有可能以某种方式重用它?在 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]; 
4

5 回答 5

2

希望您将所有连接类与其余代码隔离开来,那么为什么您不能只创建一个方法来创建您的请求设置您的变量并返回请求以让您启动异步......即使您不保留你的连接东西被隔离了你仍然可以有一些类的静态方法有这个方法......

于 2012-06-01T15:52:04.870 回答
2

你可以实现你的自定义 ASIHTTPRequest 类:

@interface YourRequest : ASIFormDataRequest
@end


@implementation YourRequest  

- (id)initWithURL:(NSURL *)newURL {
  self = [super initWithURL: newURL];

  if (self) {
      [self addRequestHeader:@"Content-Type" value:@"text/html; charset=utf-8;"];
      [self setDefaultResponseEncoding:NSUTF8StringEncoding]; 
      [self setTimeOutSeconds: 10.0f]; 
      [self setCachePolicy: ASIDoNotWriteToCacheCachePolicy | ASIDoNotReadFromCacheCachePolicy]; 
    }
  }

  return self;
}
@end

并创建对象:

 - (void)doSomething { 
     __block YourRequest *request = [YourRequest requestWithURL: url]; 
     __weak ASIHTTPRequest *request_b = request;
     [request setDelegate : self];
     //Set variables 

     [request startAsynchronous];

 }
于 2012-06-01T15:53:34.930 回答
1

如果您需要在同一个实现文件中多次重用该设置代码,请考虑使用Extract Method重构以创建返回正确配置的请求对象的实用程序方法。

如果您需要在许多地方执行此类操作,请考虑子类化ASIFormDataRequest,以便您可以更简洁地创建请求对象,并按照您最常设置的属性配置它们。或者,您可以使用静态方法创建某种请求工厂类来生成请求对象。

于 2012-06-01T15:52:26.707 回答
1

您可以在头文件中声明方法,这些方法在使用您的类时可用。所以你可以在你的.h文件中声明doSomthing,然后在你的.m文件中实现该方法,当你想“doSomthing”时,[className doSomthing] 如果你想显示更多代码,我可能会给你一个更好的例子

于 2012-06-01T15:52:45.900 回答
1

您是否尝试过使用宏?

在您的 .h 文件中:

#define LoadServerCode() \
__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];

然后,在您的实现中:

-(void)doSomething {
    LoadServerCode();

    //Set variables

    [request startAsynchronous];
}
于 2012-06-01T21:05:59.790 回答