小编Rob*_*son的帖子

单元测试和私有变量

我正在为公共方法编写BDD单元测试.该方法更改了一个私有属性(private var),所以我想写一个expect()并确保它被正确设置.由于它是私有的,我无法弄清楚如何从单元测试目标访问它.

对于Objective-C,我只需添加一个扩展头.Swift中有类似的技巧吗?作为注释,该属性还有一个带有一些代码的didSet().

bdd unit-testing swift

36
推荐指数
1
解决办法
9413
查看次数

Flatten TupleViews using SwiftUI

Ok, SwiftUI was released this week so we're all n00bs but... I have the following test code:

var body: some View {
    switch shape {
    case .oneCircle:
        return ZStack {
            Circle().fill(Color.red)
        }
    case .twoCircles:
        return ZStack {
            Circle().fill(Color.green)
            Circle().fill(Color.blue)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

which produces the following error:

Function declares an opaque return type, but the return statements in its body do not have matching underlying types

This happens because the first ZStack is this type:

ZStack<ShapeView<Circle, Color>>
Run Code Online (Sandbox Code Playgroud)

and the second …

ios swift swiftui

3
推荐指数
1
解决办法
400
查看次数

Swift - 时区关闭一小时/秒FromGMT 不正确

这应该是一个非常简单的问题,但我似乎无法理解它。

鉴于我的时区是 EDT (GMT-4),为什么 GMT 的 04:00 变成 23:00 而不是 00:00?

// The offset is -4 hours
let offsetFromGMT = Calendar.current.timeZone.secondsFromGMT() / 60 / 60

// 2017-03-12 04:00
var destinationComponents = DateComponents()
destinationComponents.timeZone = TimeZone(secondsFromGMT: 0)
destinationComponents.year = 2017
destinationComponents.month = 03
destinationComponents.day = 12
destinationComponents.hour = -offsetFromGMT // 4 hours

// Why is this 2017-03-11 23:00 and not 2017-03-12 00:00?
let date = Calendar.current.date(from: destinationComponents)!
// Outputs 23
Calendar.current.dateComponents([.hour], from: date).hour
Run Code Online (Sandbox Code Playgroud)

timezone calendar ios swift

2
推荐指数
1
解决办法
4733
查看次数

标签 统计

swift ×3

ios ×2

bdd ×1

calendar ×1

swiftui ×1

timezone ×1

unit-testing ×1