|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +@_exported import Foundation // Clang module |
| 14 | +import CoreFoundation |
| 15 | +import CoreGraphics |
| 16 | + |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | +// NSObject |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | + |
| 21 | +// These conformances should be located in the `ObjectiveC` module, but they can't |
| 22 | +// be placed there because string bridging is not available there. |
| 23 | +extension NSObject : CustomStringConvertible {} |
| 24 | +extension NSObject : CustomDebugStringConvertible {} |
| 25 | + |
| 26 | +public let NSNotFound: Int = .max |
| 27 | + |
| 28 | +//===----------------------------------------------------------------------===// |
| 29 | +// NSLocalizedString |
| 30 | +//===----------------------------------------------------------------------===// |
| 31 | + |
| 32 | +/// Returns a localized string, using the main bundle if one is not specified. |
| 33 | +public |
| 34 | +func NSLocalizedString(_ key: String, |
| 35 | + tableName: String? = nil, |
| 36 | + bundle: Bundle = Bundle.main, |
| 37 | + value: String = "", |
| 38 | + comment: String) -> String { |
| 39 | + return bundle.localizedString(forKey: key, value:value, table:tableName) |
| 40 | +} |
| 41 | + |
| 42 | +//===----------------------------------------------------------------------===// |
| 43 | +// NSLog |
| 44 | +//===----------------------------------------------------------------------===// |
| 45 | + |
| 46 | +public func NSLog(_ format: String, _ args: CVarArg...) { |
| 47 | + withVaList(args) { NSLogv(format, $0) } |
| 48 | +} |
| 49 | + |
| 50 | +//===----------------------------------------------------------------------===// |
| 51 | +// AnyHashable |
| 52 | +//===----------------------------------------------------------------------===// |
| 53 | + |
| 54 | +extension AnyHashable : _ObjectiveCBridgeable { |
| 55 | + public func _bridgeToObjectiveC() -> NSObject { |
| 56 | + // This is unprincipled, but pretty much any object we'll encounter in |
| 57 | + // Swift is NSObject-conforming enough to have -hash and -isEqual:. |
| 58 | + return unsafeBitCast(base as AnyObject, to: NSObject.self) |
| 59 | + } |
| 60 | + |
| 61 | + public static func _forceBridgeFromObjectiveC( |
| 62 | + _ x: NSObject, |
| 63 | + result: inout AnyHashable? |
| 64 | + ) { |
| 65 | + result = AnyHashable(x) |
| 66 | + } |
| 67 | + |
| 68 | + public static func _conditionallyBridgeFromObjectiveC( |
| 69 | + _ x: NSObject, |
| 70 | + result: inout AnyHashable? |
| 71 | + ) -> Bool { |
| 72 | + self._forceBridgeFromObjectiveC(x, result: &result) |
| 73 | + return result != nil |
| 74 | + } |
| 75 | + |
| 76 | + public static func _unconditionallyBridgeFromObjectiveC( |
| 77 | + _ source: NSObject? |
| 78 | + ) -> AnyHashable { |
| 79 | + // `nil` has historically been used as a stand-in for an empty |
| 80 | + // string; map it to an empty string. |
| 81 | + if _slowPath(source == nil) { return AnyHashable(String()) } |
| 82 | + return AnyHashable(source!) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +//===----------------------------------------------------------------------===// |
| 87 | +// CVarArg for bridged types |
| 88 | +//===----------------------------------------------------------------------===// |
| 89 | + |
| 90 | +extension CVarArg where Self: _ObjectiveCBridgeable { |
| 91 | + /// Default implementation for bridgeable types. |
| 92 | + public var _cVarArgEncoding: [Int] { |
| 93 | + let object = self._bridgeToObjectiveC() |
| 94 | + _autorelease(object) |
| 95 | + return _encodeBitsAsWords(object) |
| 96 | + } |
| 97 | +} |
0 commit comments