You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
we just evaluated using your library "MultiSelectSegmentedControl". After implementing it we suddenly noticed some crashes which we could trace back to the extension included in this library Codable+Sweeter.
In this file you're overloading Decodable with a new init:
extension Decodable {
/// Sweeter: Create object from a dictionary
public init?(from: Any) {
guard let data = try? JSONSerialization.data(withJSONObject: from, options: .prettyPrinted) else { return nil }
guard let decodedSelf = try? JSONDecoder().decode(Self.self, from: data) else { return nil }
self = decodedSelf
}
}
As you're overloading the init with type from: Any this is being used unintentionally by other implementations.
For example, we're also using the library R.swift which generates the following line to use when passing arguments to translations:
fileprivate static let applicationLocale = hostingBundle.preferredLocalizations.first.flatMap(Locale.init) ?? Locale.current
After adding your Library this line (Locale.init in particular) is using your overloaded init instead of the default Codable one. And this causes the app to crash as, of course, this is not passing any valid JSON as an argument.
Normally it should use
extension Locale : Codable {
/// Creates a new instance by decoding from the given decoder.
///
/// This initializer throws an error if reading from the decoder fails, or
/// if the data read is corrupted or otherwise invalid.
///
/// - Parameter decoder: The decoder to read data from.
public init(from decoder: Decoder) throws
/// Encodes this value into the given encoder.
///
/// If the value fails to encode anything, `encoder` will encode an empty
/// keyed container in its place.
///
/// This function throws an error if any values are invalid for the given
/// encoder's format.
///
/// - Parameter encoder: The encoder to write data to.
public func encode(to encoder: Encoder) throws
}
I guess in general it is pretty dangerous to overload something with type Any but maybe this can also be fixed by using another Keyword instead of from:.
The text was updated successfully, but these errors were encountered:
Good point! If you change the constructor in your copy of Codable+Sweeter to
publicinit?(dictionary:[String:Any]){
guard let data =try?JSONSerialization.data(withJSONObject: dictionary, options:.prettyPrinted)else{returnnil}
guard let decodedSelf =try?JSONDecoder().decode(Self.self, from: data)else{returnnil}self= decodedSelf
}
Does it solve the problem for you? If it does, I'll change it here in master.
Description of the problem:
Hi,
we just evaluated using your library "MultiSelectSegmentedControl". After implementing it we suddenly noticed some crashes which we could trace back to the extension included in this library
Codable+Sweeter
.In this file you're overloading
Decodable
with a new init:As you're overloading the init with type
from: Any
this is being used unintentionally by other implementations.For example, we're also using the library
R.swift
which generates the following line to use when passing arguments to translations:After adding your Library this line (
Locale.init
in particular) is using your overloaded init instead of the default Codable one. And this causes the app to crash as, of course, this is not passing any valid JSON as an argument.Normally it should use
I guess in general it is pretty dangerous to overload something with type
Any
but maybe this can also be fixed by using another Keyword instead offrom:
.The text was updated successfully, but these errors were encountered: