Eliminate most remaining uses of _convertNSFooToFoo and _convertFooToNSFoo.

Generalized bridging has fully subsumed most of these. NSError is
still special, and _convertStringToNSString remains for the the
runtime's implementation of SwiftObject's -description method.
This commit is contained in:
Doug Gregor
2016-03-18 10:31:30 -07:00
parent 4c49e67780
commit d92ae77076
11 changed files with 103 additions and 213 deletions

View File

@@ -75,16 +75,6 @@ func _convertStringToNSString(string: String) -> NSString {
return string._bridgeToObjectiveC()
}
@warn_unused_result
@_semantics("convertFromObjectiveC")
public // COMPILER_INTRINSIC
func _convertNSStringToString(nsstring: NSString?) -> String {
if nsstring == nil { return "" }
var result: String?
String._forceBridgeFromObjectiveC(nsstring!, result: &result)
return result!
}
extension NSString : StringLiteralConvertible {
/// Create an instance initialized to `value`.
public required convenience init(unicodeScalarLiteral value: StaticString) {
@@ -461,33 +451,6 @@ extension NSArray : ArrayLiteralConvertible {
}
}
/// The entry point for converting `NSArray` to `Array` in bridge
/// thunks. Used, for example, to expose :
///
/// func f([NSView]) {}
///
/// to Objective-C code as a method that accepts an `NSArray`. This operation
/// is referred to as a "forced conversion" in ../../../docs/Arrays.rst
@warn_unused_result
@_semantics("convertFromObjectiveC")
public func _convertNSArrayToArray<T>(source: NSArray?) -> [T] {
if _slowPath(source == nil) { return [] }
var result: [T]?
Array._forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
/// The entry point for converting `Array` to `NSArray` in bridge
/// thunks. Used, for example, to expose :
///
/// func f() -> [NSView] { return [] }
///
/// to Objective-C code as a method that returns an `NSArray`.
@warn_unused_result
public func _convertArrayToNSArray<T>(array: [T]) -> NSArray {
return array._bridgeToObjectiveC()
}
extension Array : _ObjectiveCBridgeable {
/// Private initializer used for bridging.
@@ -626,54 +589,6 @@ extension Dictionary {
}
}
/// The entry point for bridging `NSDictionary` to `Dictionary` in bridge
/// thunks. Used, for example, to expose:
///
/// func f([String : String]) {}
///
/// to Objective-C code as a method that accepts an `NSDictionary`.
///
/// This is a forced downcast. This operation should have O(1) complexity
/// when `Key` and `Value` are bridged verbatim.
///
/// The cast can fail if bridging fails. The actual checks and bridging can be
/// deferred.
@warn_unused_result
@_semantics("convertFromObjectiveC")
public func _convertNSDictionaryToDictionary<
Key : Hashable, Value
>(d: NSDictionary?) -> [Key : Value] {
// Note: there should be *a good justification* for doing something else
// than just dispatching to `_forceBridgeFromObjectiveC`.
if _slowPath(d == nil) { return [:] }
var result: [Key : Value]?
Dictionary._forceBridgeFromObjectiveC(d!, result: &result)
return result!
}
// FIXME: right now the following is O(n), not O(1).
/// The entry point for bridging `Dictionary` to `NSDictionary` in bridge
/// thunks. Used, for example, to expose:
///
/// func f() -> [String : String] {}
///
/// to Objective-C code as a method that returns an `NSDictionary`.
///
/// This is a forced downcast. This operation should have O(1) complexity.
///
/// The cast can fail if bridging fails. The actual checks and bridging can be
/// deferred.
@warn_unused_result
public func _convertDictionaryToNSDictionary<Key, Value>(
d: [Key : Value]
) -> NSDictionary {
// Note: there should be *a good justification* for doing something else
// than just dispatching to `_bridgeToObjectiveC`.
return d._bridgeToObjectiveC()
}
// Dictionary<Key, Value> is conditionally bridged to NSDictionary
extension Dictionary : _ObjectiveCBridgeable {
public static func _getObjectiveCType() -> Any.Type {
@@ -925,45 +840,6 @@ extension NSIndexSet : Sequence {
}
}
// FIXME: right now the following is O(n), not O(1).
/// The entry point for bridging `Set` to `NSSet` in bridge
/// thunks. Used, for example, to expose:
///
/// func f() -> Set<String> {}
///
/// to Objective-C code as a method that returns an `NSSet`.
///
/// This is a forced downcast. This operation should have O(1) complexity.
///
/// The cast can fail if bridging fails. The actual checks and bridging can be
/// deferred.
@warn_unused_result
public func _convertSetToNSSet<T>(s: Set<T>) -> NSSet {
return s._bridgeToObjectiveC()
}
/// The entry point for bridging `NSSet` to `Set` in bridge
/// thunks. Used, for example, to expose:
///
/// func f(Set<String>) {}
///
/// to Objective-C code as a method that accepts an `NSSet`.
///
/// This is a forced downcast. This operation should have O(1) complexity
/// when `T` is bridged verbatim.
///
/// The cast can fail if bridging fails. The actual checks and bridging can be
/// deferred.
@warn_unused_result
@_semantics("convertFromObjectiveC")
public func _convertNSSetToSet<T : Hashable>(s: NSSet?) -> Set<T> {
if _slowPath(s == nil) { return [] }
var result: Set<T>?
Set._forceBridgeFromObjectiveC(s!, result: &result)
return result!
}
// Set<Element> is conditionally bridged to NSSet
extension Set : _ObjectiveCBridgeable {
public static func _getObjectiveCType() -> Any.Type {

View File

@@ -384,7 +384,7 @@ extension String {
if let matches = nsMatches {
// Since this function is effectively a bridge thunk, use the
// bridge thunk semantics for the NSArray conversion
matchesIntoArray._setIfNonNil { _convertNSArrayToArray(matches) }
matchesIntoArray._setIfNonNil { return matches as! [String] }
}
if let n = nsOutputName {
@@ -406,7 +406,7 @@ extension String {
let nsa = _ns.componentsSeparatedByCharacters(in: separator) as NSArray
// Since this function is effectively a bridge thunk, use the
// bridge thunk semantics for the NSArray conversion
return _convertNSArrayToArray(nsa)
return nsa as! [String]
}
@@ -418,7 +418,7 @@ extension String {
let nsa = _ns.componentsSeparated(by: separator) as NSArray
// Since this function is effectively a bridge thunk, use the
// bridge thunk semantics for the NSArray conversion
return _convertNSArrayToArray(nsa)
return nsa as! [String]
}
// - (const char *)cStringUsingEncoding:(NSStringEncoding)encoding
@@ -1040,7 +1040,7 @@ extension String {
}
}
return _convertNSArrayToArray(result)
return result as! [String]
}
// - (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)aString