在新的项目中,接触到了一个很不错的框架R.swift,能够帮助更方便安全的使用资源文件,相信已经使用过的或者尚未接触过的,一旦使用过了解过它,会爱上这个框架工具!json
3. 打开并复制代码放入下面:"$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/R.generated.swift"swift
4. 拖拽R.generated.swift文件到项目中.安全
原生写法框架
let sIcon = UIImage(named: "settings-icon")
使用R.swift工具
func icon() -> UIImage? { switch self { case .sourceRegulator: return R.image.home_SourceRegulatory() case .regulation: return R.image.home_regulationIcon() case .broker: return R.image.home_brokerIcon() case .engine: return R.image.home_engineIcon() case .falseBroker: return R.image.home_falseBrokerIcon() case .spread: return R.image.home_spredIcon() } }
原始写法字体
let plistURL = Bundle.main.url(forResource: "Book", withExtension: "plist") let jsonPath = Bundle.main.path(forResource: "data", ofType: "json")
使用R.swift后ui
let plistURL = R.file.bookPlist() let jsonPath = R.file.DataJson.path()
原始用法url
let lightFontTitle = UIFont(name: "", size: 22)chalkduster
使用R.swiftspa
R.font.chalkduster(size: 35)
原始写法code
let welcomeMessage = NSLocalizedString("welcome.message", comment: "") let settingsTitle = NSLocalizedString("title", tableName: "Settings", comment: "") // Formatted strings let welcomeName = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Alice") // Stringsdict files let progress = String(format: NSLocalizedString("copy.progress", comment: ""), locale: NSLocale.current, 4, 23)
使用R.swift
// Localized strings are grouped per table (.strings file) let welcomeMessage = R.string.localizable.welcomeMessage() let settingsTitle = R.string.settings.title() // Functions with parameters are generated for format strings let welcomeName = R.string.localizable.welcomeWithName("Alice") // Functions with named argument labels are generated for stringsdict keys let progress = R.string.localizable.copyProgress(completed: 4, total: 23)
上面就是本人项目常用到的,固然还有其余的用法和用处,不过,经过R.swift已经大大的方便咱们平常开发,但愿你们在项目中尽早的使用R.swift这个第三方库.