简体   繁体   English

如何在同一个快速框架中访问其他类?

[英]how to access other class in same swift framwork?

In my swift framework, I define two class , call A and B, but it build for error. 在我的快速框架中,我定义了两个类,分别称为A和B,但它是为错误而构建的。

 // in a.swift
 public class A : NSObject {
    public var count
    public override init() {
          count = 10
    }
 }

 // in b.swift
 public class B : NSObject {
    public func getACount(a:A) {     // error : use undeclare type : A
        println(a.count)
    }
    public override init() {

    }
 }

why it cannot output this error? 为什么无法输出此错误?

You need to set the type of var count to Int in class A . 您需要在A类中将var count的类型设置为Int

Is this what you want? 这是你想要的吗?

public class A : NSObject {
    public var count: Int
    public override init() {
        count = 10
        super.init()
    }
}

public class B : NSObject {
    public func getACount(a:A) {     // error : use undeclare type : A
        println(a.count)
    }
}


var b = B()
var a = A()
var aa = A()
aa.count = 123


b.getACount(a) // prints 10
b.getACount(aa) // prints 123

Also if you override an init() make sure you call super.init() to make sure the parent class initialises. 另外,如果您覆盖init()确保调用super.init()以确保父类初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM