If Let Error - Initializer for Conditional Binding Must Have Optional Type, Not '[Nsobject:Anyobject]'

If Let Error - Initializer for conditional binding must have Optional type, not '[NSObject : AnyObject]'

error.userInfo is not an Optional, it's of type [NSObject : AnyObject] as hinted by the compiler. No need to unwrap it with if let, it will never be nil.

You can replace

if let info = error.userInfo {
ProgressHUD.showError(info["error"] as! String)
}

with

ProgressHUD.showError(error.userInfo["error"] as! String)

if you're sure the value will be a String.

Otherwise, the dictionary value should be safely unwrapped and downcast as a String. Example:

if let errorString = error.userInfo["error"] as? String {
ProgressHUD.showError(errorString)
}

Initialiser for conditional binding must have Optional type, not '[NSObject: AnyObject]' SwiftyJSON

Conditional binding (if let) expects an expression where the result can be nil. That error means that error.userInfo is never nil.

You just have to delete the if in the first line, like this:

let userInfo = error.userInfo

The rest of your code should be fine. You also need to delete the closing bracket } of that if as well.

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

Please ⌥-click on the arguments in the optional binding expression, you will see that absoluteString is declared as non-optional. A non-optional cannot be conditional bound as the error message states.

Bind only url and add absoluteString in values:

storageRef.downloadURL { (url, error) in
guard let url = url else { return }
let values = ["name": name, "email": email, "profileImageUrl": url.absoluteString]
self.registerUserIntoDatabaseWithUID(uid: uid, values: values as [String: AnyObject])
}

Initializer for conditional binding must have Optional type, not 'AnyObject - Approach

No need to unwrap the result from try. It is not an optional. You do need to cast the result from try to an NSDictionary. Use as? to downcast it.

Best practice: full access to returned error for good error handling

func parseData2(){
var data:NSData?

if let data2 = data {
do {
let details = try NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)

if let detailsDict = details as? NSDictionary {
print("Parse Data")
} else if let detailsArray = details as? NSArray {
print("array")
}

} catch {
print("Error \(error)")
}
}
}

Quick and dirty: error handling is not for me!

func parseData2(){
var data:NSData?

if let data2 = data {

let details = try? NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)

if let detailsDict = details as? NSDictionary {
print("Parse Data")
} else {
print("details might be nil, or not an NSDictionary")
}
}
}

Bad Ass Mode: crashes are features

func parseData2(){
var data:NSData?

if let data2 = data {

let details = try! NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments) as! NSDictionary

}
}

Some extra info on multiple unwraps :
Drop the code below in a playground.

struct SomeStruct {
var anOptional : Int?
init() {
}
}

func unwrapWithIfLet() {

if let unWrappedStruct = myStruct, let unWrappedSomething = unWrappedStruct.anOptional {
print("multiple optional bindings succeeded")
// both unWrappedStruct and unWrappedSomething are available here
} else {
print("something is nil")
}
}

func unwrapWithGuard() {

guard let unWrappedStruct = myStruct, let unWrappedSomething = unWrappedStruct.anOptional else {
print("something is nil")
return
}
print("multiple optional bindings succeeded")
// both unWrappedStruct and unWrappedSomething are available here
}

var myStruct : SomeStruct?

//unwrapWithGuard()
//unwrapWithIfLet()

myStruct = SomeStruct()
myStruct!.anOptional = 1

unwrapWithGuard()
unwrapWithIfLet()

Initializer for conditional binding must have Optional type, not 'NSNumber' in Swift

If if let MId succeeds then MId is non-optional (by the way please name variables with starting lowercase letter) so the second if let conditional binding is redundant and causes the error

if let MId = (M as AnyObject).gid {
let id = MId
fetchvalue(id, limit: limit, delegate: delegate)
}

or even

if let id = (M as AnyObject).gid {
fetchvalue(id, limit: limit, delegate: delegate)
}

And don't use ugly ... as AnyObject).foo syntax use concrete static Swift types.

Cannot assign a value of type '[AnyObject]' to a value of type '[String]' Swift 2

If you are sure that the array is of type [String] you can make a forced cast:

PhotoController.mediaTypes = mediaTypes as! [String]

Otherwise I would suggest to use flatMap and an individual optional cast of each object:

PhotoController.mediaTypes = mediaTypes.flatMap{ $0 as? String }

Binding must have optional type Swift

You report the error:

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

This is because you're calling CLPlacemark(placemark: CLPlacemark), but that's not returning an optional. So it's complaining that you're using if let syntax, which is only valid with optionals.

But you really don't want to do that forced unwrapping of placemarks, and I'd suggest you eliminate that ! altogether. Furthermore, given that in Swift 2, the placemarks is now a [CLPlacemark]?, and you can eliminate the cast altogether, thereby simplifying that if statement:

if let placemark = placemarks?.first {
let subThoroughfare = placemark.subThoroughfare ?? ""
let thoroughfare = placemark.thoroughfare ?? ""

title = "\(subThoroughfare) \(thoroughfare)"
}

Note, you can simplify the thoroughfare and subThoroughfare implementations, too, as shown above.

Bound value in a conditional binding must be of Optional Type

You can get it to compile by making the cast as Usable? instead of as Usable, like this:

// Check whether or not a class is useable
if let usableThing = thing as Usable? { // error here
usableThing.use()
}
else {
println("can't use that")
}

Argument type error on MKPlacemark

Replace this string :

  let title: String?

Replace this code :

 var subtitle: String? {
return locationName
}

You need to cast your subtitle as AnyObject as shown below:

let addressDict = [String(kABPersonAddressStreetKey): self.subtitle as! AnyObject]

and your complete code for "func mapItem() -> MKMapItem { }" will be:

func mapItem() -> MKMapItem {
let addressDict = [String(kABPersonAddressStreetKey): self.subtitle as! AnyObject]
let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDict)

let mapItem = MKMapItem(placemark: placemark)
mapItem.name = self.title

return mapItem
}


Related Topics



Leave a reply



Submit