编译你的项目,在Finder中你会看到R.generated.swift在Pod文件中,将该文件拖动至项目中,切记千万不要勾选Copy items if needed,
新增\删除\修改资源文件之后都需要重新command+B编译项目,保证正确引用。
使用方式
1、Images
之前:
1 2
let png =UIImage(named: "png") let jpg =UIImage(named: "jpg.jpg")
R.swift:
1 2
let png =R.image.png() let jpg =R.imgae.jpgJpg()
2、Custom fonts
之前:
1
let customFont =UIFont(name: "Acme-Light", size: 22)
R.swift:
1
let customFont =R.font.acmeLight(size: 22)
3、Resource files
之前 :
1 2
let jsonURL =Bundle.main.url(forResource: "seed-data", withExtension: "json") let jsonPath =Bundle.main.path(forResource: "seed-data", ofType: "json")
R.swift:
1 2
let jsonURL =R.file.seedDataJson() let jsonPath =R.file.seedDataJson.path()
4、Localized Strings
之前:
1 2 3 4 5
let welcomeMessage =NSLocalizedString("welcome.message", comment: "") let settingsTitle =NSLocalizedString("title", tableName: "Settings", comment: "") let welcomeName =String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Alice") let progress =String(format: NSLocalizedString("copy.progress", comment: ""), locale: NSLocale.current, 4, 23)
R.swift:
1 2 3 4
let welcomeMessage =R.string.localizable.welcomeMessage() let settingsTitle =R.string.settings.title() let welcomeName =R.string.localizable.welcomeWithName("Alice") let progress =R.string.localizable.copyProgress(completed: 4, total: 23)
let nameOfNib ="CustomView" let customViewNib =UINib(nibName: "CustomView", bundle: nil) let rootViews = customViewNib.instantiate(withOwner: nil, options: nil) let customView = rootViews[0] as?CustomView let viewControllerWithNib =CustomViewController(nibName: "CustomView", bundle: nil)
R.swift:
1 2 3 4 5 6
let nameOfNib =R.nib.customView.name let customViewNib =R.nib.customView() let rootViews =R.nib.customView.instantiate(withOwner: nil) let customView =R.nib.customView.firstView(owner: nil)
let viewControllerWithNib =CustomViewController(nib: R.nib.customView)