Swift - Could Not Cast Value of Type '_Nscfstring' to 'Nsdictionary'

Swift - Could not cast value of type '__NSCFString' to 'NSDictionary'

Issue regards snapshot cast to NSDictionary. Since snapshot value is a String.
Try this:

FIRDatabase.database().reference().child("users").child(uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in

if let dictionary = snapshot.value as? NSDictionary {

if let username = dictionary["name"] as? String {
cell.name.text = username
}

if let userlogin = dictionary["login"] as? String {
cell.login.text = userlogin
}
}
})

Could not cast value of type __NSCFString

alert key can be string or dictionary (depends on your server). You can try this.

if let dict = notification.request.content.userInfo["aps"] as? [String : Any] {

if let d = dict["alert"] as? [String : Any],
let body = d["body"] as? String,
let title = d["title"] as? String {

print("Title:\(title) + body:\(body)")
} else if let body = dict["alert"] as? String {

print("body:\(body)")
}
}

And also you should avoid force casting and don't use NSDictionary in swift.

Could not cast value of type '__NSCFString' to 'NSData'

You are mixing up String and Data in createBodyWithParams

Don't use NSMutableData in Swift 3+ anyway. Data is mutable with var

However I recommend to compose the body as String and create the Data object at the end.

func createBodyWithParams(_ parameters: [String: String]?, filePathKey: String?, mediaPath: Data, boundary: String) -> Data {

var body = ""

if let params = parameters {
for (key, value) in params {
body += "--\(boundary)\r\n"
body += "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n"
body += "\(value)\r\n"
}
}
var filename = ""

if imageSelected {
filename = "video-\(uuid).mp4"
}

let mimetype = "video/mp4"
body += "--\(boundary)\r\n"
body += "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n"
body += "Content-Type: \(mimetype)\r\n\r\n"
body += String(data: mediaPath, encoding: .utf8)!
body += "\r\n"

body += "--\(boundary)--\r\n"

return Data(body.utf8)

}

Xcode Swift: Could not cast value of type '__NSCFString' (0x102e8ac50) to 'NSDictionary' (0x102e8b8d0)

The error is clear, you are trying to convert a string to a dictionary. In the json which you posted the key "image", "image2" and "image3" are strings.

{
"titulo": "Verox's 1002",
"image": "http://wlodsgn.x10host.com/img/jeans/vrxjns1002/veroxjeans1002_front.jpg",
"image2": "http://wlodsgn.x10host.com/img/jeans/vrxjns1002/veroxjeans1002_rightside.jpg",
"image3": "http://wlodsgn.x10host.com/img/jeans/vrxjns1002/veroxjeans1002_front_b.jpg",
"marca": "Verox",
"color": "Azul",
"tipo": "Jean",
"ref": 1002
}

so the correct way the get those values is:

if let imageURL = data["image"] as? String {
self.imageUrl = imageURL
}

Avoid to use the explicit unwrap if you are not sure of the type of the object that you are try to casting.

UPDATE

The new error is of the same nature of the previous one, you are implicitly unwrapping a nil value that in this case is the closure completion. Your definition of the closure is completion: ((AnyObject) -> Void)! say to the compiler:

Don't worry when you will need to use this closure it will certainly has a non-nil value.

But instead you are passing a nil value here:

json.loadBbup(nil)

The implicit unwrapping is useful but you are using it in a wrong way. If you want that the completion closure can be nil you have to define it as optional in this way:

completion: ((AnyObject) -> Void)?

and the call it in this way:

completion?(jnslst)

Regarding the fact that the array contains only 3 values instead 20 there be a lot of reasons. I suggest to put a breakpoint in this line jnslst.append(jnsextrct) and watch the value of jnsextrct.

As general advices:

  • avoid to use the implicit unwrapping unless you're certain the variable as non-nil value.
  • use let instead var when you don't need to modify a variable; in this way you are certain that value never changes.

Swift: Could not cast value of type 'Could not cast value of type '__NSCFString' (0x10e4d5ef0) to 'NSDictionary' (0x10e4d6bc0).'

Check your return data from web-service. I think you are converting it in Jason String multiple times, Or something like this. There is no issue in above code. Check your server side.



Related Topics



Leave a reply



Submit