Aldee
Aldee

Reputation: 4518

Objective C: Methods with return type of NSString but with variables inside

I have a certain confusion on how to return values in Objective C with NSString return type but having variables inside it.

Is this okey?

- (NSString *)returnInfo{
    return @"Userinfo:\nusername: %@\npassword: %@", self.username, self.password;
}

Your answers are highly appreciated... Thanks...

Upvotes: 0

Views: 3398

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726519

It should probably be

- (NSString *)returnInfo{
    return [NSString stringWithFormat:@"Userinfo:\nusername: %@\npassword: %@", self.username, self.password];
}

Your code would probably compile too, but it would return password, because of the way the comma operator works. This is almost certainly not what you had in mind, though.

Upvotes: 7

Related Questions