gpt4 book ai didi

ios - 如何使用 rxswift 将信息从一个类发送到另一个类?

转载 作者:行者123 更新时间:2023-11-28 23:25:00 28 4
gpt4 key购买 nike

如何使用 rxswift 将信息从一个 A 类发送到另一个 B 类?在 iOS 中。例如我想发送我的类(class)帐户。

也许 rxswift 根本不适合代表或发送信息。 https://www.reddit.com/r/swift/comments/7pxt4h/rxswift_vs_delegation_which_is_better/

class Account
{
var myId = 0
var Name = "name"

init (myId inputMyId:Int , Name inputName: String)
{
myId = inputMyId
Name = inputName
}
}

class B
{
var myAccount : Account
func receive()
{
// here self.myAccount should be replaced with myA.myAccount
// where myA is object of class A
}
}



class A
{
var myAccount : Account
func send()
{
// here myB.myAccount should be replaced with self.myAccount
// where myB is object of class B
}
}

解决方案1

创建类 C,使 A 和 B 的可观察量相等

class SimpleC
{
func makeAB()
{
var theB : SimpleB = SimpleB()
var theA : SimpleA = SimpleA()
theA.valueAccount = theB.valueAccount
theB.receive()
theA.send()
}
}


class SimpleA
{
var myAccount : Account = Account(myId : 0, Name : "")
var valueAccount = PublishSubject<Account>()
var myB : SimpleB?
func send()
{
// here myB.myAccount should be replaced with self.myAccount
// where myB is object of class B

self.myAccount = Account(myId : 1, Name : "MicrosoftTest5")
self.valueAccount.onNext(self.myAccount)

self.myAccount = Account(myId : 2, Name : "bestWorkTest5")
self.valueAccount.onNext(self.myAccount)

}
}


class SimpleB
{
var myAccount : Account = Account(myId : 0, Name : "")
var valueAccount = PublishSubject<Account>()
let disposer = DisposeBag()
func receive()
{
// here self.myAccount should be replaced with myA.myAccount
// where myA is object of class A
valueAccount.subscribe(onNext: { (newValue) in
print("SimpleB subscribe")
self.myAccount = newValue
var theString = "Account{" + "\(self.myAccount.myId)"
theString = theString + "} {"
theString = theString + "\(self.myAccount.Name)" + "}"
print(theString)
}).addDisposableTo(disposer)
}
}

最佳答案

回答这个问题是希望涵盖两个场景 -

情况 1:在创建对象时传递数据(我可以创建并将其包装在变量中,然后传递),这与 vanilla swift 相同

This holds true in cases of pushing/ presenting as well

class Account
{
var myId = Variable(0)
var name = Variable("")

init (myId inputMyId:Int , Name inputName: String)
{
myId.value = inputMyId
name.value = inputName
}

func passDataAnotherAccount() {
let anotherAccount = AnotherAccount(id: myId, name: name)
anotherAccount.makeChangesToVars()
}

}


class AnotherAccount {

var classListenerOfMyId = Variable(0)
var classListenerOfMyName = Variable("name")

init(id: Variable<Int>, name: Variable<String>) {
self.classListenerOfMyId = id
self.classListenerOfMyName = name
}


func makeChangesToVars() {
self.classListenerOfMyId.value = self.classListenerOfMyId.value + 1
self.classListenerOfMyName.value = Date().description
}

}

案例 2(委托(delegate)和回调的替代方法)

lets say you want to know about changes from class AnotherAccount in previous class, you can make use of OBSERVABLES in that case

将方法从 Account 类更改为这个

func passDataAnotherAccount() {
let anotherAccount = AnotherAccount(id: myId, name: name)
anotherAccount.makeChangesToVars()

// this will observe changes from AnotherAccount class
// we just created above in my current class,
// i.e. Account class
anotherAccount.classListenerOfMyId.asObservable().subscribe(onNext:{[weak self] valueChange in
print(valueChange)
}).disposed(by: DisposeBag())
anotherAccount.classListenerOfMyName.asObservable().subscribe(onNext:{[weak self] valueChange in
print(valueChange)
}).disposed(by: DisposeBag())
}

为了处理委托(delegate)/回调,您需要通过订阅底层 Variables/Subjects/Observable 来观察变化。

注意事项

  1. Please use single dispose bag for one class, unlike me, i have disposed using DisposeBag() itself.
  2. Read More about replacing delegates in RxSwift with Observables (this will be helpful)

关于ios - 如何使用 rxswift 将信息从一个类发送到另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58954962/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com