Guard Let Error: Initializer for Conditional Binding Must Have Optional Type Not 'String'

Conditional Binding: if let error – Initializer for conditional binding must have Optional type

if let/if var optional binding only works when the result of the right side of the expression is an optional. If the result of the right side is not an optional, you can not use this optional binding. The point of this optional binding is to check for nil and only use the variable if it's non-nil.

In your case, the tableView parameter is declared as the non-optional type UITableView. It is guaranteed to never be nil. So optional binding here is unnecessary.

func tableView(tableView: UITableView, commitEditingStyle editingStyle:UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
myData.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

All we have to do is get rid of the if let and change any occurrences of tv within it to just tableView.

guard let error: Initializer for conditional binding must have Optional type not 'String'

You should be very carefull with optionals. Using ! you tell to Swift compiler that you can guarantee that the value is exists.
Try to do it like this:

@IBAction func signUpButtonPressed(sender: UIButton) {
guard let email = emailTextField.text where email.characters.count > 0 else {
// alert
return
}

guard let password = passwordTextField.text where password.characters.count > 0 else {
// alert
return
}

self.registerUserAsync(email, password: password)

Swift also introduces optional types, which handle the absence of a value. Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”.

More about optionals you can find here https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309

Initializer for conditional binding must have Optional type, not '[String]' guard let when shuffled

I think you meant to use

guard let sound = sounds.first

which picks the first element out of the shuffled array (which can be nil if the array is empty).

Alternatively you can remove the shuffle and just use

guard let sound = ["x", "y", "z"].randomElement()

How to fix Initializer for conditional binding must have Optional type, not 'String'?

Replace this

if let newProfileImageUrl = personsArray[indexPath.row].profileImageUrl {

with

let newProfileImageUrl = personsArray[indexPath.row].profileImageUrl 

Also it's better to use SDWebImage

cell.imageAvatar.sd_setImage(with: URL(string:newProfileImageUrl), placeholderImage: UIImage(named: "placeholder.png"))

instead of repeatedly fetching the image every scroll

Error Initializer for conditional binding must have Optional type, not 'String' trying to sort column in Swift 5, NSTableView

You're not showing the type of p1 or p2, but on at least one (probably both if they're the same type), the property artist is defined as a String. In order to use guard let or if let, the variable/property you're binding to must be an Optional (String?, for example).

If these are both non-optional Strings, there's no reason to do the guard statement, since there's nothing to bind -- you can just do your comparisons directly on p1.artist and p2.artist.

More reading on optional bindings: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html



Related Topics



Leave a reply



Submit