in the section about NSError there is a sample script and a part of it is the following line "NSError *error = nil;"
I don’t really get why would we need here to assign it to nil value? I tried to write it without it, just like NSError *error; and it works the same.
Plus, I tried to do NSLog to see the value of the *error variable after this line before it gets allocated, and it gives me (null) in any case anyway.
So what’s the point of assigning nil to it?
It is not guaranteed that the method into which you pass the address of your &error will actually hand back an NSError pointer. Also, a local variable (such as NSError *error;) has a garbage value until you assign it one. If you try to read the value of the NSError instance but nothing ever assigned it a value, the value is still garbage.
Whenever you are creating a local variable whose value may or may not change, it is always a good idea to give it an initial value.
This way, in the case of our NSError example, in case the method responsible for actually generating the error object doesn’t create one, you’re guaranteed that the error variable at least has a known (nil) value. This way, error is always either nil or a valid NSError pointer and never anything else.
I see, thanks a lot!