简体   繁体   中英

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.

 // 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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