Objective-C "if" statements are not saved

I know that the title of this question is a bit confusing, but here anyway:

I create an NSString after the statement if, but it just doesn’t want to save outside the statement. My condition is as follows:

if ([[password stringValue] isEqualToString:@""]) {
    NSString *pwd = [[NSString alloc]initWithString:@"password"];
}
else {
    NSString *pwd = [[NSString alloc]initWithFormat:@"%@", [password stringValue]];
}

... and I call pwdlater in the script as follows:

NSArray *arguments;
arguments = [NSArray arrayWithObjects: ip, pwd, nil];
[task setArguments: arguments];

But when I do this, the first fragment returns a warning Unused variable 'pwd', and the last call ends in an error 'pwd' undeclared.

What am I doing wrong here? Thanks in advance.;)

+3
source share
3 answers

Using:

NSString * pwd = nil;
if ([[password stringValue] isEqualToString:@""]) {
    pwd = [[NSString alloc]initWithString:@"password"];
} else {
    pwd = [[NSString alloc]initWithFormat:@"%@", [password stringValue]];
}

, . , ( , ). , "pwd", if... else, pwd if... else, . , pwd .

+19

, : , - ! , :

NSString *pwd;
if ([[password stringValue] isEqualToString:@""]) {
    pwd = [[NSString alloc]initWithString:@"password"];
}
else {
    pwd = [[NSString alloc]initWithFormat:@"%@", [password stringValue]];
}
+10

You declared it pwdas a local variable inside if bodies. The variable you refer to later is probably declared outside and is never set by any assignment. Just remove NSString *from tasks.

0
source

Source: https://habr.com/ru/post/1742634/


All Articles