Prefer Asset Symbols

Prefer using asset symbols over string-based image initialization

  • Identifier: prefer_asset_symbols
  • Enabled by default: No
  • Supports autocorrection: No
  • Kind: idiomatic
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.9.0
  • Default configuration:
    KeyValue
    severity warning

Rationale

UIKit.UIImage(named:) and SwiftUI.Image(_:) bear the risk of bugs due to typos in their string arguments. Since Xcode 15, Xcode generates codes for images in the Asset Catalog. Usage of these codes and system icons from SF Symbols avoid typos and allow for compile-time checking.

Non Triggering Examples

UIImage(resource: .someImage)
UIImage(systemName: "trash")
Image(.someImage)
Image(systemName: "trash")
UIImage(named: imageName)
UIImage(named: "image_\(suffix)")
Image(imageName)
Image("image_\(suffix)")

Triggering Examples

UIImage(named: "some_image")
UIImage(named: "some image")
UIImage.init(named: "someImage")
UIImage(named: "someImage", in: Bundle.main, compatibleWith: nil)
UIImage(named: "someImage", in: .main)
Image("some_image")
Image("some image")
Image.init("someImage")
Image("someImage", bundle: Bundle.main)
Image("someImage", bundle: .main)