使用NSURLConnection的自定义子类,它如何“找到”后面的类中的附加数据? [英] Using custom subclass of NSURLConnection, how does it "find" the additional data in the class later?
问题描述
此博客为处理多个NSURLConnections提供了一个很好的解决方案:创建一个具有额外标记
属性的自定义CustomURLConnection类。
http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/
http:// blog.emmerinc.be/index.php/2009/03/15/multiple-async-nsurlconnections-example/
基本上,他只是添加了一个
CustomURLConnection.m
$ b
$ b - (id)initWithRequest:(NSURLRequest *)请求委托:(id)委托startImmediately:(BOOL)startImmediately标签:
self = [super initWithRequest:request delegate:delegate startImmediately:startImmediately];
if(self){
self.tag = tag;
}
return self;
}
然后,在正常的NSURLConnection加载方法之后, p>
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//记录连接的标记
CustomURLConnection * ttttag =(CustomURLConnection *)connection; // ** HERE **
NSLog(@%@,ttttag.tag);
NSMutableData * dataForConnection = [self dataForConnection:(CustomURLConnection *)connection];
[connection release];
}
所以,这就是我遇到的麻烦。我看到它,这是怎么回事:
- 我创建了一个连接+标签
- 我在上面发布的第一个代码片段创建了一个常规的连接(没有
标签
),这将最终调用正常的NSURLConnection方法,如 connectionDidFinishLoading
。 标签
会发生什么情况?
- 在
connectionDidFinishLoading
我可以将连接转回到连接+标记,然后找到丢弃的标记信息。如何?
也许我只是困惑自己,但看起来好像标签
当它开始正常的 NSURLConnection
路径时被丢弃。但是,通过将它作为子类,我再次能够恢复标签
属性。
有人能更好地理解继承对我有帮助吗?
解决方案使用此代码:
[[CustomURLConnection alloc] initWithRequest :. .. delegate:... startImmediately:... startImmediately tag:...];
您可以创建 CustomURLConnection
的实例。现在这里是你的理解是错误的:这个 CustomURLConnection
对象可以自由地调用其超类的所有方法,但它将始终保持 CustomURLConnection
。 标签
始终存在。
在超类中定义的方法如 initWithRequest:delegate:startImmediately:
不知道标签,但他们也不必知道。当委托方法被调用时:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
连接
参数是非常相同 CustomURLConnection
。方法签名中的类型不同,但并不重要;因为您知道此连接是 CustomURLConnection
类型,您只需将连接
对象转换为正确的类型,访问新属性。但即使你不这样做,标签
仍然会一直存在。
This blog offers a nice solution for handling multiple NSURLConnections: make a custom "CustomURLConnection" class that has an additional tag
property.
http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/
http://blog.emmerinc.be/index.php/2009/03/15/multiple-async-nsurlconnections-example/
Basically, he has simply added a tag
property to the exsisting NSURLConnection:
CustomURLConnection.m
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString*)tag {
self = [super initWithRequest:request delegate:delegate startImmediately:startImmediately];
if (self) {
self.tag = tag;
}
return self;
}
then, later in the normal NSURLConnection loading methods, you can do:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//Log the connection’s tag
CustomURLConnection *ttttag = (CustomURLConnection *)connection; // **HERE**
NSLog(@"%@", ttttag.tag);
NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
[connection release];
}
So, that's where I'm having trouble. The way I see it, this is how things go:
- I create a "connection+tag"
- The first code snippet I posted above creates a regular "connection" (no
tag
), which will eventually call the the normal NSURLConnection methods like connectionDidFinishLoading
. What happens to the tag
at this point?
- In the
connectionDidFinishLoading
method I'm able to cast the connection back into a "connection+tag", then find that missing tag information that had been discarded. How?
Maybe I'm just confusing myself, but it seems as if the tag
was discarded when it starts down the normal NSURLConnection
path. But then by casting it as the subclass, I'm again able to recover the tag
property. Where did it live/go in the mean time?
Could someone with a better understanding of inheritance explain this to me?
解决方案 With this code:
[[CustomURLConnection alloc] initWithRequest:... delegate:... startImmediately:... startImmediately tag:...];
you create an instance of CustomURLConnection
. Now here is where your understanding is wrong: this CustomURLConnection
object can freely call all methods of its superclasses but it will always remain a CustomURLConnection
. The tag
is always there.
The methods that are defined in the superclass such as initWithRequest:delegate:startImmediately:
don't know about the tag but they don't have to, either. When the delegate method gets called:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
the connection
argument is the very same CustomURLConnection
that you created yourself above. The type in the method signature is different but that doesn't matter; because you know that this connection is of the CustomURLConnection
type, you can just cast the connection
object to the correct type and access the new property. But even if you wouldn't do that, the tag
would still be there all the time.
这篇关于使用NSURLConnection的自定义子类,它如何“找到”后面的类中的附加数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!