@@ -33,12 +33,6 @@ class LoginViewController: UIViewController {
3333 @IBOutlet weak var btnClose : UIButton !
3434
3535
36- var disposeBag = DisposeBag ( )
37-
38-
39- var indicatorView : UIActivityIndicatorView !
40-
41-
4236 deinit {
4337 print ( " deinit: \( self . description) " )
4438 }
@@ -50,20 +44,22 @@ class LoginViewController: UIViewController {
5044
5145 btnClose. rx. tap
5246 . subscribe ( onNext: { [ weak self] in
53- self ? . dismiss ( animated: true , completion: nil )
47+ guard let strongSelf = self else { return }
48+ strongSelf. dismiss ( animated: true , completion: nil )
5449 } )
55- . addDisposableTo ( disposeBag )
50+ . addDisposableTo ( CS_DisposeBag )
5651
5752
5853
5954 // 声明Observable,可观察对象
6055 // username的text没有太多参考意义,因此使用map来加工,得到是否可用的消息
6156 let userValidation = textFieldUsername. rx. text. orEmpty
57+ // map的参数是一个closure,接收element
6258 . map { ( user) -> Bool in
6359 let length = user. characters. count
6460 return length >= minUsernameLength && length <= maxUsernameLength
6561 }
66- . shareReplay ( 1 )
62+ . shareReplay ( 1 )
6763
6864 let passwdValidataion = textFieldPasswd. rx. text. orEmpty
6965 . map { ( passwd) -> Bool in
@@ -75,27 +71,28 @@ class LoginViewController: UIViewController {
7571 // 声明Observable
7672 // 组合两个Observable
7773 let loginValidation = Observable . combineLatest ( userValidation, passwdValidataion) {
78- $0 && $1
79- } . shareReplay ( 1 )
74+ $0 && $1
75+ }
76+ . shareReplay ( 1 )
8077
8178
8279 // bind,即将Observable与Observer绑定,最终也会调用subscribe
8380 // 此处是将isEnabled视为一个Observer,接收userValidation的消息,做出响应
8481 // 所以Observable发送的消息与Observer能接收的消息要对应起来(此处是Bool)
8582 userValidation
8683 . bindTo ( textFieldPasswd. rx. isEnabled)
87- . addDisposableTo ( disposeBag )
84+ . addDisposableTo ( CS_DisposeBag )
8885 userValidation
8986 . bindTo ( lbUsernameInfo. rx. isHidden)
90- . addDisposableTo ( disposeBag )
87+ . addDisposableTo ( CS_DisposeBag )
9188
9289 passwdValidataion
9390 . bindTo ( lbPasswdInfo. rx. isHidden)
94- . addDisposableTo ( disposeBag )
91+ . addDisposableTo ( CS_DisposeBag )
9592
9693 loginValidation
9794 . bindTo ( btnLogin. rx. isEnabled)
98- . addDisposableTo ( disposeBag )
95+ . addDisposableTo ( CS_DisposeBag )
9996
10097
10198 // 将tap操作视为一个Observable,添加一些对应的响应操作(订阅),一旦tap执行(即发送消息),则会执行对应的响应代码
@@ -104,18 +101,20 @@ class LoginViewController: UIViewController {
104101 . subscribe (
105102 onNext: { [ weak self] in
106103 print ( " onNext " )
107- self ? . login ( )
104+ guard let strongSelf = self else { return }
105+ strongSelf. login ( )
108106 } ,
109107 onCompleted: { [ weak self] in
110108 print ( " onCompleted " )
111- print ( self !)
109+ guard let strongSelf = self else { return }
110+ print ( strongSelf)
112111 }
113112 )
114- . addDisposableTo ( disposeBag )
113+ . addDisposableTo ( CS_DisposeBag )
115114 }
116115
117116 func login( ) {
118- indicatorView = UIActivityIndicatorView ( activityIndicatorStyle: UIActivityIndicatorViewStyle . gray)
117+ let indicatorView = UIActivityIndicatorView ( activityIndicatorStyle: UIActivityIndicatorViewStyle . gray)
119118 view. addSubview ( indicatorView)
120119 indicatorView. center = view. center
121120
@@ -129,7 +128,7 @@ class LoginViewController: UIViewController {
129128
130129 DispatchQueue . main. async {
131130 print ( " Done " )
132- self . indicatorView. stopAnimating ( )
131+ indicatorView. stopAnimating ( )
133132 }
134133 }
135134 }
0 commit comments