在新的項目中,接觸到了一個很不錯的框架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這個第三方庫.