Skip to content

Commit bd37f18

Browse files
committed
new posts
1 parent 0052193 commit bd37f18

5 files changed

+332
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
layout: post
3+
title: Swift 的懒加载和计算型属性
4+
subtitle: 比较水的个人笔记
5+
date: 2017-05-03
6+
author: BY
7+
header-img: img/post-bg-swift.jpg
8+
catalog: true
9+
tags:
10+
- iOS
11+
- Swift
12+
- Swift语法
13+
---
14+
15+
16+
> 本文首次发布于 [BY Blog](http://qiubaiying.github.io), 作者 [@柏荧(BY)](http://github.com/qiubaiying) ,转载请保留原文链接.
17+
18+
### 懒加载
19+
20+
常规(简化)写法
21+
22+
懒加载的属性用 `var` 声明
23+
24+
```
25+
lazy var name: String = {
26+
return "BY"
27+
}()
28+
```
29+
30+
完整写法
31+
32+
```
33+
lazy var name: String = { () -> String i
34+
return "BY"
35+
}()
36+
```
37+
38+
本质是一个创建一个闭包 `{}` 并且在调用该属性时执行闭包 `()`
39+
40+
如OC的懒加载不同的是 swift 懒加载闭包只调用一次,再次调用该属性时因为属性已经创建,不再执行闭包。
41+
42+
### 计算型属性
43+
44+
常规写法
45+
46+
```
47+
var name: string {
48+
return "BY"
49+
}
50+
```
51+
52+
完整写法
53+
54+
```
55+
var name: string {
56+
get {
57+
return "BY"
58+
}
59+
}
60+
```
61+
62+
计算型属性本质是重写了 `get` 方法,其类似一个无参有返回值函数,每次调用该属性都会执行 `return`
63+
64+
通常这样使用
65+
66+
```
67+
struct Cuboid {
68+
var width = 0.0, height = 0.0, depth = 0.0
69+
var volume: Double {
70+
return width * height * depth
71+
}
72+
}
73+
let fourByFiveByTwo = Cuboid(width: 4.0, height: 5.0, depth: 2.0)
74+
print("the volume of fourByFiveByTwo is \(fourByFiveByTwo.volume)")
75+
// Prints "the volume of fourByFiveByTwo is 40.0"
76+
```
77+
78+
### 两者对比
79+
80+
相同点
81+
82+
- 使用方法完全一致
83+
- 都是用 `var` 声明
84+
85+
不同点
86+
87+
- 实现原理不同
88+
89+
懒加载是第一次调用属性时执行闭包进行赋值
90+
91+
计算型属性是重写 `get` 方法
92+
93+
- 调用 `{}`的次数不同
94+
95+
懒加载的闭包只在属性第一次调用时执行
96+
计算型属性每次调用都要进入 `{}` 中,`return` 新的值
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
layout: post
3+
title: R.swift 的使用
4+
subtitle: 在项目中引入 R.swift,更安全的获取资源
5+
date: 2017-05-04
6+
author: BY
7+
header-img: img/post-bg-swift2.jpg
8+
catalog: true
9+
tags:
10+
- iOS
11+
- Swift
12+
- 开源库
13+
---
14+
15+
16+
> 本文首次发布于 [BY Blog](http://qiubaiying.github.io), 作者 [@柏荧(BY)](http://github.com/qiubaiying) ,转载请保留原文链接.
17+
18+
19+
# 什么是 R.swift
20+
21+
介绍 [R.swift](https://github.com/mac-cain13/R.swift) 前,我们先看看 R.swift 能做什么
22+
23+
通常,我们是基于 字符串 来获取资源,例如:图片、xib、或者是 segue
24+
25+
```swift
26+
let myImage = UIImage(named: "myImage")
27+
let myViewController = R.storyboard.main.myViewController()
28+
```
29+
30+
使用 R.swfit,我们可以这样写
31+
32+
```swift
33+
let myImage = R.image.myImage()
34+
let viewController = R.storyboard.main.myViewController()
35+
36+
```
37+
38+
R.swift 通过扫描你的各种基于字符串命名的资源,创建一个使用类型来获取资源。
39+
40+
在保证类型安全的同时,也在自动补全的帮助下节省了大量的时间。
41+
42+
# 导入 R.swift
43+
44+
[R.swift](https://github.com/mac-cain13/R.swift) 开源在 github 上。
45+
46+
这里是导入的[视频教程](https://vimeo.com/122888912)
47+
48+
使用 CocoaPods 导入项目中
49+
50+
1. 添加 `pod 'R.swift'`到 Podfile 文件,然后运行 `pod install`
51+
2. 添加一个 `New Run Script Phase`
52+
53+
![](https://ww4.sinaimg.cn/large/006tKfTcgy1ff84sw06qxj30vm0hrq6s.jpg)
54+
55+
3.`Run Script` 拖动到 `Check Pods Manifest.lock` 的下面,并且添加脚本 `"$PODS_ROOT/R.swift/rswift" "$SRCROOT/项目名称"`
56+
57+
![](https://ww4.sinaimg.cn/large/006tNc79gy1ff853qjiucj30yp0kkn1b.jpg)
58+
59+
4. `Command+B` 编译项目,在项目代码目录下,会生成一个 `R.generated.swift` 的文件,将它拖如项目中
60+
61+
>注意:不要勾选 `Copy items if needed` 选项,因为每次编译都会生成新的 `R.generated.swift` 文件,copy 的话,旧的 `R.generated.swift` 将不会被覆盖。
62+
63+
![](https://ww4.sinaimg.cn/large/006tNc79gy1ff85epj1qpj30qj0hdn17.jpg)
64+
65+
>tip: 可以在添加 `.gitignore` 添加一行 `*.generated.swift` 忽略该文件,避免造成冲突
66+
67+
68+
# 用法
69+
70+
71+
导入完成后,就可以在使用 R.swift 了
72+
73+
![](https://github.com/mac-cain13/R.swift/raw/master/Documentation/Images/DemoUseImage.gif)
74+
75+
关于 R.swift 的更多用法,可以 [看这里](https://github.com/mac-cain13/R.swift/blob/master/Documentation/Examples.md)
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
layout: post
3+
title: 在 Swift 中使用 IBInspectable
4+
subtitle: IBInspectable 在 Swift 中的实际应用
5+
date: 2017-05-05
6+
author: BY
7+
header-img: img/post-bg-swift.jpg
8+
catalog: true
9+
tags:
10+
- iOS
11+
- Swift
12+
- IBInspectable
13+
---
14+
15+
16+
> 本文首次发布于 [BY Blog](http://qiubaiying.github.io), 作者 [@柏荧(BY)](http://github.com/qiubaiying) ,转载请保留原文链接.
17+
18+
# 前言
19+
20+
通过 IB 设置 控件 的属性非常的方便。
21+
22+
![](https://ww3.sinaimg.cn/large/006tNc79gy1ff9fpog0vrj30ho084t9m.jpg)
23+
24+
但是缺点也很明显,那就是有一些属性没有暴露在 IB 的设置面板中。这时候就要使用 `@IBInspectable` 在 IB 面板中添加这些没有的属性。
25+
26+
关于在 OC 中使用 `IBInspectable` 可以看一下我的 [这篇文章](http://qiubaiying.top/2016/12/01/%E5%BF%AB%E9%80%9F%E6%B7%BB%E5%8A%A0%E5%9C%86%E8%A7%92%E5%92%8C%E6%8F%8F%E8%BE%B9/#高级)
27+
28+
# 正文
29+
30+
在项目中最常遇到的情况是为 view 设置圆角、描边,以及为 文本控件 添加本地化字符串。
31+
32+
## 圆角、描边
33+
34+
先来看看设置圆角、描边
35+
36+
```swift
37+
extension UIView {
38+
@IBInspectable var cornerRadius: CGFloat {
39+
get {
40+
return layer.cornerRadius
41+
}
42+
43+
set {
44+
layer.cornerRadius = newValue
45+
layer.masksToBounds = newValue > 0
46+
}
47+
}
48+
49+
@IBInspectable var borderWidth: CGFloat {
50+
get {
51+
return layer.borderWidth
52+
}
53+
set {
54+
layer.borderWidth = newValue > 0 ? newValue : 0
55+
}
56+
}
57+
58+
@IBInspectable var borderColor: UIColor {
59+
get {
60+
return UIColor(cgColor: layer.borderColor!)
61+
}
62+
set {
63+
layer.borderColor = newValue.cgColor
64+
}
65+
}
66+
67+
}
68+
```
69+
70+
添加完成就可以在 IB 中设置 view 的这些属性了
71+
72+
![](https://ww4.sinaimg.cn/large/006tNc79gy1ff9h5afhv2j30f803ajri.jpg)
73+
74+
运行效果
75+
76+
![](https://ww3.sinaimg.cn/large/006tNc79gy1ff9h70z922j30ag061wf7.jpg)
77+
78+
## 利用 @IBDesignable 在 IB 中实时显示 @IBInspectable 的样式
79+
80+
创建一个新的 class 继承 `UIView` ,并且使用 `@IBDesignable` 声明
81+
82+
```swift
83+
import UIKit
84+
85+
@IBDesignable class IBDesignableView: UIView {
86+
87+
}
88+
```
89+
90+
在 IB 中,选择 view 的 class 为 我们新建的 `IBDesignableView`
91+
92+
93+
94+
![](https://ww1.sinaimg.cn/large/006tNc79gy1ff9hs6z5q1j30fr03vweu.jpg)
95+
96+
这样在 IB 调整属性时,这些属性的变化就会实时显示在 IB 中。
97+
98+
99+
## 本地化字符串
100+
101+
本地化字符串的解决方法和上面的添加圆角一样
102+
103+
```swift
104+
extension UILabel {
105+
@IBInspectable var localizedKey: String? {
106+
set {
107+
guard let newValue = newValue else { return }
108+
text = NSLocalizedString(newValue, comment: "")
109+
}
110+
get { return text }
111+
}
112+
}
113+
114+
extension UIButton {
115+
@IBInspectable var localizedKey: String? {
116+
set {
117+
guard let newValue = newValue else { return }
118+
setTitle(NSLocalizedString(newValue, comment: ""), for: .normal)
119+
}
120+
get { return titleLabel?.text }
121+
}
122+
}
123+
124+
extension UITextField {
125+
@IBInspectable var localizedKey: String? {
126+
set {
127+
guard let newValue = newValue else { return }
128+
placeholder = NSLocalizedString(newValue, comment: "")
129+
}
130+
get { return placeholder }
131+
}
132+
}
133+
```
134+
135+
这样,在 IB 中我们就可以利用对应类型的 Localized Key 来直接设置本地化字符串了:
136+
137+
![](https://ww1.sinaimg.cn/large/006tNc79gy1ff9h94um01j30aj01vjre.jpg)
138+
139+
140+
141+
# 结语
142+
143+
`IBInspectable` 可以使用这些的类型
144+
145+
- `Int`
146+
- `CGFloat`
147+
- `Double`
148+
- `String`
149+
- `Bool`
150+
- `CGPoint`
151+
- `CGSize`
152+
- `CGRect`
153+
- `UIColor`
154+
- `UIImage`
155+
156+
合理的使用`@IBInspectable` 能减少很多的模板代码,提高我们的开发效率。
157+
158+
> 参考
159+
>
160+
> - [《再看关于 Storyboard 的一些争论》](https://onevcat.com/2017/04/storyboard-argue/)
161+
> - [@IBDesignable and @IBInspectable in Swift 3》](https://medium.com/@Anantha1992/ibdesignable-and-ibinspectable-in-swift-3-702d7dd00ca)

img/post-bg-swift.jpg

151 KB
Loading

img/post-bg-swift2.jpg

110 KB
Loading

0 commit comments

Comments
 (0)