查看UIKit 的开源实现,似乎没有区别:
extension UIImage : _ImageLiteralConvertible {
private convenience init!(failableImageLiteral name: String) {
self.init(named: name)
}
public required convenience init(imageLiteralResourceName name: String) {
self.init(failableImageLiteral: name)
}
}
public typealias _ImageLiteralType = UIImage
它所做的只是强制解开init(named:)
.
似乎它只是以下_ImageLiteralConvertible
协议的实现CompilerProtocols.swift
:
public protocol _ImageLiteralConvertible {
init(imageLiteralResourceName path: String)
}
AppKit也有类似的实现:
extension NSImage : _ImageLiteralConvertible {
private convenience init!(failableImageLiteral name: String) {
self.init(named: name)
}
public required convenience init(imageLiteralResourceName name: String) {
self.init(failableImageLiteral: name)
}
}
public typealias _ImageLiteralType = NSImage
这可能与#imageLiteral
添加到 Xcode 8 的新图像文字功能 ( ) 有关,并且永远不应直接调用。