2

If want to both assign a string and check that its not empty in Swift.

if let alternative3Text = attributes.stringForKey("choiceThree") && alternative3Text != "" {
   // do stuff with alternative3Text
}

Is this possible in Swift, or do i have to do a nested if-statement?

1
  • No, it's not possible, alternative3Text is only visible inside if body {} Commented Oct 19, 2014 at 18:30

2 Answers 2

8

Update: As of Swift 3 (Xcode 8), additional clauses are separated by a comma, not by where:

if let alternative3Text = attributes.string(forKey: "choiceThree"),
        alternative3Text != "" {
    // do stuff with alternative3Text
}

Update: As of Swift 1.2 (Xcode 6.3 beta), you can combine optional binding with additional conditions:

if let alternative3Text = attributes.stringForKey("choiceThree") where alternative3Text != "" {
   // do stuff with alternative3Text
}

Using switch-case still works but is not necessary anymore for this purpose.


Old answer: It is not possible with an if statement, but with switch. A switch case can use a where clause to check for additional conditions (documentation).

Assuming (from your question) that attributes.stringForKey("choiceThree") returns String?, the following would work:

switch (attributes.stringForKey("choiceThree")) {
case .Some(let alternative3Text) where alternative3Text != "":
    // alternative3Text is the unwrapped String here
default:
    break
}
Sign up to request clarification or add additional context in comments.

3 Comments

cool, didn't know about the switch where clause -- ... but that would he would have a if let AND a switch so the sample doesn't help him AFAIK!?
@Daij-Djan: I am not sure if I understand your question. The case .Some(let alternative3Text) does the optional binding (via pattern matching), this replaces the if let.
This is a really handy use of pattern matching in general, but it's worth noting that, for a single case, simply nesting an if inside an if let might be more concise. (Of course, concise is in the eye of the beholder.)
0

No, you can't require additional expressions to be true in an if let statement. You will need to add additional code to do this in either the form of a nested if statement as you've already mentioned, or in some other way. If your only requirement is to keep this statement looking clean and wouldn't mind moving some of the logic elsewhere, you could always make an extension to what ever type your attributes variable is to add this functionality.

Here's an example if attributes was an instance of NSUserDefaults. (just because it already contains a stringForKey() instance method.)

extension NSUserDefaults {
    func nonEmptyStringForKey(key: String) -> String? {
        let full = self.stringForKey(key)

        return full != "" ? full : nil
    }
}

And then use it like this

if let alternative3Text = attributes.nonEmptyStringForKey("choiceThree") {
    // stuff
}

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.