2

我有一个简单的扩展UITraitCollection

extension UITraitCollection {

    var isIpad: Bool {
        return horizontalSizeClass == .Regular && verticalSizeClass == .Regular
    }
}

之前,我需要为此编写测试(使用 TDD):

let traitCollection = UITraitCollection(verticalSizeClass: .Regular) //the init is either for vertical or horizontal at time
traitCollection.horizontalSizeClass = .Regular //error

XCTAssertTrue(traitCollection.isIpad)

但错误是:

不能分配给属性:horizontalSizeClass是一个只能获取的属性。

那我该怎么办?

4

1 回答 1

3

好的,我找到了解决方案:

let compactTraitCollection = UITraitCollection(traitsFromCollections: [UITraitCollection(verticalSizeClass: .Compact), UITraitCollection(horizontalSizeClass: .Compact)])
XCTAssertFalse(compactTraitCollection.isIpad)

let regularTraitCollection = UITraitCollection(traitsFromCollections: [UITraitCollection(verticalSizeClass: .Regular), UITraitCollection(horizontalSizeClass: .Regular)])
XCTAssertTrue(regularTraitCollection.isIpad)
于 2016-07-14T06:41:11.520 回答