[英]Swift - Passing custom objects as a parameter
I started learning Swift today and in my first test app I am getting this error:我今天开始学习 Swift,在我的第一个测试应用程序中,我收到了这个错误:
TestClass
is not convertible toAnotherClass
TestClass
不能转换为AnotherClass
The following is the TestClass :以下是TestClass :
class TestClass : NSObject {
var parameter1 : String = ""
var parameter2 : String = ""
override init() {
super.init()
}
func createJob(parameter1: String, parameter2: String) -> TestClass {
self.parameter1 = parameter1
self.parameter2 = parameter2
return self;
}
}
And this is the AnotherClass :这是另一个类:
class AnotherClass: NSObject {
private struct internalConstants {
static let test1 = "testData"
static let test2 = "testData2"
}
var current : String
override init() {
self.current = internalConstants.test1
super.init()
}
func executeTask(testClass : TestClass) {
if testClass.parameter1 == "abc" {
return;
}
}
}
And this is the ViewController where I am getting the compiler error:这是我收到编译器错误的ViewController :
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let obj = TestClass()
AnotherClass.executeTask(obj)
}
}
AnotherClass.executeTask
line is giving the compiler error. AnotherClass.executeTask
行给出了编译器错误。
The obj
variable sent as a parameter on this line is highlighted by Xcode with the error在此行上作为参数发送的
obj
变量由 Xcode 突出显示,并显示错误
"
TestClass
is not convertible toAnotherClass
".“
TestClass
不能转换为AnotherClass
”。
In C# or Objective C it is allowed to pass custom objects as a parameter to another methods.在 C# 或 Objective C 中,允许将自定义对象作为参数传递给其他方法。 How can I do it in Swift?
我怎样才能在 Swift 中做到这一点?
Let's correct first the TestClass
.让我们首先更正
TestClass
。 This is how you should init a class like that:这是您应该如何初始化这样的类:
class TestClass : NSObject {
....
init(parameter1: String, parameter2: String) {
....
}
}
Much simpler.简单多了。 Now, going back to your problem,
现在,回到你的问题,
"TestClass is not convertible to AnotherClass".
“TestClass 不能转换为 AnotherClass”。
Take a look at it again.再看一看。 The line you've mentioned in your question.
您在问题中提到的那条线。 You are trying to do this:
您正在尝试这样做:
let obj = TestClass()
AnotherClass.executeTask(obj)
This line, AnotherClass.executeTask(obj)
, is giving you an error because indeed executeTask()
is an instance method.这行
AnotherClass.executeTask(obj)
给你一个错误,因为executeTask()
确实是一个实例方法。 You could do three ways for that.你可以做三种方法。
add static
keyword to the func executeTask...
So it becomes like this: static func executeTask(testClass : TestClass) {
添加
static
关键字到func executeTask...
所以它变成这样: static func executeTask(testClass : TestClass) {
Instead of static
keyword, you could add class
.您可以添加
class
,而不是static
关键字。 It becomes like so: class func executeTask(....
它变成这样:
class func executeTask(....
OR, better if you just instantiate the AnotherClass
.或者,如果您只是实例化
AnotherClass
。 Make a new object of AnotherClass
.创建一个
AnotherClass
的新对象。 How to instantiate?如何实例化? You tell me.
你告诉我。 But here:
但在这里:
let anotherClass = AnotherClass()
Either implement executeTask as a class function要么将 executeTask 实现为类函数
class func executeTask(testClass : TestClass) {
if testClass.parameter1 == "abc" {
return;
}
}
or instantiate AnotherClass
in vieweDidLoad
或在
vieweDidLoad
实例化AnotherClass
vieweDidLoad
let obj = TestClass()
let another = AnotherClass()
another.executeTask(testClass: obj)
Note the slightly different call to executeTask
with the argument name.请注意使用参数名称对
executeTask
调用略有不同。
And there is really no reason for you to subclass NSObject as I see it.在我看来,您真的没有理由将 NSObject 子类化。
I think it's best to keep is simple.我认为最好保持简单。 Create an instance of
AnotherClass
inside of ViewController
.在
ViewController
创建另一个AnotherClass
的实例。
class ViewController: UIViewController {
// Create an instance of AnotherClass which lives with ViewController.
var anotherClass = AnotherClass()
override func viewDidLoad() {
super.viewDidLoad()
let obj = TestClass()
// Use the instance of AnotherClass to call the method.
anotherClass.executeTask(testClass: obj)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.