我一直在对我的模型进行一些测试,以确保当我将它们编码为 JSON 然后使用JSONEncoder/Decoder. 然而,我的一项测试失败了,罪魁祸首是UIImage。我已确保在编码/解码过程中没有抛出任何错误。
首先,这是有问题的测试:
func testProfileImageCodable() throws {
let image = ProfileImage(UIImage(systemName: "applelogo")!)
try XCTAssertTrue(assertCodable(image))
}
Run Code Online (Sandbox Code Playgroud)
这是我的“可编码性”测试,我确保编码/解码之前和之后类型相等:
func assertCodable<T: Codable & Equatable>(
_ value: T,
decoder: JSONDecoder = .init(),
encoder: JSONEncoder = .init()
) throws -> Bool {
let encoded = try encoder.encode(value)
let decoded = try decoder.decode(T.self, from: encoded)
return value == decoded
}
Run Code Online (Sandbox Code Playgroud)
首先,这是我的UIImage工作方式Codable:
extension KeyedEncodingContainer {
mutating func encode(_ value: UIImage, forKey key: Key) throws {
guard let …Run Code Online (Sandbox Code Playgroud) 我遇到一个问题,我试图对满足特定条件时出现的按钮使用转换。该按钮覆盖在另一个视图的顶部,并固定在底部。我想让它出现时从底部向上滑动,消失时向下滑动。
为此,我会使用.transition(.move(edge: .bottom).animation(.linear)). 然而,它根本不起作用——它只是弹出来,然后弹出来,没有任何滑动或动画。
.opacity.animation(.linear)更奇怪的是,当我使用或时,过渡确实有效.scale.animation(.linear),但不适用于任何其他过渡。
代码如下所示:
var body: some View {
ScrollView {
// ...
}
.overlay(alignment: .bottom) {
if condition {
Button(action: { /* ... */ }) {
Text("Share")
}
.background { LinearGradient(...) }
.clipShape(Capsule())
.transition(.move(edge: .bottom).animation(.linear))
}
}
}
Run Code Online (Sandbox Code Playgroud)
知道为什么转换只能与.opacity和 一起使用.scale吗?我如何才能使其与 一起使用.move(edge: .bottom)?