1

I want to define a dynamic variable for my alert box. I'm getting a json from my server, like ok, error, username_in_use, etc etc.

let response = parseJSON["message"] as? String

if response == "username_in_use" {
    let error_msg = "Username in already use!"
} else if response == "email_in_use" {
    let error_msg = "Email address in already use!"
} else {
    let error_msg = "Unknown Error!"
}

alertView.showTitle(
    alertTitle: error_msg
)

But i'm getting this message:

Use of unresolved identifier 'error_msg'

How can I set a dynamic value for my alert title?

Thanks for help and sorry my poor english.

3
  • let error_msg: String above the if conditions and remove the let from inside the conditions brackets Commented Dec 11, 2016 at 17:36
  • Before the if ...: let error_msg = "" in if ...: error_msg = .... Commented Dec 11, 2016 at 17:37
  • 1
    Again, no need to declare it as variable as suggested in both answers. Commented Dec 11, 2016 at 17:45

4 Answers 4

3

This is the perfect use case for a switch statement:

let errorMsg: String

switch response {
case "username_in_use": errorMsg = "Username in already use!"
case "email_in_use": errorMsg = "Email address in already use!"
default: errorMsg = "Unknown Error!"
}
Sign up to request clarification or add additional context in comments.

2 Comments

You can also use let instead of var
@LeoDabus Oh interesting, nice!
1

In your code, the scope of error_msg is limited to the blocks within the if statement. You could declare error_msg outside of the if blocks scope, e.g.

let response = parseJSON["message"] as? String
var error_msg:String

if response == "username_in_use" {
    error_msg = "Username in already use!"
} else if response == "email_in_use" {
    error_msg = "Email address in already use!"
} else {
    error_msg = "Unknown Error!"
}

alertView.showTitle(
    alertTitle: error_msg
)

1 Comment

This would be more appropriate as a Switch statement.
1

If you only want to use the possibly existing (Any?) value corresponding to key "message" in the parseJSON dictionary for direct further processing/translation, you needn't necessarily store this value, but can use it inline to proceed with processing it to a valid error message. E.g., using switch statement:

var error_msg: String = "Unknown error"
switch parseJSON["message"] {
    case let val as String where val == "username_in_use": error_msg = "Username in already use!"
    case let val as String where val == "email_in_use": error_msg = "Email address in already use!"
    case _: ()
}

// ...
alertView.showTitle(
    alertTitle: error_msg
)

Or, make use of a helper dictionary to map the JSON "message" codes (values) to error messages, readily allowing use of the nil coalescing operator to supply a the default (unknown error) message:

let error_msgs = [
    "username_in_use" : "Username in already use!",
    "email_in_use"    : "Email address in already use!"]

let error_msg = error_msgs[parseJSON["message"] as? String ?? ""] ?? "Unknown Error!"

// ...
alertView.showTitle(
    alertTitle: error_msg
)

Comments

0

You should declare the let error_msg outside the if statement so that it is visible when you pass it to the alert.

   let response = parseJSON["message"] as? String

   var error_msg = ""

   if response == "username_in_use" {
       error_msg = "Username in already use!"
   } else if response == "email_in_use" {
       error_msg = "Email address in already use!"
   } else {
       error_msg = "Unknown Error!"
   }

   alertView.showTitle(alertTitle: error_msg)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.