Cast ID for pointer to pointer NSError (NSError **)

I have an NSError ** stored in an array (so I can get it as such array[0] ). I am trying to use it in a variable:

NSError * __autoreleasing *errorPointer = (NSError * __autoreleasing *)array[0];

so I can access the base object as *errorPointer .

However, Xcode complains that Cast of an Objective-C pointer to 'NSError *__autoreleasing *' is disallowed with ARC . Is there any way to get to this object without disabling ARC?

+4
source share
1 answer

Neither the stub:withBlock: method nor any of its supporting infrastructure can simply NSArray double pointer to an NSArray . An array does not accept objects, and a pointer to an object is not an object. Something else is happening there.

This obviously requires some digging in the code to determine. Where does the value fall into the array? This is at -[KWStub processInvocation:] , and this was apparently done using the method added to NSInvocation from OCMock, getArgumentAtIndexAsObject: In this method, when invoked, a switch is used to check the type of the requested argument and, if necessary, puts it in a square.

The relevant case here is the latter, where the argument type is ^ , which means "pointer". This argument terminates in NSValue ; therefore, the array received by your block does not actually contain the double pointer itself, but an NSValue representing an external pointer. You just need to remove it.

It should look like this:

 NSValue * errVal = array[1]; NSError * __autoreleasing * errPtr = (NSError * __autoreleasing *)[errVal pointerValue]; 
+8
source

Source: https://habr.com/ru/post/1481974/


All Articles