I know the title of this question is a bit confusing, but here it goes anyway:
I\'m creating an NSString after an if statement but it just doesn\'t seem
Use:
NSString* pwd = nil;
if ([[password stringValue] isEqualToString:@""]) {
pwd = [[NSString alloc]initWithString:@"password"];
} else {
pwd = [[NSString alloc]initWithFormat:@"%@", [password stringValue]];
}
The problem is that each block introduces a scope. A variable exists only in the scope in which it is defined (a variable exists from the point of declaration to the end of the scope it is declared). Although the memory pointed to by "pwd" will outlast the if...else block, the pointer named pwd will cease to exist after the if...else block in which it is declared. Declaring the pointer before the block moves pwd up one scope.