如果类属性是弱的或者不是弱的,那么在XCTest中是否可以测试它。
class A {
weak var p: String? = nil
}结果:如果类的p属性是弱的,则断言为
发布于 2017-12-08 15:59:10
您可以使用这样的方法:
class TestObject {}
protocol A {
var a: TestObject? { get set }
}
class B: A {
var a: TestObject?
}
class C: A {
weak var a: TestObject?
}
func addVar(to: A) {
var target = to
target.a = TestObject() // Once we leave the scope of this function, the TestObject instance created here will be released unless retained by target
}
let b = B()
let c = C()
addVar(to: b)
addVar(to: c)
print(b.a) // prints Optional(TestObject) because class C uses a strong var for a
print(c.a) // prints nil because class B uses a weak var for a转换为测试用例时,它可能如下所示:
func testNotWeak() {
func addVar(to: A) {
var target = to
target.a = TestObject() // Once we leave the scope of this function, the TestObject instance created here will be released unless retained by target
}
let testClass = B()
addVar(to: testClass)
XCTAssertNotNil(testClass.a)
}https://stackoverflow.com/questions/47717567
复制相似问题