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 String
s, 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 guard
ed 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
How to Use Sf Rounded Font in Swiftui
Why Unsaferawpointer Shows Different Result When Function Signatures Differs in Swift
Fibonacci Calculator Stack Overflows at 93Nd Number in Swift
"Generic Parameter Could Not Be Inferred" in Swiftui Uiviewrepresentable
How to Test Whether Generic Variable Is of Type Anyobject
Extending Collection with a Recursive Property/Method That Depends on the Element Type
Swiftui - Wait Until Firestore Getdocuments() Is Finished Before Moving On
Self' Captured by a Closure Before All Members Were Initialized
Can an Enum Contain Another Enum Values in Swift
Swiftyjson - Call Can Throw, But It Is Marked with 'Try' and the Error Is Not Handled
How to Handle Closure Recursivity
Count Elements of Array Matching Condition in Swift
Distinction Between Private and Fileprivate Top-Level Classes
Differencebetween Swift 2.0 Do-Try-Catch and Regular Java/C#/C++ Exceptions
Retrieve String Value from Function with Closure in Swift
Writing Data to an Nsoutputstream in Swift 3
Anonymous Closure Argument Not Contained in a Closure
Child View Controller to Rotate While Parent View Controller Does Not