Why passing &error instead of error in Cocoa programming?

前端 未结 4 2065
刺人心
刺人心 2020-12-08 01:45

In Cocoa programming, when dealing with NSError, why we are passing &error to a message instead of error?

NSError *error;
if (![managedObject.managedObje         


        
4条回答
  •  失恋的感觉
    2020-12-08 01:50

    When you call the method, you aren't giving it a pointer to an NSError, you're giving it a place to pass a pointer to an NSError back to you.

    I often write

    NSError *error = nil;
    

    which makes it a bit more obvious that there nothing (interesting) in error when you call -save:. It's just a place you've set aside for the address in memory of an NSError object. If the method encounters an error, it creates an NSError object and writes the address of the object in error to pass it back to you.

提交回复
热议问题