Skip to content

Commit fae057c

Browse files
committed
Fix the _other_ place where 'as AnyObject' was being used by refactoring this bit of code up.
1 parent ac6a3f6 commit fae057c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

Foundation/UserDefaults.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,19 @@ import CoreFoundation
1212
private var registeredDefaults = [String: Any]()
1313
private var sharedDefaults = UserDefaults()
1414

15+
fileprivate func bridgeFromNSCFTypeIfNeeded(_ value: Any) -> Any {
16+
// This line will produce a 'Conditional cast always succeeds' warning if compoiled on Darwin, since Darwin has bridging casts of any value to an object,
17+
// but is required for non-Darwin to work correctly, since that platform _doesn't_ have bridging casts of that kind for now.
18+
if let object = value as? AnyObject {
19+
return _SwiftValue.fetch(nonOptional: object)
20+
} else {
21+
return value
22+
}
23+
}
24+
1525
open class UserDefaults: NSObject {
1626
static private func _isValueAllowed(_ nonbridgedValue: Any) -> Bool {
17-
let value = _SwiftValue.fetch(nonOptional: nonbridgedValue as AnyObject)
27+
let value = bridgeFromNSCFTypeIfNeeded(nonbridgedValue)
1828

1929
if let value = value as? [Any] {
2030
for innerValue in value {
@@ -268,15 +278,7 @@ open class UserDefaults: NSObject {
268278
}
269279

270280
open func register(defaults registrationDictionary: [String : Any]) {
271-
registeredDefaults.merge(registrationDictionary.mapValues { value in
272-
// This line will produce a 'Conditional cast always succeeds' warning on Darwin, since Darwin has bridging casts of any value to an object,
273-
// but is required for non-Darwin to work correctly, since that platform _doesn't_ have bridging casts of that kind for now.
274-
if let object = value as? AnyObject {
275-
return _SwiftValue.fetch(nonOptional: object)
276-
} else {
277-
return value
278-
}
279-
}, uniquingKeysWith: { $1 })
281+
registeredDefaults.merge(registrationDictionary.mapValues(bridgeFromNSCFTypeIfNeeded), uniquingKeysWith: { $1 })
280282
}
281283

282284
open func addSuite(named suiteName: String) {

0 commit comments

Comments
 (0)