Swift UIImage extension

10,247

Solution 1

There are 2 problems:

1. Failable init

In your convenience initializer you are calling a failable initializer. So how can you guarantee that an instance of UIImage is always created when you are relying on a failable initializer that, by definition, does not guarantee that? You can fix this by using the magic ! when you call the failable init.

2. Referencing the param you received

When you call self.init you are not passing the param received in your init. You are instead referencing the enum definition. To fix this replace this

self.init(named: AssetIdentifier.RawValue)

with this

self.init(named: assetIdentifier.rawValue)

Wrap up

This is the result

extension UIImage {
    enum AssetIdentifier: String {
        case Search = "Search"
        case Menu = "Menu"
    }
    convenience init(assetIdentifier: AssetIdentifier) {
        self.init(named: assetIdentifier.rawValue)!
    }
}

Testing

UIImage(assetIdentifier: .Search)

Solution 2

You can use this code. I have tested it.

import UIKit
import Foundation

enum AssetIdentifier: String {
    case Search = "Search"
    case Menu = "Menu"
}
extension UIImage {
    convenience init?(assetIdentifier: AssetIdentifier) {
        self.init(named: assetIdentifier.rawValue)
    }
}


class ViewController: UIViewController {

    @IBOutlet var imageview: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        imageview.image = UIImage(assetIdentifier: AssetIdentifier.Menu)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
Share:
10,247

Related videos on Youtube

Cody Weaver
Author by

Cody Weaver

BY DAY: I work at a Olive Garden as a server. FREE TIME: Swift programmer with limited knowledge of Front-End web development. Also some work with PHP and MySQL. My tools of use are a MacBook Air 11 inch, iPad Mini first edition, and iPhone 6 all running the latest version of the publicly released software. I am part of the Apple Developer program and have released one iOS app. You can find it if you search "Htlaeh" on the iOS app store. PERSONAL: Married to my lovely wife Evelyn with a son on the way (Noah). Lived in California all my life. P.S. I am the little boy in the picture the other man is my Father, Felix. He was my hero and a great man.

Updated on September 15, 2022

Comments

  • Cody Weaver
    Cody Weaver over 1 year

    I am trying to make my code safer using Enums and a connivence initializer when dealing with UIImage and the Asset Catalog. My code is here below.

    import UIKit
    
    extension UIImage {
        enum AssetIdentifier: String {
            case Search = "Search"
            case Menu = "Menu"
        }
    
        convenience init(assetIdentifier: AssetIdentifier) {
            self.init(named: AssetIdentifier.RawValue)
        }
    }
    

    Currently I am getting this error.

    'Cannot invoke 'UIImage.init' with an argument of type '(named: RawValue.Type)'
    
  • Cody Weaver
    Cody Weaver over 8 years
    Thanks man yeah I just figured it out right before I refreshed the page. Its actually better to not make this init an optional value because you know your enum values are good.
  • Luca Angeletti
    Luca Angeletti over 8 years
    @CodyWeaver: please don't forget to accept the answer if you think it is correct.