Why does NSError need double indirection? (pointer to a pointer)

前端 未结 5 1433
遇见更好的自我
遇见更好的自我 2020-11-30 18:53

This concept seems to trouble me. Why does an NSError object need its pointer passed to a method that is modifying the object? For instance, wouldn\'t just passing a referen

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 19:32

    An old question, but still I think its worth putting this here -

    The actual culprit is NSError. If you look at its class reference, there are no setter methods for any of its attributes, i.e. domain, code or userInfo. So there is no way, you can just alloc and initialize a NSError, pass it to the method and then populate information on the passed NSError object. (Had there been a setter method, we could have just passed a NSError * and done something like error.code = 1 in the method.)

    So in case there is an error, you have to generate a new NSError object in the method and if you are doing so the only way to pass it back to the caller is by having a NSError ** argument. (For the reason explained in the above answers.)

提交回复
热议问题