ios - 如何在同一个swift框架中访问其他类?

标签 ios class swift public

在我的 swift 框架中,我定义了两个类,调用 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() {

    }
 }

为什么不能输出这个错误?

最佳答案

您需要在 A 类中将 var count 的类型设置为 Int

这是你想要的吗?

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

此外,如果您覆盖 init(),请确保您调用 super.init() 以确保父类初始化。

关于ios - 如何在同一个swift框架中访问其他类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27705279/

相关文章:

ios - 从 mapView.annotations 访问 MKAnnotationView

css - 带有 html 类的 swfobject?

ios - 使用 Codable 协议(protocol)仅解码 JSON 的一部分

ios - 如何快速调用另一个类的另一个方法?

ios - 配置文件之间有什么区别?

ios - 如何检测 Swift 2 的 UITextField 上的点击?

c++ - How to?: 将cv::Mat定义为类成员,然后在源代码文件中修改它

c++成员函数声明问题w/class

arrays - 如何迭代函数数组

ios Swift fatal error : use of unimplemented initializer 'init()'