NSError object already populated on first call to method

I am following a book developed by J. Lee using Test-Driven iOS and stumbled upon this unit test, which I don't understand. First of all, if you need more code, please let me know immediately.

-(void)testDelegateNotifiedOfErrorWhenNewsBuilderFails
{
    MockNewsBuilder *builder = [MockNewsBuilder new];
    builder.arrayToReturn = nil;
    builder.errorToSet = underlyingError;
    newsManager.newsBuilder = builder;
    [newsManager receivedNewsJSON:@"Fake Json"];
    ...
}

-(void)receivedNewsJSON:(NSString *)objectNotation
{
    NSError *error = nil;
    //  As you see error is nil and I am passing in a nil error.
    NSArray *news = [_newsBuilder newsFromJSON:objectNotation error:&error];
    ...
}

@implementation MockNewsBuilder

-(NSArray *)newsFromJSON:(NSString *)objectNotation error:(NSError **)error
{
    // But once I arrive here, error is no longer nil.
    // (NSError **) error = 0x00007fff5cb887f0 domain: @"Fake Json" - code: 0
    ...
}

How is an error automatically set?

UPDATE:

Thanks to everyone for the active discussion and advice. The answers explain how the receiving side receives an instance of the error due to &, I understand that. My question remains, but why does the called side point to a populated NSError instance, although it should be zero. I did not install the error instance in newsFromJSON:error:, since it is already populated there?

I just changed [newsManager receivedNewsJSON:@"Fake Json1"];and the instance of the error appeared newsFromJSON:error:immediately (NSError **) error = 0x00007fff5b9b27f0 domain: @"Fake Json1" - code: 0. Its very confusing ...

+4
3

. & ; -(NSArray *)newsFromJSON:(NSString *)objectNotation error:(NSError **)error;

, .

. .

enter image description here

:

, , . newsFromJSON, (&error). .
newsFromJSON.

: newsFromJSON, (* operator) **error = something;

(NSError *error), .
C CPP Objective-C, , * - .

& obj → obj * obj → .

+3

error - NSError*, " NSError" ( objective-C , , , ++).

, error - () , NSError, nil.

, , (autoreleased) NSError. , &error, , , " NSError" ( , ).

, C Objective-C : error, (nil) , , , error () . &error.

, "" error (, ), NSError.

?

: , Cocoa: , / "/" . false (NO, 0 ..), , (, ) NSError.a >

EDITED:. @Droppy, , , ( ), , error - nil, -. , "" , , / . , , @"Fake JSON", , , ( ).

0

** - . , . Objective-C C. , C . . - NS- . outParameter . C . - , C. Objective-C ++ .

​​ Apple. Cocoa BOOL NSError. BOOL - , NSError. Apple framework NSError.

Sometimes they don’t use BOOL and instead return an object or zero.

Core Foundation C structures work very accurately and often use input and output options.

0
source

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


All Articles