Weston
Weston

Reputation: 1491

Issue with NSString and concatenation

Why this code gives me the following errors?

"use of undeclared identifier baseURL"

and

"Unexpected Interface name NSString, expected expression"

here is the entire block of code

switch (type) {
    case 1:
        NSString *baseURL = [NSString stringWithString:@"http://www.myserver.net/somephp/"];
        NSString *finalURL = [baseURL stringByAppendingString:@"?i="];
        break;
    case 2:
        NSString *finalURL = [baseURL stringByAppendingString:@"?n="];
        break;
    default:
        break;
}

Upvotes: 0

Views: 908

Answers (1)

albertamg
albertamg

Reputation: 28572

Sounds like those lines are within a switch statement. If this is the case, move the declaration of the strings outside the switch statement.

NSString *baseURL;
NSString *finalURL;
switch (<expression>) {
    case <constant>:
        baseURL = [NSString stringWithString:@"http://www.myserver.net/somephp"];
        finalURL = [baseURL stringByAppendingString:@"?i="];
        break;
    default:
        break;
}

More information and other techniques to work around this on this question.

Upvotes: 2

Related Questions