Skip to content

Commit dbee05d

Browse files
committed
Without CF imported directly, one cannot use 'as' casts with CF types. Replace with unsafeBitCast() or other means as needed.
1 parent 75f3bac commit dbee05d

10 files changed

+28
-20
lines changed

Sources/Foundation/Bundle.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private func _getTypeContextDescriptor(of cls: AnyClass) -> UnsafeRawPointer
1515
open class Bundle: NSObject {
1616
private var _bundleStorage: AnyObject!
1717
private var _bundle: CFBundle! {
18-
get { _bundleStorage as! CFBundle? }
18+
get { unsafeBitCast(_bundleStorage, to: CFBundle?.self) }
1919
set { _bundleStorage = newValue }
2020
}
2121

Sources/Foundation/DateIntervalFormatter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal extension _CFDateIntervalFormatterBoundaryStyle {
8585
open class DateIntervalFormatter: Formatter {
8686
var _core: AnyObject
8787
var core: CFDateIntervalFormatter {
88-
get { _core as! CFDateIntervalFormatter }
88+
get { unsafeBitCast(_core, to: CFDateIntervalFormatter.self) }
8989
set { _core = newValue }
9090
}
9191

Sources/Foundation/NSCFCharacterSet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ internal func _CFSwiftCharacterSetCharacterIsMember(_ cset: CFTypeRef, _ ch: Un
9393
}
9494

9595
internal func _CFSwiftCharacterSetMutableCopy(_ cset: CFTypeRef) -> Unmanaged<CFMutableCharacterSet> {
96-
return Unmanaged.passRetained((cset as! NSCharacterSet).mutableCopy() as! CFMutableCharacterSet)
96+
return Unmanaged.passRetained(unsafeBitCast((cset as! NSCharacterSet).mutableCopy(), to: CFMutableCharacterSet.self))
9797
}
9898

9999
internal func _CFSwiftCharacterSetLongCharacterIsMember(_ cset: CFTypeRef, _ ch:UInt32) -> Bool {

Sources/Foundation/NSKeyedArchiver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ open class NSKeyedArchiver : NSCoder {
223223
success = true
224224
}
225225
} else {
226-
let stream = self._stream as! CFWriteStream
226+
let stream = unsafeBitCast(self._stream, to: CFWriteStream.self)
227227
success = CFPropertyListWrite(plist, stream, kCFPropertyListXMLFormat_v1_0, 0, nil) > 0
228228
}
229229

Sources/Foundation/NSRegularExpression.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension NSRegularExpression {
3030
open class NSRegularExpression: NSObject, NSCopying, NSSecureCoding {
3131
internal var _internalStorage: AnyObject
3232
internal var _internal: _CFRegularExpression {
33-
_internalStorage as! _CFRegularExpression
33+
unsafeBitCast(_internalStorage, to: _CFRegularExpression.self)
3434
}
3535

3636
open override func copy() -> Any {

Sources/Foundation/NSURL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ open class NSURLQueryItem : NSObject, NSSecureCoding, NSCopying {
11651165

11661166
open class NSURLComponents: NSObject, NSCopying {
11671167
private let _componentsStorage : AnyObject!
1168-
private var _components: CFURLComponents! { _componentsStorage as! CFURLComponents? }
1168+
private var _components: CFURLComponents! { unsafeBitCast(_componentsStorage, to: CFURLComponents?.self) }
11691169

11701170
open override func copy() -> Any {
11711171
return copy(with: nil)

Sources/Foundation/Process.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,16 +334,24 @@ open class Process: NSObject {
334334
}
335335
}
336336

337-
private var _runLoopSourceContextStorage: Any? = nil
337+
private class NonexportedCFRunLoopSourceContextStorage {
338+
internal var value: CFRunLoopSourceContext?
339+
}
340+
341+
private class NonexportedCFRunLoopSourceStorage {
342+
internal var value: CFRunLoopSource?
343+
}
344+
345+
private var _runLoopSourceContextStorage = NonexportedCFRunLoopSourceContextStorage()
338346
private var runLoopSourceContext: CFRunLoopSourceContext? {
339-
get { _runLoopSourceContextStorage as! CFRunLoopSourceContext? }
340-
set { _runLoopSourceContextStorage = newValue }
347+
get { _runLoopSourceContextStorage.value }
348+
set { _runLoopSourceContextStorage.value = newValue }
341349
}
342350

343-
private var _runLoopSourceStorage: Any? = nil
351+
private var _runLoopSourceStorage = NonexportedCFRunLoopSourceStorage()
344352
private var runLoopSource: CFRunLoopSource? {
345-
get { _runLoopSourceStorage as! CFRunLoopSource? }
346-
set { _runLoopSourceStorage = newValue }
353+
get { _runLoopSourceStorage.value }
354+
set { _runLoopSourceStorage.value = newValue }
347355
}
348356

349357
fileprivate weak var runLoop : RunLoop? = nil

Sources/Foundation/RunLoop.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal func _NSRunLoopNew(_ cf: CFRunLoop) -> Unmanaged<AnyObject> {
5555
open class RunLoop: NSObject {
5656
internal var _cfRunLoopStorage : AnyObject!
5757
internal var _cfRunLoop: CFRunLoop! {
58-
get { _cfRunLoopStorage as! CFRunLoop? }
58+
get { unsafeBitCast(_cfRunLoopStorage, to: CFRunLoop?.self) }
5959
set { _cfRunLoopStorage = newValue }
6060
}
6161

@@ -280,7 +280,7 @@ extension RunLoop {
280280
@available(*, deprecated, message: "For XCTest use only.")
281281
public class _Observer {
282282
fileprivate let _cfObserverStorage: AnyObject
283-
fileprivate var cfObserver: CFRunLoopObserver { _cfObserverStorage as! CFRunLoopObserver }
283+
fileprivate var cfObserver: CFRunLoopObserver { unsafeBitCast(_cfObserverStorage, to: CFRunLoopObserver.self) }
284284

285285
fileprivate init(activities: _Activities, repeats: Bool, order: Int, handler: @escaping (_Activity) -> Void) {
286286
self._cfObserverStorage = CFRunLoopObserverCreateWithHandler(kCFAllocatorSystemDefault, CFOptionFlags(activities.rawValue), repeats, CFIndex(order), { (cfObserver, cfActivity) in
@@ -309,7 +309,7 @@ extension RunLoop {
309309
@available(*, deprecated, message: "For XCTest use only.")
310310
open class _Source: NSObject {
311311
fileprivate var _cfSourceStorage: AnyObject!
312-
fileprivate var cfSource: CFRunLoopSource { _cfSourceStorage as! CFRunLoopSource }
312+
fileprivate var cfSource: CFRunLoopSource { unsafeBitCast(_cfSourceStorage, to: CFRunLoopSource.self) }
313313

314314

315315
public init(order: Int = 0) {

Sources/Foundation/Stream.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ open class InputStream: Stream {
108108
case cantSeekInputStream
109109
}
110110

111-
internal let _streamStorage: Any!
112-
internal var _stream: CFReadStream { _streamStorage as! CFReadStream }
111+
internal let _streamStorage: AnyObject!
112+
internal var _stream: CFReadStream { unsafeBitCast(_streamStorage, to: CFReadStream.self) }
113113

114114
// reads up to length bytes into the supplied buffer, which must be at least of size len. Returns the actual number of bytes read.
115115
open func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int {
@@ -185,8 +185,8 @@ open class InputStream: Stream {
185185
// Currently this is left as named OutputStream due to conflicts with the standard library's text streaming target protocol named OutputStream (which ideally should be renamed)
186186
open class OutputStream : Stream {
187187

188-
internal let _streamStorage: Any!
189-
internal var _stream: CFWriteStream { _streamStorage as! CFWriteStream }
188+
internal let _streamStorage: AnyObject!
189+
internal var _stream: CFWriteStream { unsafeBitCast(_streamStorage, to: CFWriteStream.self) }
190190

191191
// writes the bytes from the specified buffer to the stream up to len bytes. Returns the number of bytes actually written.
192192
open func write(_ buffer: UnsafePointer<UInt8>, maxLength len: Int) -> Int {

Sources/Foundation/Timer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ open class Timer : NSObject {
2626
}
2727

2828
internal var _timerStorage: AnyObject?
29-
internal var _timer: CFRunLoopTimer? { _timerStorage as! CFRunLoopTimer? } // has to be optional because this is a chicken/egg problem with initialization in swift
29+
internal var _timer: CFRunLoopTimer? { unsafeBitCast(_timerStorage, to: CFRunLoopTimer?.self) } // has to be optional because this is a chicken/egg problem with initialization in swift
3030
internal var _fire: (Timer) -> Void = { (_: Timer) in }
3131

3232
/// Alternative API for timer creation with a block.

0 commit comments

Comments
 (0)