简体   繁体   English

如何访问NSURLConnection代理属性

[英]How to access NSURLConnection Delegate property

Why is it not possible to do the following: 为什么无法执行以下操作:

id connectionDelegate = myConnection.delegate;
//myConnection is an instance of NSURLConnection

Why is this not possible, and how do I access the delegate otherwise. 为什么这不可能,否则我将如何访问委托。 Seems rather awkward to me, or am I missing something here? 对我来说似乎很尴尬,还是我在这里想念什么?

NSURLConnection is pretty awkward. NSURLConnection非常尴尬。 Why are you still using it? 您为什么仍在使用它? Unless you're trying to maintain backwards compatibility with iOS 6 or OS X v10.8 and earlier, just use NSURLSession. 除非您尝试保持与iOS 6或OS X v10.8及更早版本的向后兼容性,否则请使用NSURLSession。

With that said, if you must keep using that API for backwards compatibility, you can always do what I've done in these situations: 话虽如此,如果您必须继续使用该API来实现向后兼容性,那么在这些情况下,您始终可以做我所做的事情:

  • Create category methods called delegate and setDelegate. 创建称为委托和setDelegate的类别方法。
  • Use Objective-C associated objects to store and retrieve the value on the connection object. 使用与Objective-C相关的对象来存储和检索连接对象上的值。
  • If you aren't in control of the code that creates the NSURLConnection objects, swizzle the NSURLConnection class's init methods to call your setter. 如果您不受创建NSURLConnection对象的代码的控制,请整理NSURLConnection类的init方法以调用您的setter。 Otherwise, just call the setter manually right after you create the NSURLConnection object. 否则,只需在创建NSURLConnection对象之后立即手动调用setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM