Ios 在Swift中发出函数调用

Ios 在Swift中发出函数调用,ios,swift,function,function-call,Ios,Swift,Function,Function Call,在Swift中调用函数时遇到问题。 乍一看,它看起来像是普通的代码(意思是类似于我过去所做的没有问题的事情)。但是在发布编译器消息时,我肯定遗漏了一些微妙之处 首先是我得到的错误: 'NSManagedObject' is not convertible to 'MyViewController' 然后遵循相关代码: 1) 包含被调用函数的类: import UIKit class MyViewController: UIViewController { .... func

在Swift中调用函数时遇到问题。 乍一看,它看起来像是普通的代码(意思是类似于我过去所做的没有问题的事情)。但是在发布编译器消息时,我肯定遗漏了一些微妙之处

首先是我得到的错误:

'NSManagedObject' is not convertible to 'MyViewController'
然后遵循相关代码:

1) 包含被调用函数的类:

import UIKit

class MyViewController: UIViewController {
    ....
    func myVCFunction(_ theData: NSManagedObject) {
        .... // Do some useful work.
    }
}
2) 包含另一个函数的文件,调用第一个函数(在1中提到)

我不明白错误信息。 为什么“NSManagedObject”在此处应转换为“MyViewController”


如果有人能指出我遗漏了什么,我会很高兴。

哦!非常正确。我完全错过了。我现在使用这种语法:“MyViewController().myVCFunction(dataUnit)”并且它可以工作。谢谢你的提醒。
Create an instance of MyViewController like below:

 import Foundation
    import UIKit

    func otherCustomFunc(_ dataUnit: NSManagedObject) {
        let myViewController = MyViewController()
        myViewController.myVCFunction(dataUnit)
    }
Create an instance of MyViewController like below:

 import Foundation
    import UIKit

    func otherCustomFunc(_ dataUnit: NSManagedObject) {
        let myViewController = MyViewController()
        myViewController.myVCFunction(dataUnit)
    }