| //===----------------------------------------------------------------------===// |
| // |
| // This source file is part of the Swift.org open source project |
| // |
| // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| // Licensed under Apache License v2.0 with Runtime Library Exception |
| // |
| // See https://swift.org/LICENSE.txt for license information |
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| // |
| //===----------------------------------------------------------------------===// |
| |
| @_exported import Foundation // Clang module |
| import CoreFoundation |
| import CoreGraphics |
| |
| //===----------------------------------------------------------------------===// |
| // NSObject |
| //===----------------------------------------------------------------------===// |
| |
| // These conformances should be located in the `ObjectiveC` module, but they can't |
| // be placed there because string bridging is not available there. |
| extension NSObject : CustomStringConvertible {} |
| extension NSObject : CustomDebugStringConvertible {} |
| |
| public let NSNotFound: Int = .max |
| |
| //===----------------------------------------------------------------------===// |
| // NSLocalizedString |
| //===----------------------------------------------------------------------===// |
| |
| /// Returns a localized string, using the main bundle if one is not specified. |
| public |
| func NSLocalizedString(_ key: String, |
| tableName: String? = nil, |
| bundle: Bundle = Bundle.main, |
| value: String = "", |
| comment: String) -> String { |
| return bundle.localizedString(forKey: key, value:value, table:tableName) |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // NSLog |
| //===----------------------------------------------------------------------===// |
| |
| public func NSLog(_ format: String, _ args: CVarArg...) { |
| withVaList(args) { NSLogv(format, $0) } |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // AnyHashable |
| //===----------------------------------------------------------------------===// |
| |
| extension AnyHashable : _ObjectiveCBridgeable { |
| public func _bridgeToObjectiveC() -> NSObject { |
| // This is unprincipled, but pretty much any object we'll encounter in |
| // Swift is NSObject-conforming enough to have -hash and -isEqual:. |
| return unsafeBitCast(base as AnyObject, to: NSObject.self) |
| } |
| |
| public static func _forceBridgeFromObjectiveC( |
| _ x: NSObject, |
| result: inout AnyHashable? |
| ) { |
| result = AnyHashable(x) |
| } |
| |
| public static func _conditionallyBridgeFromObjectiveC( |
| _ x: NSObject, |
| result: inout AnyHashable? |
| ) -> Bool { |
| self._forceBridgeFromObjectiveC(x, result: &result) |
| return result != nil |
| } |
| |
| public static func _unconditionallyBridgeFromObjectiveC( |
| _ source: NSObject? |
| ) -> AnyHashable { |
| // `nil` has historically been used as a stand-in for an empty |
| // string; map it to an empty string. |
| if _slowPath(source == nil) { return AnyHashable(String()) } |
| return AnyHashable(source!) |
| } |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // CVarArg for bridged types |
| //===----------------------------------------------------------------------===// |
| |
| extension CVarArg where Self: _ObjectiveCBridgeable { |
| /// Default implementation for bridgeable types. |
| public var _cVarArgEncoding: [Int] { |
| let object = self._bridgeToObjectiveC() |
| _autorelease(object) |
| return _encodeBitsAsWords(object) |
| } |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // Runtime support for NSKeyedArchives |
| //===----------------------------------------------------------------------===// |
| |
| @_silgen_name("swift_registerClassNameForArchiving") |
| public func _registerClassNameForArchiving(_ nameForClass: UnsafePointer<CChar>, |
| _ classType: AnyClass) { |
| // If it's not possible to create a String from the name, it should abort |
| // and not fail silently. |
| let nameStr = String(utf8String: nameForClass)! |
| |
| // Register the class name mapping for archiving and unarchiving. |
| NSKeyedArchiver.setClassName(nameStr, for: classType) |
| NSKeyedUnarchiver.setClass(classType, forClassName: nameStr) |
| } |
| |