gpt4 book ai didi

iphone - objective-c : Do I have to retype all the code everytime?

转载 作者:行者123 更新时间:2023-11-28 18:13:31 24 4
gpt4 key购买 nike

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

最佳答案

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

关于iphone - objective-c : Do I have to retype all the code everytime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10853441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com