-6

ok guys can I get a little help with my code. When running the app I get an error is there any way to fix this problem?

let fileUrl = dict["fileUrl"]as! String
let url = NSURL(string: fileUrl)
let data = NSData(contentsOf: url! as URL!)
let picture = UIImage(data: data! as Data!)
let photo = JSQPhotoMediaItem(image: picture)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))

Image here

Anurag Sharma
  • 4,276
  • 2
  • 28
  • 44

3 Answers3

1

Here I see 4 big fat problems with your code.

  1. You are force casting the value of fileUrl of the dictionary dict to String. If your dictionary doesn't have the value for fileUrl, or if it's not castable to string, your code will crash. You should change that to optional cast like:

    if let fileUrl = dict["fileUrl"] as? String { //your code if you have fileUrl }

  2. When creating the url to the file, you are using the wrong initialization method, you should be using this:

    let url = URL(fileURLWithPath: fileUrl)

  3. After you have the url to the file, you should also check if you have the data of the file, because contentsOfFile: initializer of the NSData returns the optional object, which may be nil, so another if check: if let data = NSData(contentsOf: url) {\\ code with the data}

  4. init?(data: Data) initializer of the UIImage also returns optional object, so if the required by latter code, you should also check if you have the image or nil with if statement.

The result code should be something like:

if let fileUrl = dict["fileUrl"] as? String {
            let url = URL(fileURLWithPath: fileUrl)
            if let data = NSData(contentsOf: url) {
                let image = UIImage(data: data as Data) // you can cast NSData to Data without force or optional casting
                let photo = JSQPhotoMediaItem(image: image)
                self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
            }
        }

Hope this helps.

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
0

Replace the first line of code with this line for optional binding check :-

guard let fileUrl = dict["fileUrl"] as! String else {return}
Himanshu
  • 2,832
  • 4
  • 23
  • 51
0

Yo should do validation in cases where the variable may be nil, the following is an example:

if let fileUrl = dict["fileUrl"] as? String {
    let url = URL(string: fileUrl)
    do {
        let data = try Data(contentsOf: url!)
        let picture = UIImage(data: data)
        let photo = JSQPhotoMediaItem(image: picture)
        self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
    } catch {

    }
}
chengsam
  • 7,315
  • 6
  • 30
  • 38
  • While solving 2 issues with code, your code doesn't solve other problems. Please, pay more attention when you are helping with code. – Fahri Azimov Mar 10 '17 at 06:34
  • @FahriAzimov I haven't tested my code, could you point the problems this solution may have? – chengsam Mar 10 '17 at 06:38