Files
swift-mirror/objc/ObjectiveC.swift
Jordan Rose fa197bff3a The latest OSX 10.9 SDKs have changed name of the ObjC module to ObjectiveC.
Update everywhere in Swift that refers to this module accordingly.

This change is backwards-incompatible and will require rebuilding any
Objective-C-based object files. I recommend a clean of swiftFoundation
and NSStringDemo at the very least. The swiftObjC target is also being
renamed to swiftObjectiveC for consistency.

Swift SVN r3784
2013-01-17 19:46:39 +00:00

79 lines
2.3 KiB
Swift

import ObjectiveC
//===----------------------------------------------------------------------===//
// Objective-C Primitive Types
//===----------------------------------------------------------------------===//
// FIXME: Objective-C types belong in a layer below the Objective-C support
// libraries, not here.
/// \brief The Objective-C BOOL type.
///
/// The Objective-C BOOL type is typically a typedef of "signed char". However,
/// the Clang importer treats it as a distinct type.
///
/// Note: When BOOL is a typedef of C's _Bool/C++'s bool, this struct goes away
/// and simply becomes a typealias for CBool.
struct ObjCBool {
// FIXME: Ambiguity between ObjC and swift modules. Ugh.
var value : swift.UInt8
/// \brief Allow use in a Boolean context.
func getLogicValue() -> Bool {
if value == 0 { return false }
return true
}
/// \brief Implicit conversion from C Boolean type to Swift Boolean
/// type.
func [conversion] __conversion() -> Bool {
return this
}
}
extension Bool {
/// \brief Implicit conversion from Swift Boolean type to
/// Objective-C Boolean type.
func [conversion] __conversion() -> ObjCBool {
var result : ObjCBool
if this { result.value = 1 }
else { result.value = 0 }
return result
}
}
/// \brief The Objective-C SEL type.
///
/// The Objective-C SEL type is typically an opaque pointer. Swift
/// treats it as a distinct struct type, with operations to
/// convert between C strings and selectors.
struct ObjCSel {
var ptr : COpaquePointer
/// \brief Create a selector from a string.
constructor(str : String) {
ptr = sel_registerName(CString(str)).ptr
}
/// \brief Construct a selector from a string literal.
///
/// FIXME: Fast-path this in the compiler, so we don't end up with
/// the sel_registerName call at compile time.
static func convertFromStringLiteral(str : CString) -> ObjCSel {
return sel_registerName(str)
}
func replPrint() {
print(String(this))
}
}
extension String {
/// \brief Construct the C string representation of an Objective-C selector.
constructor(sel : ObjCSel) {
// FIXME: This is crazy. Adopting a C string as a Swift string should be
// simpler. This even misses the ASCII optimization.
var name = sel_getName(sel)
this = String.convertFromStringLiteral(name.cstr.value, strlen(name).value)
}
}