sunshinejr / SwiftyUserDefaults

Modern Swift API for NSUserDefaults
http://radex.io/swift/nsuserdefaults/static
MIT License
4.85k stars 365 forks source link

Building stuck #239

Closed klconur closed 4 years ago

klconur commented 4 years ago

It is working like a virus, man. I lost totally of 28 hours. I had too much legacy library on my project. Xcode was not compiling the project when I upgraded to the latest version to all libraries. (pod update)

Lastly, I found the mistake. I downgraded to the 4.0.0 version and it works.

Give me my 28 hours back :)

sunshinejr commented 4 years ago

Hey @klconur, sorry to hear that. Can you add your stack (Swift/Xcode version) so I could investigate more? Did you also migrate using the guide?

klconur commented 4 years ago

Hi @sunshinejr

Thank you for your reply.

Yes, I tried the guide but the project was not compiled. Maybe I could not implement it correctly.

I have seen this bug, this is not your fault actually.

Here is my code, please can you check it? It is working with pod 'SwiftyUserDefaults', '~> 4.0'

Xcode 11.3.1 Swift 5 iOS 13.2

extension DefaultsKeys {
    static let accessToken = DefaultsKey<String?>("access_token")
    static let userId = DefaultsKey<Int?>("user_id")
    static let deviceId = DefaultsKey<Int?>("device_id")
    static let userName = DefaultsKey<String?>("user_name")
    //TODO
    //static let storedFiles = DefaultsKey<[FileModel]>("storedFiles")
    static let pushToken = DefaultsKey<String?>("push_token")
    static let voipToken = DefaultsKey<String?>("voip_token")
    static let notificationCount = DefaultsKey<Int?>("notification_count")
    static let minSupportedVersion = DefaultsKey<String?>("minSupportedVersion")
}

extension UserDefaults {
    /*subscript(key: DefaultsKey<[FileModel]>) -> [FileModel] {
        get { return unarchive(key) ?? [] }
        set { archive(key, newValue) }
    }
    */
}

class DataManager {

    static var hasLoggedInUser: Bool {
        get {
            return Defaults[.userId] != nil && Defaults[.userName] != nil
        }
    }

    static var minSupportedVersion: String? {
        get {
            return Defaults[.minSupportedVersion]
        }

        set {
            Defaults[.minSupportedVersion] = newValue
        }
    }

    static var userId: Int? {
        get {
            return Defaults[.userId]
        }

        set {
            Defaults[.userId] = newValue
        }
    }

    static var deviceId: Int? {
        get {
            return Defaults[.deviceId]
        }

        set {
            Defaults[.deviceId] = newValue
        }
    }

    static var userName: String? {
        get {
            return Defaults[.userName]
        }

        set {
            Defaults[.userName] = newValue
        }
    }

    static var pushToken: String? {
        get {
            return Defaults[.pushToken]
        }

        set {
            Defaults[.pushToken] = newValue
        }
    }

    static var voipToken: String? {
        get {
            return Defaults[.voipToken]
        }

        set {
            Defaults[.voipToken] = newValue
        }
    }

    static var notificationCount: Int? {
        get {
            return Defaults[.notificationCount]
        }

        set {
            Defaults[.notificationCount] = newValue
        }
    }

    static var accessTokenHeaderUrlEncoded: HTTPHeaders {

        get {
            var header = ["Content-Type" : "application/x-www-form-urlencoded"]

            if let token = Defaults[.accessToken] {
                header["Authorization"] = "Bearer \(token)"
            }

            header["Accept-Language"] = Locale.current.languageCode
            header["User-Agent"] = "iphone"
            header["Gmt"] = TimeZone.current.abbreviation()
            return header
        }
    }
}
sunshinejr commented 4 years ago

@klconur try this, but I wrote it by hand without compiler so it might have some errors:

extension DefaultsKeys {
    var accessToken: DefaultsKey<String?> { .init("access_token") }
    var userId: DefaultsKey<Int?> { .init("user_id") }
    var deviceId: DefaultsKey<Int?> { .init("device_id") }
    static let userName: DefaultsKey<String?> { .init("user_name") }
    //TODO
    //var storedFiles: DefaultsKey<[FileModel]> { .init("storedFiles") }
    var pushToken: DefaultsKey<String?> { .init("push_token") }
    var voipToken: DefaultsKey<String?> { .init("voip_token") }
    var notificationCount: DefaultsKey<Int?> { .init("notification_count") }
    var minSupportedVersion: DefaultsKey<String?> { .init("minSupportedVersion") }
}

class DataManager {

    static var hasLoggedInUser: Bool {
        get {
            return Defaults[\.userId] != nil && Defaults[\.userName] != nil
        }
    }

    static var minSupportedVersion: String? {
        get {
            return Defaults[\.minSupportedVersion]
        }

        set {
            Defaults[\.minSupportedVersion] = newValue
        }
    }

    static var userId: Int? {
        get {
            return Defaults[\.userId]
        }

        set {
            Defaults[\.userId] = newValue
        }
    }

    static var deviceId: Int? {
        get {
            return Defaults[\.deviceId]
        }

        set {
            Defaults[\.deviceId] = newValue
        }
    }

    static var userName: String? {
        get {
            return Defaults[\.userName]
        }

        set {
            Defaults[\.userName] = newValue
        }
    }

    static var pushToken: String? {
        get {
            return Defaults[\.pushToken]
        }

        set {
            Defaults[\.pushToken] = newValue
        }
    }

    static var voipToken: String? {
        get {
            return Defaults[\.voipToken]
        }

        set {
            Defaults[\.voipToken] = newValue
        }
    }

    static var notificationCount: Int? {
        get {
            return Defaults[\.notificationCount]
        }

        set {
            Defaults[\.notificationCount] = newValue
        }
    }

    static var accessTokenHeaderUrlEncoded: HTTPHeaders {

        get {
            var header = ["Content-Type" : "application/x-www-form-urlencoded"]

            if let token = Defaults[\.accessToken] {
                header["Authorization"] = "Bearer \(token)"
            }

            header["Accept-Language"] = Locale.current.languageCode
            header["User-Agent"] = "iphone"
            header["Gmt"] = TimeZone.current.abbreviation()
            return header
        }
    }
}

if it still has problems, please comment the lines in DataManager:

            if let token = Defaults[\.accessToken] {
                header["Authorization"] = "Bearer \(token)"
            }

this should be resolved in the official build so not sure why it would break again, though. Make sure you use 5.0.0 version, not a beta/alpha.

klconur commented 4 years ago

Thank you @sunshinejr

It works. You saved my life. 👏👏👏

sunshinejr commented 4 years ago

@klconur awesome! I'm closing the issue in that case but let me know if you still have any problems.

have a great day & stay safe 🍀