SWIFT: If the Let statement is condition, you cannot use the array filter.

Suppose I have a set of usernames

let users = ["Hello", "1212", "12", "Bob", "Rob"]

I want to get the first user whose name length is 2, so I filtered the array and got the first user

if let selected = users.filter{$0.characters.count == 2}.first {
print(selected)
}

This code is thrown under swift 2.2 A compilation error occurred

Continuous statements on one line must be separated by’;’

However, this works fine

let selected = users.filter{$0.characters.count == 2}.first
if let selected = selected {
print(selected)
}

Anyone can Explain why I need to store the filtered results in a separate variable first? Any help will be greatly appreciated.

You can add Use parentheses to complete this work:

if let selected = users.filter({$0.characters.count == 2}).first {
print(selected)
}

This is the right way. Sometimes on lines with extra elements, the trailing closure syntax does not work well. You can also surround the entire statement Add parentheses:

if let selected = (users.filter {$0.characters.count == 2}.first) {
print(selected)
}

Swift is having trouble parsing your statement. Parentheses help in how to parse the line. You should prefer the first way because the closure is indeed the filter parameter , So enclosing it in parentheses will make Swift make it clear that you pass it to the filter.

Suppose I have a set of usernames

< p>

let users = ["Hello", "1212", "12", "Bob", "Rob"]

I want to get the first name with a length of 2 Users, so I filtered the array and got the first user

if let selected = users.filter{$0.characters.count == 2}.first {
print(selected)
}

This code throws a compilation error under Swift 2.2

Continuous statements on one line must be separated by’;’

But, this works fine

let selected = users.filter{$0.characters.count == 2}.first
if let selected = selected {
print(selected)
}

Anyone can solve Explain why I need to store the filtered results in a separate variable first? Any help would be greatly appreciated.

You can do this by putting parentheses around the closure you pass to the filter:

< /p>

if let selected = users.filter({$0.characters.count == 2}).first {
print(selected)
}

This is the correct way. Sometimes the trailing closure syntax does not work well on lines with extra elements. You can also put parentheses around the entire statement:

< /p>

if let selected = (users.filter {$0.characters.count == 2}.first) {
print(selected)
}

Swift is in I'm having trouble parsing your statement. The parentheses give help in how to parse the line. You should prefer the first way because the closure is indeed a parameter of the filter, so enclosing it in parentheses makes Swift clear Means you pass it to the filter.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4141 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.