The compiler translates
self.currentRegion = region;
into
[self setCurrentRegion:region];
which means that you have an infinite recursion in your setter method
- (void)setCurrentRegion:(CLRegion *)region
{
self.currentRegion = region;
}
And the same problem occurs in your getter method:
- (CLRegion *)currentRegion
{
return self.currentRegion;
}
The program then crashes because of a stack overflow.
But the underlying problem is that you cannot add an instance variable to a class in a category method. The only solution for this problem that I know if is to use so-called "associated objects" to hold the contents of the property. See Objective-C Category and new iVar for an example.
Update: There are problems that are special to the NSURLSession
class.
This seems to be a class cluster, but quite unusually the concrete object returned
by the factory method is not a subclass of NSURLSession
:
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
BOOL b = [session isKindOfClass:[NSURLSession class]];
// --> NO !!!
This is the reason for the exception message
-[__NSCFURLSession setCurrentRegion:]: unrecognized selector sent to instance ...
One workaround would be to add the property in a category to NSObject
.
But as it turned out in the discussion, the property was meant to hold a
reference to some object (the region
) so that it would be available in the
completion block of the dataTaskWithURL:completionHandler:
method.
But that is not necessary (if I understand the problem correctly), because creating a block captures the values of variables that are referenced inside the block:
CLRegion *region = ...;
[[session dataTaskWithURL:[NSURL URLWithString:[rUrl setDeviceTarget:[[gObject getSwitchID] stringValue] setServiceID:serviceName setAction:actionName setActionName:actionValue setActionValue:performAction]]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error)
{
// ...
NSLog(@"%@", [region identifier]);
// ...