【发布时间】:2014-05-03 20:14:02
【问题描述】:
我开始研究 iOS 的网络编程。我正在关注教程并停止,因为我收到“变量从未使用”警告。我发现另一个教程以不同的方式处理它,但它也会产生警告。然后,我决定查看 Apple 的示例代码,结果发现他们也采用了另一种方式!
教程1: http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
教程 2: http://www.cimgf.com/2010/02/12/accessing-the-cloud-from-cocoa-touch/
[[NSURLConnection alloc]
initWithRequest:request
delegate:self];
// Declare property in .m file so it doesn't show as public
@property (nonatomic, strong, readwrite) NSURLConnection * connection;
// Then just assign to it when the connection is created
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
Apple 的方法消除了警告,但分配给一个永远不会再次读取的变量似乎有点浪费。这是最佳做法吗?
【问题讨论】:
-
如果您只是针对 iOS 7,请改用
NSURLSession,或者直接使用AFNetworking。即使您不只是针对 iOS 7,我仍然建议使用AFNetworking,它消除了对大量样板代码的需求。 -
我需要支持 iOS 6,否则我会像你建议的那样使用
NSURLSession。不幸的是,我不允许在这个项目中使用任何第三方库。我很想使用AFNetworking!谢谢
标签: ios objective-c nsurlconnection