R.swift的安装和使用

R.swift

1.终端导入R.swift

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
target '项目名' do
  use_frameworks!
  pod 'R.swift' 
end

2.R.swift的安装

  1. 添加Run Script文件
3371612405485_.pic_hd.jpg

2.挪动Run Script文件位置,在Mainfest.lock和Compile Sources之间

3381612405554_.pic.jpg

3.添加Run Script的脚本

(1)"$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/R.generated.swift"

(2)$TEMP_DIR/rswift-lastrun

(3)$SRCROOT/R.generated.swift
3391612405776_.pic.jpg

4.编译项目

show in find在项目目录下发现多一个 R.generated.swift文件,将R.generated.swift文件添加到项目,当把资源文件拖到项目后,R.generated.swift文件会实时更新,就可以正常使用了

image.png
image.png

注意:添加的时候不要勾选 copy item if needs选项,否则图片等有变化,R.generated.swift文件将不会更新

3.使用

原来写法

let icon = UIImage(named: "settings-icon")
let font = UIFont(name: "San Francisco", size: 42)
let color = UIColor(named: "indicator highlight")
let viewController = CustomViewController(nibName: "CustomView", bundle: nil)
let string = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Arthur Dent")

使用R.wift写法

let icon = R.image.settingsIcon()
let font = R.font.sanFrancisco(size: 42)
let color = R.color.indicatorHighlight()
let viewController = CustomViewController(nib: R.nib.customView)
let string = R.string.localizable.welcomeWithName("Arthur Dent")

你可能感兴趣的:(R.swift的安装和使用)