I am just learning objective-C and I wonder why the following code leads to an error?
Person *p = [[Person alloc] init]; Person *p = [[Person alloc] init];
In this case, the second line gives an error indicating that the pointer "p" is already defined.
It makes sense to me; however, if it is true that the pointer cannot be redefined, then why does the following code work?
for(int i = 0; i<10; i+=1) { Person *p = [[Person alloc] init]; }
When I inject this into Xcode, it gives no errors, and it compiles and works just fine. In this case, I would expect the loop to simply override the p pointer, similar to how they will be declared sequentially, but this is not the case.
Does anyone have an explanation why this is?
source share