在Swift中是没有宏定义的,但是我们可以灵活的使用rawValue为String的enum类型来代替字符串,然后通过为资源类型添加合适的extension来让编译器帮助我们在资源名称修改时能在代码中做出对应的改变
enum ImageName: String {
case MyIcon = "my_icon"
}
enum SegueName: String {
case MySegue = "my_segue"
}
extension UIImage {
convenience init!(imageName: ImageName) {
self.init(named: imageName.rawValue)
}
}
extension UIViewController {
func performSegueWithSegueName(segueName: SegueName, sender: AnyObject?) {
performSegue(withIdentifier: segueName.rawValue, sender: sender)
}
}
网友评论