我实现了NSURLProtocol
的自定义子类。我需要将NSURLProtocol
实例存储在NSMutableURLRequest
中。如果您尝试存储非plist-serializable对象,[NSURLProtocol setProperty:forKey:inRequest:]
会引发警告Invalid protocol-property list
,我会这样做:
- (void)startLoading {
...
// when I need to store an NSURLProtocol subclass
[NSURLProtocol setProperty:[NSNumber numberWithLongLong:(long long)self] forKey:@"WebProxyURLProtocol" inRequest:mutableRequest];
...
}
和
// when I need to get an NSURLProtocol subclass back in NSURLSessionDelegate
- (NSURLProtocol *)protocolForTask:(NSURLSessionTask *)task {
NSNumber *number = [NSURLProtocol propertyForKey:@"WebProxyURLProtocol" inRequest:task.originalRequest];
return (__bridge NSURLProtocol *)(void *)number.longLongValue;
}
这很有效。但这是解决我的问题的安全和正确的方法,或者我有时可以获得已经解除分配的对象?谢谢!