在函数中返回self会让我快速无效

时间:2023-01-23 18:16:02

I have implemented in a playground a GreetingDelegate protocol with a function sayHi (hypothetically)and a class Data that conforms with said protocol.

我已经在游乐场中实现了一个GreetingDelegate协议,它具有一个函数sayHi(假设)和一个符合所述协议的类Data。

I also have a class named Class that has a delegate that conforms to the GreetingDelegate, another sayHi function, and a function that determines if who shall respond to sayHi.

我还有一个名为Class的类,它有一个符合GreetingDelegate的委托,另一个sayHi函数,以及一个确定是谁应该响应sayHi的函数。

If Class doesn't have a delegate triggers his own version of the sayHi function but if the delegate exists triggers the delegate's version of the function.

如果Class没有委托,则触发他自己的sayHi函数版本,但如果委托存在则触发委托的函数版本。

protocol GreetingDelegate {
  func sayHi(name: String) -> String
}

class Class {
  var delegate: GreetingDelegate?

  func sayHi(name: String) -> String {
    return "Hello, \(name)"
  }

  func target() -> Any {
    if let delegate = delegate {
      return delegate
    }
    return self
  }
}

class Data: GreetingDelegate {

  func sayHi(name: String) -> String {
    return "Hi, \(name)"
  }
}

let classInstance = Class()
classInstance.sayHi("Matt")

classInstance.target()

let dataInstance = Data()
dataInstance.sayHi("Matt")

classInstance.delegate = dataInstance
classInstance.sayHi("Matt")

classInstance.target()

The strange part is when I call the target function before assigning any delegate it return nil when it should return itself. Really would appreciate help on this problem.

奇怪的是当我在分配任何委托之前调用目标函数时它返回nil时它应该返回它自己。真的很感激这个问题的帮助。

I attach a picture of the results of the playground if it be any help.在函数中返回self会让我快速无效

如果有任何帮助,我附上操场结果的图片。

UPDATE #1

更新#1

When I change the return type of target() to AnyObject it doesn't return nil but it doesn't accept delegate as a return result.

当我将target()的返回类型更改为AnyObject时,它不会返回nil,但它不接受委托作为返回结果。

2 个解决方案

#1


1  

It's returning {nil} because that's what all of the variables belonging to that instance of Class are (because delegate is an optional, it defaults to nil). If you add another variable, such as

它返回{nil},因为这就是属于该Class实例的所有变量(因为委托是可选的,它默认为nil)。如果添加另一个变量,例如

var num: Int = 1

to the class, it will return {nil, 1} instead. So it is returning the object - not the class name, but basically a summary of its data members.

在课堂上,它将返回{nil,1}。所以它返回对象 - 而不是类名,但基本上是它的数据成员的摘要。

#2


0  

In the first case calling target function before setting the delegate returning self instance with its delegate value as nil. {nil} means instance with delegate value is uninitialised.

在第一种情况下,在设置委托返回自我实例之前调用目标函数,其委托值为nil。 {nil}表示委托值未初始化的实例。

In the second case calling target function after setting the delegate returning self instance with its delegate value as Data object. {Data} means with delegate value is set with a Data object.

在第二种情况下调用目标函数后,设置委托返回自我实例,其委托值为Data对象。 {Data}表示委托值是使用Data对象设置的。

#1


1  

It's returning {nil} because that's what all of the variables belonging to that instance of Class are (because delegate is an optional, it defaults to nil). If you add another variable, such as

它返回{nil},因为这就是属于该Class实例的所有变量(因为委托是可选的,它默认为nil)。如果添加另一个变量,例如

var num: Int = 1

to the class, it will return {nil, 1} instead. So it is returning the object - not the class name, but basically a summary of its data members.

在课堂上,它将返回{nil,1}。所以它返回对象 - 而不是类名,但基本上是它的数据成员的摘要。

#2


0  

In the first case calling target function before setting the delegate returning self instance with its delegate value as nil. {nil} means instance with delegate value is uninitialised.

在第一种情况下,在设置委托返回自我实例之前调用目标函数,其委托值为nil。 {nil}表示委托值未初始化的实例。

In the second case calling target function after setting the delegate returning self instance with its delegate value as Data object. {Data} means with delegate value is set with a Data object.

在第二种情况下调用目标函数后,设置委托返回自我实例,其委托值为Data对象。 {Data}表示委托值是使用Data对象设置的。