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
.
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
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
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
}
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()
Initializer for conditional binding must have Optional type, not 'String' not working after clean build
The if let
construct is used for "conditional binding" of an optional. The thing you are trying to assign to name
(the function result) is not optional - instead, the value you are passing to String(cString:,name:)
is optional.
You should rewrite your code to something like this:
if let interfaceName = interface?.ifa_name {
let name = String(cString: interfaceName)
if name == "utun0" {
print("Ipfound")
ipFound = true
} else {
print("name != Ipfound")
}
} else {
print("interface or interface.ifa_name is nil")
}
In that code I'm using optional binding to try to get a non-nil value from interface?.ifa_name
.
If you use !
"force unwrap" operator as you were trying to do, your code would crash if interface
is nil, or if interface.ifa_name is nil.
Edit:
Another way you could handle this, that would look closer to your original code, would be to use map()
on the Optional
:
if let name: String = interface?.ifa_name.map ({ String(cString: $0)}),
name == "utun0" }
{
print("Ipfound")
ipFound = true;
}
The form of map()
that works on an Optional takes an Optional as input. If that optional is nil, it returns nil. If the optional is not nil, the result returned by map()
is the result of applying your closure to the unwrapped optional.
Initializer for conditional binding must have Optional type, not 'AVAudioInputNode' // Swift 4.2
Doing the guard let node = audioEngine.inputNode ...
is trying to unwrap an optional value. However, audioEngine.inputNode
does not return an optional value.
If you just do let node = audioEngine.inputNode
(without the guard let return
), it will work.
Related Topics
How to Develop For Iphone Using a Windows Development Machine
Presenting Modal in iOS 13 Fullscreen
How to Force Nslocalizedstring to Use a Specific Language
Determine Device (Iphone, Ipod Touch) With Ios
How to Debug 'Unrecognized Selector Sent to Instance' Error
Undefined Symbols For Architecture Armv7
The Simplest Way to Resize an Uiimage
Send and Receive Messages Through Nsnotificationcenter in Objective-C
How to Add Constraints Programmatically Using Swift
Attempt to Present Uiviewcontroller on Uiviewcontroller Whose View Is Not in the Window Hierarchy
Detecting Which Uibutton Was Pressed in a Uitableview
How to Create Delegates in Objective-C
How to Develop or Migrate Apps For Iphone 5 Screen Resolution
How to Check How Long Ago Was the App Last Opened
Swift - Json Error: the Data Couldn'T Be Read Because It Isn'T in the Correct Format