Files
Facundo Menzella 6b5c430082 chore: Enable force_unwrapping for SwiftLint (#4820)
* enable force_unwrapping

* avoid force unwrap

* fixed all errors

* remove extra disable
2025-02-26 12:40:49 +01:00

54 lines
1.8 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// UserDefaults+Extensions.swift
//
// Created by Nacho Soto on 11/9/22.
import Foundation
extension UserDefaults {
// swiftlint:disable force_unwrapping
// These are the only 2 documented reasons why `.init(suiteName:)` might return `nil`:
// - "Because a suite manages the defaults of a specified app group, a suite name
// must be distinct from your app’s main bundle identifier.
// - The globalDomain is also an invalid suite name, because it isn't writeable by apps.
//
// Because we know at compile time that it's neither of those, this is a safe force-unwrap.
static let revenueCatSuite: UserDefaults = .init(suiteName: UserDefaults.revenueCatSuiteName)!
// swiftlint:enable force_unwrapping
private static let revenueCatSuiteName = "com.revenuecat.user_defaults"
}
extension UserDefaults {
/// Determines the "default" `UserDefaults` to use for the SDK.
///
/// Moving foward, this default is `UserDefaults.revenueCatSuite`,
/// but existing users will continue using `UserDefaults.standard` for compatibility.
/// This is determined by the presence of an app user ID in `UserDefaults.standard`.
static func computeDefault() -> UserDefaults {
let standard: UserDefaults = .standard
if standard.value(forKey: DeviceCache.CacheKeys.appUserDefaults.rawValue) != nil {
Logger.debug(Strings.configure.using_user_defaults_standard)
return standard
} else {
Logger.debug(Strings.configure.using_user_defaults_suite_name)
return .revenueCatSuite
}
}
}