lytics / ios-sdk

MIT License
0 stars 0 forks source link

Fix Swift 5.9 Test Failures #130

Closed mgacy closed 1 year ago

mgacy commented 1 year ago

Fixes some test failures and warnings with Swift 5.9.

About that UserManagerMock.cast() Method

A minimal demonstration of the crash this method addresses. It only affects test code, not the SDK itself, and is visible when using Xcode 15.0b1 to test on iOS 17.0 but not when using it to test on iOS 16.4 (so it doesn't seem to be a Swift 5.9 thing):

extension Never: Codable {
    public func encode(to encoder: Encoder) throws {
        // no-op
    }

    public init(from decoder: Decoder) throws {
        throw DecodingError.valueNotFound(...)
    }
}

struct UserUpdate<I: Encodable, A: Encodable>: Encodable {
    var identifiers: I?
    var attributes: A?
}

func safeCast<I: Encodable, A: Encodable>(_ update: UserUpdate<I, A>) {
    _ = update as! UserUpdate<Int, Int>
}

func unsafeCast<I: Encodable, A: Encodable>(_ update: UserUpdate<I, A>) {
    _ = update as! UserUpdate<Never, Int>  // Ok on iOS <= 16.x; crash on iOS 17
}

let u1 = UserUpdate<Int, Int>(identifiers: nil, attributes: 5)
safeCast(u1) // no problems

let u2 = UserUpdate<Never, Int>(identifiers: nil, attributes: 5)
_ = u2 as! UserUpdate<Never, Int> // no problems
unsafeCast(u2) // problems