我如何使用 RxSwift 让我的程序在 swift 中具有反应性
How can i make my program reactive in swift with RxSwift
嗨!!!
我在OSX,最后一个,用的是最后一个xcode版本,也是iOS的最后一个版本:)
我在测试项目中实现 mvvm 和响应式编程时遇到了一些问题。
对于反应式,我正在使用这里的 RxSwift 库 RxSwift
// so here my implementation of SettingsVC.swift
import UIKit
import SpeedLog
import RxCocoa
import RxSwift
import Eureka
class SettingsVC: FormViewController {
let viewModel = SettingsVM()
override func viewDidLoad() {
super.viewDidLoad()
self.setupFormer()
}
func setupFormer() {
// self.tableView?
// .rx_setDelegate(self)
// .addDisposableTo(disposeBag)
//
// // Setup Observer
self.setRegisterAndLoginRows()
self.setProfileRow()
}
func setRegisterAndLoginRows() {
self.form
+++= ButtonRow("connectDisconnect") {
[=11=].title = "Connexion"
} .onCellSelection { row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
UIViewController.pushModal(self, pushModalTo: PushModalTo.Connection)
}
<<< ButtonRow("registration") {
[=11=].title = "Inscription"
} .onCellSelection { row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
//UIViewController.pushModal(self, pushModalTo: PushModalTo.Connection)
}
}
func setProfileRow() {
form
+++= Section("Mon compte")
<<< LabelRow("profile") {
[=11=].title = "Mon profil"
[=11=].value = "Non enregistré"
let cell = [=11=]
Session.appUser?.username
.asObservable()
.subscribeNext { username in
if let usernameTemp = username {
cell.value = usernameTemp
}
}
.addDisposableTo((Session.appUser?.disposeBag)!)
} .onCellSelection { row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
UIViewController.pushView(self, pushSettingsTo: PushSettingsTo.MyProfile)
}
}
}
// my SessionManager
import Foundation
import RxSwift
let Session = SessionManager.sharedInstance
class SessionManager {
static let sharedInstance = SessionManager()
var appUser: AppUser?
init() {
self.appUser = nil
}
}
// and my class User, because AppUser just inherit from User
import Foundation
import SpeedLog
import RxSwift
class User {
var username: Variable<String?> = Variable(nil)
var accessToken: Variable<String?> = Variable(nil)
var country: Variable<String?> = Variable(nil)
var created: Variable<NSDate?> = Variable(nil)
var disabled: Variable<Bool?> = Variable(nil)
var displayName: Variable<String?> = Variable(nil)
var email: Variable<String?> = Variable(nil)
var emailVerified: Variable<Bool?> = Variable(nil)
var locale: Variable<LocaleContainer?> = Variable(nil)
var modified: Variable<NSDate?> = Variable(nil)
var phoneNumber: Variable<String?> = Variable(nil)
var phoneVerified: Variable<Bool?> = Variable(nil)
var userID: Variable<String?> = Variable(nil)
var picture: Variable<UIImage?> = Variable(nil)
var pictureURL: Variable<String?> = Variable(nil)
let disposeBag = DisposeBag()
func set(kiiUser: KiiUser) {
self.username.value = kiiUser.username
self.accessToken.value = kiiUser.accessToken
self.country.value = kiiUser.country
self.created.value = kiiUser.created
self.disabled.value = kiiUser.disabled
self.displayName.value = kiiUser.displayName
self.email.value = kiiUser.email
self.emailVerified.value = kiiUser.emailVerified
self.locale.value = kiiUser.locale
self.modified.value = kiiUser.modified
self.phoneNumber.value = kiiUser.phoneNumber
self.phoneVerified.value = kiiUser.phoneVerified
self.userID.value = kiiUser.userID
self.picture.value = UIImage()
self.pictureURL.value = String()
}
}
我已经尝试将 Singleton 更改为 Variable() 但它不起作用..
我做错了什么?
抱歉我的英语不好:/
感谢您的帮助
嗨,
我找到了实现 Eureka 库、rxswift 和 MVVM 的解决方案,我将分享它:
设置VC
import UIKit
import SpeedLog
import RxCocoa
import RxSwift
import Eureka
class SettingsVC: FormViewController {
private var viewModel: SettingsVM?
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
self.initUI()
self.bindViewModel()
}
func initUI() {
// Settings form rows
self.form
+++= ButtonRow("connectDisconnect")
<<< ButtonRow("registration")
self.form
+++= Section("Mon compte")
<<< LabelRow("profile")
}
func bindViewModel() {
self.viewModel = SettingsVM(form: &self.form, vc: self)
}
}
设置虚拟机
import Foundation
import SpeedLog
import RxCocoa
import RxSwift
import RxViewModel
import Eureka
class SettingsVM: RxViewModel {
// Private
private let disposeBag = DisposeBag()
let vc: SettingsVC
let connectDisconnectRow: ButtonRow?
let inscriptionRow: ButtonRow?
let myProfileRow: LabelRow?
init(inout form: Form, vc: SettingsVC) {
self.vc = vc
self.connectDisconnectRow = form.rowByTag("connectDisconnect")
self.inscriptionRow = form.rowByTag("registration")
self.myProfileRow = form.rowByTag("profile")
super.init()
self.configureCell()
}
func configureCell() {
// Connexion Disconnect button
connectDisconnectRow?.baseCell.baseRow.title = "Connexion"
connectDisconnectRow?.onCellSelection { cell, row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
UIViewController.pushModal(self.vc, pushModalTo: PushModalTo.Connection)
}
// Inscription button
inscriptionRow?.baseCell.baseRow.title = "Inscription"
inscriptionRow?.onCellSelection { cell, row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
UIViewController.pushModal(self.vc, pushModalTo: PushModalTo.Registration)
}
// My profile button
myProfileRow?.baseCell.baseRow.title = "Mon profil"
myProfileRow?.baseCell.baseRow.baseValue = "Non enregistré"
_ = Session.appUser.displayName.asObservable().subscribeNext { displayName in
if displayName != "" && displayName != nil {
self.myProfileRow?.baseCell.baseRow.baseValue = displayName
}
self.myProfileRow?.baseCell.baseRow.reload()
}
myProfileRow?.onCellSelection { cell, row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
UIViewController.pushView(self.vc, pushSettingsTo: PushSettingsTo.MyProfile)
}
}
}
我希望它能帮助所有想要实现这些东西的人:)
再见
嗨!!!
我在OSX,最后一个,用的是最后一个xcode版本,也是iOS的最后一个版本:)
我在测试项目中实现 mvvm 和响应式编程时遇到了一些问题。 对于反应式,我正在使用这里的 RxSwift 库 RxSwift
// so here my implementation of SettingsVC.swift
import UIKit
import SpeedLog
import RxCocoa
import RxSwift
import Eureka
class SettingsVC: FormViewController {
let viewModel = SettingsVM()
override func viewDidLoad() {
super.viewDidLoad()
self.setupFormer()
}
func setupFormer() {
// self.tableView?
// .rx_setDelegate(self)
// .addDisposableTo(disposeBag)
//
// // Setup Observer
self.setRegisterAndLoginRows()
self.setProfileRow()
}
func setRegisterAndLoginRows() {
self.form
+++= ButtonRow("connectDisconnect") {
[=11=].title = "Connexion"
} .onCellSelection { row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
UIViewController.pushModal(self, pushModalTo: PushModalTo.Connection)
}
<<< ButtonRow("registration") {
[=11=].title = "Inscription"
} .onCellSelection { row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
//UIViewController.pushModal(self, pushModalTo: PushModalTo.Connection)
}
}
func setProfileRow() {
form
+++= Section("Mon compte")
<<< LabelRow("profile") {
[=11=].title = "Mon profil"
[=11=].value = "Non enregistré"
let cell = [=11=]
Session.appUser?.username
.asObservable()
.subscribeNext { username in
if let usernameTemp = username {
cell.value = usernameTemp
}
}
.addDisposableTo((Session.appUser?.disposeBag)!)
} .onCellSelection { row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
UIViewController.pushView(self, pushSettingsTo: PushSettingsTo.MyProfile)
}
}
}
// my SessionManager
import Foundation
import RxSwift
let Session = SessionManager.sharedInstance
class SessionManager {
static let sharedInstance = SessionManager()
var appUser: AppUser?
init() {
self.appUser = nil
}
}
// and my class User, because AppUser just inherit from User
import Foundation
import SpeedLog
import RxSwift
class User {
var username: Variable<String?> = Variable(nil)
var accessToken: Variable<String?> = Variable(nil)
var country: Variable<String?> = Variable(nil)
var created: Variable<NSDate?> = Variable(nil)
var disabled: Variable<Bool?> = Variable(nil)
var displayName: Variable<String?> = Variable(nil)
var email: Variable<String?> = Variable(nil)
var emailVerified: Variable<Bool?> = Variable(nil)
var locale: Variable<LocaleContainer?> = Variable(nil)
var modified: Variable<NSDate?> = Variable(nil)
var phoneNumber: Variable<String?> = Variable(nil)
var phoneVerified: Variable<Bool?> = Variable(nil)
var userID: Variable<String?> = Variable(nil)
var picture: Variable<UIImage?> = Variable(nil)
var pictureURL: Variable<String?> = Variable(nil)
let disposeBag = DisposeBag()
func set(kiiUser: KiiUser) {
self.username.value = kiiUser.username
self.accessToken.value = kiiUser.accessToken
self.country.value = kiiUser.country
self.created.value = kiiUser.created
self.disabled.value = kiiUser.disabled
self.displayName.value = kiiUser.displayName
self.email.value = kiiUser.email
self.emailVerified.value = kiiUser.emailVerified
self.locale.value = kiiUser.locale
self.modified.value = kiiUser.modified
self.phoneNumber.value = kiiUser.phoneNumber
self.phoneVerified.value = kiiUser.phoneVerified
self.userID.value = kiiUser.userID
self.picture.value = UIImage()
self.pictureURL.value = String()
}
}
我已经尝试将 Singleton 更改为 Variable() 但它不起作用.. 我做错了什么? 抱歉我的英语不好:/
感谢您的帮助
嗨,
我找到了实现 Eureka 库、rxswift 和 MVVM 的解决方案,我将分享它:
设置VC
import UIKit
import SpeedLog
import RxCocoa
import RxSwift
import Eureka
class SettingsVC: FormViewController {
private var viewModel: SettingsVM?
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
self.initUI()
self.bindViewModel()
}
func initUI() {
// Settings form rows
self.form
+++= ButtonRow("connectDisconnect")
<<< ButtonRow("registration")
self.form
+++= Section("Mon compte")
<<< LabelRow("profile")
}
func bindViewModel() {
self.viewModel = SettingsVM(form: &self.form, vc: self)
}
}
设置虚拟机
import Foundation
import SpeedLog
import RxCocoa
import RxSwift
import RxViewModel
import Eureka
class SettingsVM: RxViewModel {
// Private
private let disposeBag = DisposeBag()
let vc: SettingsVC
let connectDisconnectRow: ButtonRow?
let inscriptionRow: ButtonRow?
let myProfileRow: LabelRow?
init(inout form: Form, vc: SettingsVC) {
self.vc = vc
self.connectDisconnectRow = form.rowByTag("connectDisconnect")
self.inscriptionRow = form.rowByTag("registration")
self.myProfileRow = form.rowByTag("profile")
super.init()
self.configureCell()
}
func configureCell() {
// Connexion Disconnect button
connectDisconnectRow?.baseCell.baseRow.title = "Connexion"
connectDisconnectRow?.onCellSelection { cell, row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
UIViewController.pushModal(self.vc, pushModalTo: PushModalTo.Connection)
}
// Inscription button
inscriptionRow?.baseCell.baseRow.title = "Inscription"
inscriptionRow?.onCellSelection { cell, row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
UIViewController.pushModal(self.vc, pushModalTo: PushModalTo.Registration)
}
// My profile button
myProfileRow?.baseCell.baseRow.title = "Mon profil"
myProfileRow?.baseCell.baseRow.baseValue = "Non enregistré"
_ = Session.appUser.displayName.asObservable().subscribeNext { displayName in
if displayName != "" && displayName != nil {
self.myProfileRow?.baseCell.baseRow.baseValue = displayName
}
self.myProfileRow?.baseCell.baseRow.reload()
}
myProfileRow?.onCellSelection { cell, row in
SpeedLog.print((row.cell.textLabel?.text)! + " called")
UIViewController.pushView(self.vc, pushSettingsTo: PushSettingsTo.MyProfile)
}
}
}
我希望它能帮助所有想要实现这些东西的人:) 再见