swift - 在 Playground 中测试委托(delegate)给 'nil'

标签 swift swift-playground delegation

我在 Playground 中有以下代码 - 我正在学习委托(delegate) - ...

import UIKit

protocol FollowThisProtocol {

    func passingTheValue(aValue: String)
}


class IPassTheValues{

    var aDelegate: FollowThisProtocol!

    func runThisFunc(){

       aDelegate.passingTheValue(aValue: "I like this game")

    }

}


class IReceiveTheValues: FollowThisProtocol{

    var localString: String!
        var instanceOfClass: IPassTheValues!

    func runReceivefunc(){

            instanceOfClass.aDelegate = self

    }

    func passingTheValue(aValue: String) {
        localString = aValue

    }

}

当我尝试

print(IReceiveTheValues().localString)

它给了我nil

如果我在尝试 print(IReceiveTheValues().localString) 之前运行以下行,它也会给我 nil...

IPassTheValues()   

IReceiveTheValues()

你能帮我理解为什么值没有从第 1 类传递到第 2 类吗?

或者,如果您能在我的代码中发现一些自相矛盾的地方,请您指出...?

感谢您的宝贵时间和帮助。

最佳答案

您需要在将自己分配为delegate 之前创建IPassTheValues 对象,然后在实例上调用runThisFunc():

func runReceivefunc(){

    instanceOfClass = IPassTheValues()
    instanceOfClass.aDelegate = self
    instanceOfClass.runThisFunc()

}

然后测试:

// Create the `IReceiveTheValues` object
let irtv = IReceiveTheValues()

// Run the method
irtv.runReceivefunc()

// Get the resulting string
print(irtv.localString)

我建议进行另外 2 项更改。让你的 delegate weak 这样你就不会得到一个导致无法删除任何一个对象的保留周期。为此,您需要将 : class 添加到您的协议(protocol)声明中,因为只有引用对象(class 的实例)可以是 weak.

这是修改后的代码。尝试一下,看看当您删除 weak 时会发生什么。

protocol FollowThisProtocol: class {
    func passingTheValue(aValue: String)
}

class IPassTheValues{
    weak var aDelegate: FollowThisProtocol!

    func runThisFunc(){
        print("Calling delegate...")
        aDelegate.passingTheValue(aValue: "I like this game")
    }

    deinit {
        print("IPassTheValues deinitialized")
    }
}


class IReceiveTheValues: FollowThisProtocol{
    var localString: String!
    var instanceOfClass: IPassTheValues!

    func runReceivefunc(){
        instanceOfClass = IPassTheValues()
        instanceOfClass.aDelegate = self
        instanceOfClass.runThisFunc()
    }

    func passingTheValue(aValue: String) {
        print("Receiving value from helper object...")
        localString = aValue
    }

    deinit {
        print("IReceiveTheValues deinitialized")
    }
}

func test() {
    let irtv = IReceiveTheValues()
    irtv.runReceivefunc()
    print(irtv.localString)
}

test()

关于swift - 在 Playground 中测试委托(delegate)给 'nil',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44974465/

相关文章:

swift - 即使在设备旋转后也约束/居中 CAEmitterLayer

c++ - 您使用什么模式来分离 C++ 中的接口(interface)和实现?

ios - iPhone Xs - 使用 AVCaptureVideoPreviewLayer 时,为什么 UIView 的顶部边框和安全区域的顶部边框之间有巨大的填充?

ios - UINavigationBar 不在 Playground 中显示项目部件

ios - 如何在解析中从对象中获取 ObjectId 并在 swift 中将其显示为字符串

swift - 如何使用 playground 在 Swift 中读取和写入数据到文本文件?

java - 如何将我的 ClassLoader 设置为 JVM 的 ClassLoader 来加载所有类(包括 jar 类)?

ios - 代表无法调用第二个 View Controller

swift - 如何使用根据节点移动方式运行的 .xscale?

swift - 如何在 NSTextView 中捕获 Alt + 向上箭头事件