Store pointer inside NSNumber

269 Views Asked by At

I implementing custom subclass of the NSURLProtocol. I need to store my NSURLProtocol instance inside NSMutableURLRequest. Since [NSURLProtocol setProperty:forKey:inRequest:] raises warning Invalid protocol-property list if you try to store non-plist-serializable object, I do it like this:

- (void)startLoading {
    ...    
    // when I need to store an NSURLProtocol subclass
    [NSURLProtocol setProperty:[NSNumber numberWithLongLong:(long long)self] forKey:@"WebProxyURLProtocol" inRequest:mutableRequest];
    ...
}

and

// 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;
}

This works quite well. But is it safe and correct way to solve my problem, or I sometimes can get already deallocated object? Thanks!

0

There are 0 best solutions below