3 Replies
      Latest reply: Feb 10, 2017 3:15 PM by eskimo RSS
      hunterw Level 1 Level 1 (0 points)

        Hello, I am pretty new to swift programming and was watching a video made by apple on how to make an app. It seems as though the video is a little outdated, as one of the lines of code has an error. I will post the code below. Next to the code I will also write where the error is. If needed I can upload the files to the project. Thanks and I hope someone can help me fix this problem! (Value of optional type "UIImage?" not unwrapped' did you mean to use "!" or "?"?)

        import UIKit

        class ViewController: UIViewController {

            @IBOutlet weak var photoimageView: UIImageView!

         

            let context = CIContext(options: nil)

          

            @IBAction func applyFilter(_ sender: AnyObject) {

              

                let inputImage = CIImage(image: photoimageView.image) This is where the error occurs

              

                let randomColor = [kCIInputAngleKey: (Double(arc4random_uniform(314)) / 100)]

              

                let filteredImage = inputImage.imageByApplyingFilter("CIHueAdjust", withInputPeramiters: randomColor)

              

                let renderedImage = context.createCGImage(filteredImage, fromRect: filteredImage.extent())

             

                photoimageView.image = UIImage(CGImage: renderedImage)

              

            }

            override func viewDidLoad() {

                super.viewDidLoad()

            }

            override func didReceiveMemoryWarning() {

                super.didReceiveMemoryWarning()

            }

        }

        • Re: Error in IOS Coding. Value of Optional type "UIImage?" not unwrapped
          eskimo Apple Staff Apple Staff (6,470 points)

          let inputImage = CIImage(image: photoimageView.image)

          photoimageView is a UIImageView.  It’s image property is of type UIImage?, that is, an optional UIImage.  An optional UIImage may be nil, that is, there may be no image there.

          You’re trying to pass that optional UIImage to CIImage(image:), but it’s image parameter is of type UIImage (not UIImage?), and thus you get a compile-time error telling you that the types don’t line up.

          You have two basic choices here:

          • If you believe that photoimageView.image can never be nil in this context, you can force unwrap the optional.  So, your call would look like CIImage(image: photoimageView.image!).

            The force unwrap will trap (crashing your app) if the value is nil, so you should only do this if the presence of nil indicates a fundamental problem with your app.  For example, if you added the image to the image view in Interface Builder, then a nil image would indicate that your app was built incorrectly, and it’s OK to crash in that case.

          • If, OTOH, is possible that photoimageView.image might be nil, you need to check for and handle this case.  You might, for example, use guard let to detect that case and display an error to the user.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Error in IOS Coding. Value of Optional type "UIImage?" not unwrapped
              hunterw Level 1 Level 1 (0 points)

              When I added a !, another error appeared, when I added a ! to that error, another error appeared, and so on. How do I fix this?

                • Re: Error in IOS Coding. Value of Optional type "UIImage?" not unwrapped
                  eskimo Apple Staff Apple Staff (6,470 points)

                  You are going to have to read up on how Swift handles optional values, and then write your code based on that understanding.  Optionals are covered in depth in The Swift Programming Language, and I’d expect them to similarly covered by any introduction to Swift programming.

                  One thing I find very useful when dealing with type mismatches in Swift is to take advantage of type inference and Xcode’s ability to show a type.  For example, you split your problematic line into two parts:

                  let tmp = photoimageView.image
                  let inputImage = CIImage(image: tmp)
                  

                  you can option-click on tmp to discover its type is UIImage?, which doesn’t match the expected type to CIImage(image:), which is just plain UIImage.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"