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.

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

Initializer for conditional binding must have Optional type, not 'String'

The compiler is telling you that you can't use an if let because it's totally unnecessary. You don't have any optionals to unwrap: URL is not optional, and the absoluteString property isn't optional either. if let is used exclusively to unwrap optionals. If you want to create a new constant named url, just do it:

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
let url = URL.absoluteString
if #available(iOS 8.0, *) {
VPMainViewController.showCompanyMessageWebView(url)
}
return false
}

However, sidenote: having a parameter named URL and a local constant named url is mighty confusing. You might be better off like this:

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
if #available(iOS 8.0, *) {
VPMainViewController.showCompanyMessageWebView(URL.absoluteString)
}
return false
}

Initializer for conditional binding must have Optional type, not 'T.ID'

The error is pretty clear, it indicates that the guarded object is non-optional.

Indeed the Identifiable protocol requires a non-optional id property so replace

guard let id = identifiableObject.id else { throw MyError.encodingError }

with

let id = identifiableObject.id


Related Topics



Leave a reply



Submit