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
}
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 '[AnyHashable : Any]'
func onAppOpenAttribution(_ attributionData: [AnyHashable : Any]?) {
your attributionData should be optional type, if let data = attributionData
if let case is used to safely unwrap an optional value. But currently you are passing a non optional value to it. So you have two options. Either to make attributionData
as optional, or remove if let statement
Option 1:
func onAppOpenAttribution(_ attributionData: [AnyHashable : Any]?) {
if let data = attributionData {
if let link = data["link"]{
print("link: \(link)")
}
}
}
Option 2:
func onAppOpenAttribution(_ attributionData: [AnyHashable : Any]) {
let data = attributionData
if let link = data["link"]{
print("link: \(link)")
}
}
}
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
Initializer for conditional binding must have Optional type, not 'Bool' - firebase
Use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary variable. In your case, you don't have to use optional binding on a non-optional bool:
let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingUrl, completion: { (dynamicLink, _) in
if let dynamicLink = dynamicLink, let _ = dynamicLink.url {
self.handleIncomingDynamicLink(dynamicLink: dynamicLink)
}
})
if linkHandled {
return linkHandled
}
Or more succinctly, as recommended by @LeoDabus in the comments, you could use Swift's trailing closure syntax when initializing linkHandled
:
let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingUrl) { (dynamicLink, _) in
if let dynamicLink = dynamicLink, let _ = dynamicLink.url {
self.handleIncomingDynamicLink(dynamicLink: dynamicLink)
}
}
Your whole function would look like:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
if let incomingUrl = userActivity.webpageURL {
let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingUrl, completion: { (dynamicLink, _) in
if let dynamicLink = dynamicLink, let _ = dynamicLink.url {
self.handleIncomingDynamicLink(dynamicLink: dynamicLink)
}
})
if linkHandled {
return linkHandled
}
}
return false
}
Related Topics
View with Continuous Scroll; Both Horizontal and Vertical
How to Access My Viewcontroller from My Appdelegate? iOS
Trying to Set Only Time in Uidatepicker in Swift 2.0
Ios: Disable Autorotation for a Subview
Perform Background Tasks When App Is Terminated
What Should Xcode 6 Gitignore File Include
Auto Layout in Uicollectionviewcell Not Working
Get MAC Address of Bluetooth Low Energy Peripheral in iOS
How to Detect Delete Key on an Uitextfield in iOS 8
Didreceiveremotenotification Not Working in the Background
Performance Testing in Swift Using Tdd
Switch Cameras with Avcapturesession
Xcode Canvas for Swiftui Previews Does Not Show Up
Uicollectionview Set Number of Columns
Perform Segue Programmatically and Pass Parameters to the Destination View