【问题标题】:Full list of fundamental Swift types基本 Swift 类型的完整列表
【发布时间】:2014-09-09 22:56:27
【问题描述】:

来自 Swift 文档:

Swift 为所有基础 C 和 Objective-C 提供了自己的版本 类型,包括用于整数的 Int、用于浮点的 DoubleFloat 值,Bool 用于布尔值,String 用于文本数据。迅速 还提供了两种主要集合类型的强大版本, ArrayDictionary,如集合类型中所述。

是基本类型的综合列表还是除了上面提到的这七种之外还有其他基本类型吗?

谁能提供完整的类型列表?

【问题讨论】:

    标签: swift


    【解决方案1】:

    更新

    从 Xcode 9.4(包括 Swift 4.1.2)开始,您可以通过输入 import Swift,选择单词 Swift,然后跳转到其定义,让 Xcode 向您显示其标准库的声明。

    我复制了生成的文件的内容并运行了这个命令:

    pbpaste | egrep '^public (struct|protocol|class|enum)' > /tmp/swifttypes
    

    结果如下:

    public struct AnyBidirectionalCollection<Element> {
    public struct AnyCollection<Element> {
    public struct AnyHashable {
    public struct AnyIndex {
    public struct AnyIterator<Element> {
    public class AnyKeyPath : Hashable, _AppendKeyPath {
    public struct AnyRandomAccessCollection<Element> {
    public struct AnySequence<Element> {
    public struct Array<Element> {
    public struct ArraySlice<Element> {
    public struct AutoreleasingUnsafeMutablePointer<Pointee> {
    public protocol BidirectionalCollection : Collection where Self.Indices : BidirectionalCollection, Self.SubSequence : BidirectionalCollection {
    public protocol BinaryFloatingPoint : ExpressibleByFloatLiteral, FloatingPoint {
    public protocol BinaryInteger : CustomStringConvertible, Hashable, Numeric, Strideable where Self.Magnitude : BinaryInteger, Self.Magnitude == Self.Magnitude.Magnitude {
    public struct Bool {
    public struct CVaListPointer {
    public protocol CVarArg {
    public struct Character {
    public struct ClosedRange<Bound> where Bound : Comparable {
    public struct ClosedRangeIndex<Bound> where Bound : Strideable, Bound.Stride : SignedInteger {
    public protocol CodingKey : CustomDebugStringConvertible, CustomStringConvertible {
    public struct CodingUserInfoKey : RawRepresentable, Equatable, Hashable {
    public protocol Collection : Sequence where Self.SubSequence : Collection {
    public struct CollectionOfOne<Element> {
    public enum CommandLine {
    public protocol Comparable : Equatable {
    public struct ContiguousArray<Element> {
    public struct CountableClosedRange<Bound> where Bound : Strideable, Bound.Stride : SignedInteger {
    public struct CountablePartialRangeFrom<Bound> where Bound : Strideable, Bound.Stride : SignedInteger {
    public struct CountableRange<Bound> where Bound : Strideable, Bound.Stride : SignedInteger {
    public protocol CustomDebugStringConvertible {
    public protocol CustomLeafReflectable : CustomReflectable {
    public protocol CustomPlaygroundDisplayConvertible {
    public protocol CustomPlaygroundQuickLookable {
    public protocol CustomReflectable {
    public protocol CustomStringConvertible {
    public protocol Decodable {
    public protocol Decoder {
    public enum DecodingError : Error {
    public struct DefaultIndices<Elements> where Elements : Collection {
    public struct Dictionary<Key, Value> where Key : Hashable {
    public struct DictionaryIterator<Key, Value> : IteratorProtocol where Key : Hashable {
    public struct DictionaryLiteral<Key, Value> : ExpressibleByDictionaryLiteral {
    public struct Double {
    public struct EmptyCollection<Element> {
    public struct EmptyIterator<Element> {
    public protocol Encodable {
    public protocol Encoder {
    public enum EncodingError : Error {
    public struct EnumeratedIterator<Base> where Base : IteratorProtocol {
    public struct EnumeratedSequence<Base> where Base : Sequence {
    public protocol Equatable {
    public protocol Error {
    public protocol ExpressibleByArrayLiteral {
    public protocol ExpressibleByBooleanLiteral {
    public protocol ExpressibleByDictionaryLiteral {
    public protocol ExpressibleByExtendedGraphemeClusterLiteral : ExpressibleByUnicodeScalarLiteral {
    public protocol ExpressibleByFloatLiteral {
    public protocol ExpressibleByIntegerLiteral {
    public protocol ExpressibleByNilLiteral {
    public protocol ExpressibleByStringLiteral : ExpressibleByExtendedGraphemeClusterLiteral {
    public protocol ExpressibleByUnicodeScalarLiteral {
    public protocol FixedWidthInteger : BinaryInteger, LosslessStringConvertible, _BitwiseOperations where Self.Magnitude : FixedWidthInteger {
    public struct FlattenBidirectionalCollection<Base> : BidirectionalCollection where Base : BidirectionalCollection, Base.Element : BidirectionalCollection {
    public struct FlattenBidirectionalCollectionIndex<BaseElements> where BaseElements : BidirectionalCollection, BaseElements.Element : BidirectionalCollection {
    public struct FlattenCollection<Base> : Collection where Base : Collection, Base.Element : Collection {
    public struct FlattenCollectionIndex<BaseElements> where BaseElements : Collection, BaseElements.Element : Collection {
    public struct FlattenIterator<Base> : IteratorProtocol, Sequence where Base : IteratorProtocol, Base.Element : Sequence {
    public struct FlattenSequence<Base> : Sequence where Base : Sequence, Base.Element : Sequence {
    public struct Float {
    public struct Float80 {
    public protocol FloatingPoint : Hashable, SignedNumeric, Strideable {
    public enum FloatingPointClassification {
    public enum FloatingPointRoundingRule {
    public enum FloatingPointSign : Int {
    public protocol Hashable : Equatable {
    public enum ImplicitlyUnwrappedOptional<Wrapped> : ExpressibleByNilLiteral {
    public struct IndexingIterator<Elements> where Elements : Collection {
    public struct Int : FixedWidthInteger, SignedInteger {
    public struct Int16 : FixedWidthInteger, SignedInteger {
    public struct Int32 : FixedWidthInteger, SignedInteger {
    public struct Int64 : FixedWidthInteger, SignedInteger {
    public struct Int8 : FixedWidthInteger, SignedInteger {
    public struct IteratorOverOne<Element> {
    public protocol IteratorProtocol {
    public struct IteratorSequence<Base> where Base : IteratorProtocol {
    public struct JoinedIterator<Base> : IteratorProtocol where Base : IteratorProtocol, Base.Element : Sequence {
    public struct JoinedSequence<Base> : Sequence where Base : Sequence, Base.Element : Sequence {
    public class KeyPath<Root, Value> : PartialKeyPath<Root> {
    public struct KeyedDecodingContainer<K> : KeyedDecodingContainerProtocol where K : CodingKey {
    public protocol KeyedDecodingContainerProtocol {
    public struct KeyedEncodingContainer<K> : KeyedEncodingContainerProtocol where K : CodingKey {
    public protocol KeyedEncodingContainerProtocol {
    public struct LazyCollection<Base> : LazyCollectionProtocol where Base : Collection {
    public protocol LazyCollectionProtocol : Collection, LazySequenceProtocol where Self.Elements : Collection {
    public struct LazyDropWhileBidirectionalCollection<Base> : LazyCollectionProtocol, BidirectionalCollection where Base : BidirectionalCollection {
    public struct LazyDropWhileCollection<Base> : LazyCollectionProtocol, Collection where Base : Collection {
    public struct LazyDropWhileIndex<Base> : Comparable where Base : Collection {
    public struct LazyDropWhileIterator<Base> : IteratorProtocol, Sequence where Base : IteratorProtocol {
    public struct LazyDropWhileSequence<Base> : LazySequenceProtocol where Base : Sequence {
    public struct LazyFilterCollection<Base> where Base : Collection {
    public struct LazyFilterIterator<Base> : IteratorProtocol, Sequence where Base : IteratorProtocol {
    public struct LazyFilterSequence<Base> : LazySequenceProtocol where Base : Sequence {
    public struct LazyMapCollection<Base, Element> : LazyCollectionProtocol, Collection where Base : Collection {
    public struct LazyMapIterator<Base, Element> : IteratorProtocol, Sequence where Base : IteratorProtocol {
    public struct LazyMapSequence<Base, Element> : LazySequenceProtocol where Base : Sequence {
    public struct LazyPrefixWhileBidirectionalCollection<Base> : LazyCollectionProtocol, BidirectionalCollection where Base : BidirectionalCollection {
    public struct LazyPrefixWhileCollection<Base> : LazyCollectionProtocol, Collection where Base : Collection {
    public struct LazyPrefixWhileIndex<Base> : Comparable where Base : Collection {
    public struct LazyPrefixWhileIterator<Base> : IteratorProtocol, Sequence where Base : IteratorProtocol {
    public struct LazyPrefixWhileSequence<Base> : LazySequenceProtocol where Base : Sequence {
    public struct LazySequence<Base> : LazySequenceProtocol, _SequenceWrapper where Base : Sequence {
    public protocol LazySequenceProtocol : Sequence {
    public protocol LosslessStringConvertible : CustomStringConvertible {
    public struct ManagedBufferPointer<Header, Element> : Equatable {
    public enum MemoryLayout<T> {
    public struct Mirror {
    public protocol MirrorPath {
    public protocol MutableCollection : Collection where Self.SubSequence : MutableCollection {
    public enum Never {
    public protocol Numeric : Equatable, ExpressibleByIntegerLiteral {
    public struct ObjectIdentifier {
    public struct OpaquePointer {
    public protocol OptionSet : RawRepresentable, SetAlgebra {
    public enum Optional<Wrapped> : ExpressibleByNilLiteral {
    public class PartialKeyPath<Root> : AnyKeyPath {
    public struct PartialRangeFrom<Bound> where Bound : Comparable {
    public struct PartialRangeThrough<Bound> where Bound : Comparable {
    public struct PartialRangeUpTo<Bound> where Bound : Comparable {
    public enum PlaygroundQuickLook {
    public protocol RandomAccessCollection : BidirectionalCollection where Self.Indices : RandomAccessCollection, Self.SubSequence : RandomAccessCollection {
    public struct Range<Bound> where Bound : Comparable {
    public protocol RangeExpression {
    public protocol RangeReplaceableCollection : Collection where Self.SubSequence : RangeReplaceableCollection {
    public protocol RawRepresentable {
    public class ReferenceWritableKeyPath<Root, Value> : WritableKeyPath<Root, Value> {
    public struct Repeated<Element> {
    public struct ReversedCollection<Base> where Base : BidirectionalCollection {
    public struct ReversedIndex<Base> : Comparable where Base : Collection {
    public protocol Sequence {
    public struct Set<Element> where Element : Hashable {
    public protocol SetAlgebra : Equatable, ExpressibleByArrayLiteral {
    public struct SetIterator<Element> : IteratorProtocol where Element : Hashable {
    public protocol SignedInteger : BinaryInteger, SignedNumeric {
    public protocol SignedNumeric : Numeric {
    public protocol SingleValueDecodingContainer {
    public protocol SingleValueEncodingContainer {
    public struct Slice<Base> where Base : Collection {
    public struct StaticString : ExpressibleByUnicodeScalarLiteral, ExpressibleByExtendedGraphemeClusterLiteral, ExpressibleByStringLiteral, CustomStringConvertible, CustomDebugStringConvertible, CustomReflectable {
    public struct StrideThrough<Element> where Element : Strideable {
    public struct StrideThroughIterator<Element> where Element : Strideable {
    public struct StrideTo<Element> where Element : Strideable {
    public struct StrideToIterator<Element> where Element : Strideable {
    public protocol Strideable : Comparable {
    public struct String {
    public protocol StringProtocol : BidirectionalCollection, Comparable, ExpressibleByStringLiteral, Hashable, LosslessStringConvertible, TextOutputStream, TextOutputStreamable where Self.Element == Character, Self.SubSequence : StringProtocol {
    public struct Substring : StringProtocol {
    public protocol TextOutputStream {
    public protocol TextOutputStreamable {
    public struct UInt : FixedWidthInteger, UnsignedInteger {
    public struct UInt16 : FixedWidthInteger, UnsignedInteger {
    public struct UInt32 : FixedWidthInteger, UnsignedInteger {
    public struct UInt64 : FixedWidthInteger, UnsignedInteger {
    public struct UInt8 : FixedWidthInteger, UnsignedInteger {
    public enum UnboundedRange_ {
    public struct UnfoldSequence<Element, State> : Sequence, IteratorProtocol {
    public enum Unicode {
    public protocol UnicodeCodec : _UnicodeEncoding {
    public enum UnicodeDecodingResult : Equatable {
    public protocol UnkeyedDecodingContainer {
    public protocol UnkeyedEncodingContainer {
    public struct Unmanaged<Instance> where Instance : AnyObject {
    public struct UnsafeBufferPointer<Element> : Collection, RandomAccessCollection {
    public struct UnsafeBufferPointerIterator<Element> : IteratorProtocol, Sequence {
    public struct UnsafeMutableBufferPointer<Element> : MutableCollection, RandomAccessCollection {
    public struct UnsafeMutablePointer<Pointee> {
    public struct UnsafeMutableRawBufferPointer {
    public struct UnsafeMutableRawPointer {
    public struct UnsafePointer<Pointee> {
    public struct UnsafeRawBufferPointer {
    public struct UnsafeRawPointer {
    public protocol UnsignedInteger : BinaryInteger {
    public class WritableKeyPath<Root, Value> : KeyPath<Root, Value> {
    public struct Zip2Iterator<Iterator1, Iterator2> where Iterator1 : IteratorProtocol, Iterator2 : IteratorProtocol {
    public struct Zip2Sequence<Sequence1, Sequence2> where Sequence1 : Sequence, Sequence2 : Sequence {
    public protocol _AppendKeyPath {
    public protocol _SequenceWrapper : Sequence {
    

    原创

    这是 Swift 文档目前糟糕的领域之一。

    无论如何,您可以通过创建 Swift 源文件或 Playground,输入任何内置类型(或函数),然后单击命令来找到完整的 Swift 内置 API:

    这会将您带到一个生成的 Swift 源文件(它在 Xcode 包中不存在),该文件声明了整个内置的 Swift API。您可以浏览它以查找所有内置类型。您提到的每种类型(甚至ArrayDictionary)都被声明为struct,通常带有一堆extensions。还有更多内置的struct 类型,还有一堆protocolclass 类型。

    通过egrep '^(struct|protocol|class) 将该文件通过管道找到这些内置类型:

    protocol AbsoluteValuable : SignedNumberType {
    protocol AnyObject {
    struct Array<T> : MutableCollectionType, Sliceable {
    protocol ArrayBoundType {
    protocol ArrayLiteralConvertible {
    struct AssertString : AssertStringType, StringInterpolationConvertible {
    protocol AssertStringType : ExtendedGraphemeClusterLiteralConvertible, StringLiteralConvertible {
    struct AutoreleasingUnsafeMutablePointer<T> : Equatable, NilLiteralConvertible {
    protocol BidirectionalIndexType : ForwardIndexType, _BidirectionalIndexType {
    struct BidirectionalReverseView<T : CollectionType where T.Index : BidirectionalIndexType> : CollectionType {
    protocol BitwiseOperationsType {
    struct Bool {
    protocol BooleanLiteralConvertible {
    protocol BooleanType {
    struct CFunctionPointer<T> : Equatable, Hashable, NilLiteralConvertible {
    struct COpaquePointer : Equatable, Hashable, NilLiteralConvertible {
    struct CVaListPointer {
    protocol CVarArgType {
    protocol CharacterLiteralConvertible {
    struct ClosedInterval<T : Comparable> : IntervalType, Equatable, Printable, DebugPrintable, Reflectable {
    struct CollectionOfOne<T> : CollectionType {
    protocol CollectionType : _CollectionType, SequenceType {
    protocol Comparable : _Comparable, Equatable {
    struct ContiguousArray<T> : MutableCollectionType, Sliceable {
    protocol DebugPrintable {
    struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConvertible {
    struct DictionaryGenerator<Key : Hashable, Value> : GeneratorType {
    struct DictionaryIndex<Key : Hashable, Value> : BidirectionalIndexType, Comparable {
    protocol DictionaryLiteralConvertible {
    struct Double {
    struct EmptyCollection<T> : CollectionType {
    struct EmptyGenerator<T> : GeneratorType, SequenceType {
    struct EnumerateGenerator<Base : GeneratorType> : GeneratorType, SequenceType {
    protocol Equatable {
    protocol ExtendedGraphemeClusterLiteralConvertible {
    protocol ExtensibleCollectionType : _ExtensibleCollectionType {
    struct FilterCollectionView<Base : CollectionType> : CollectionType {
    struct FilterCollectionViewIndex<Base : CollectionType> : ForwardIndexType {
    struct FilterGenerator<Base : GeneratorType> : GeneratorType, SequenceType {
    struct FilterSequenceView<Base : SequenceType> : SequenceType {
    struct Float {
    struct Float80 {
    protocol FloatLiteralConvertible {
    protocol FloatingPointType : Strideable {
    protocol ForwardIndexType : _ForwardIndexType {
    struct GeneratorOf<T> : GeneratorType, SequenceType {
    struct GeneratorOfOne<T> : GeneratorType, SequenceType {
    struct GeneratorSequence<G : GeneratorType> : GeneratorType, SequenceType {
    protocol GeneratorType {
    struct HalfOpenInterval<T : Comparable> : IntervalType, Equatable, Printable, DebugPrintable, Reflectable {
    protocol Hashable : Equatable {
    struct HeapBuffer<Value, Element> : Equatable {
    class HeapBufferStorage<Value, Element> : HeapBufferStorageBase {
    class HeapBufferStorageBase {
    struct IndexingGenerator<C : _CollectionType> : GeneratorType, SequenceType {
    struct Int : SignedIntegerType {
    struct Int16 : SignedIntegerType {
    struct Int32 : SignedIntegerType {
    struct Int64 : SignedIntegerType {
    struct Int8 : SignedIntegerType {
    protocol IntegerArithmeticType : _IntegerArithmeticType, Comparable {
    protocol IntegerLiteralConvertible {
    protocol IntegerType : _IntegerType, RandomAccessIndexType {
    protocol IntervalType {
    struct LazyBidirectionalCollection<S : CollectionType where S.Index : BidirectionalIndexType> : CollectionType {
    struct LazyForwardCollection<S : CollectionType where S.Index : ForwardIndexType> : CollectionType {
    struct LazyRandomAccessCollection<S : CollectionType where S.Index : RandomAccessIndexType> : CollectionType {
    struct LazySequence<S : SequenceType> : SequenceType {
    struct MapCollectionView<Base : CollectionType, T> : CollectionType {
    struct MapSequenceGenerator<Base : GeneratorType, T> : GeneratorType, SequenceType {
    struct MapSequenceView<Base : SequenceType, T> : SequenceType {
    protocol MirrorType {
    protocol MutableCollectionType : CollectionType {
    protocol MutableSliceable : Sliceable, MutableCollectionType {
    protocol NilLiteralConvertible {
    struct ObjectIdentifier : Hashable {
    struct OnHeap<T> {
    protocol OutputStreamType {
    struct PermutationGenerator<C : CollectionType, Indices : SequenceType where C.Index == C.Index> : GeneratorType, SequenceType {
    protocol Printable {
    protocol RandomAccessIndexType : BidirectionalIndexType, _RandomAccessIndexType {
    struct RandomAccessReverseView<T : CollectionType where T.Index : RandomAccessIndexType> : CollectionType {
    struct Range<T : ForwardIndexType> : Equatable, CollectionType, Printable, DebugPrintable {
    struct RangeGenerator<T : ForwardIndexType> : GeneratorType, SequenceType {
    protocol RangeReplaceableCollectionType : ExtensibleCollectionType {
    struct RawByte {
    protocol RawOptionSetType : _RawOptionSetType, BitwiseOperationsType, NilLiteralConvertible {
    protocol RawRepresentable {
    protocol Reflectable {
    struct Repeat<T> : CollectionType {
    struct ReverseBidirectionalIndex<I : BidirectionalIndexType> : BidirectionalIndexType {
    struct ReverseRandomAccessIndex<I : RandomAccessIndexType> : RandomAccessIndexType {
    struct SequenceOf<T> : SequenceType {
    protocol SequenceType : _Sequence_Type {
    protocol SignedIntegerType : _SignedIntegerType, IntegerType {
    protocol SignedNumberType : _SignedNumberType {
    struct SinkOf<T> : SinkType {
    protocol SinkType {
    struct Slice<T> : MutableCollectionType, Sliceable {
    protocol Sliceable : _Sliceable {
    struct StaticString : StaticStringType {
    protocol StaticStringType : AssertStringType {
    protocol Streamable {
    struct StrideThrough<T : Strideable> : SequenceType {
    struct StrideThroughGenerator<T : Strideable> : GeneratorType {
    struct StrideTo<T : Strideable> : SequenceType {
    struct StrideToGenerator<T : Strideable> : GeneratorType {
    protocol Strideable : Comparable, _Strideable {
    struct String {
    protocol StringElementType {
    protocol StringInterpolationConvertible {
    protocol StringLiteralConvertible : ExtendedGraphemeClusterLiteralConvertible {
    struct UInt : UnsignedIntegerType {
    struct UInt16 : UnsignedIntegerType {
    struct UInt32 : UnsignedIntegerType {
    struct UInt64 : UnsignedIntegerType {
    struct UInt8 : UnsignedIntegerType {
    struct UTF16 : UnicodeCodecType {
    struct UTF32 : UnicodeCodecType {
    struct UTF8 : UnicodeCodecType {
    protocol UnicodeCodecType {
    struct UnicodeScalar : ExtendedGraphemeClusterLiteralConvertible {
    struct Unmanaged<T> {
    struct UnsafeBufferPointer<T> : CollectionType {
    struct UnsafeBufferPointerGenerator<T> : GeneratorType, SequenceType {
    struct UnsafeMutableBufferPointer<T> : MutableCollectionType {
    struct UnsafeMutablePointer<T> : RandomAccessIndexType, Hashable, NilLiteralConvertible {
    struct UnsafePointer<T> : RandomAccessIndexType, Hashable, NilLiteralConvertible {
    protocol UnsignedIntegerType : _UnsignedIntegerType, IntegerType {
    struct Zip2<S0 : SequenceType, S1 : SequenceType> : SequenceType {
    struct ZipGenerator2<E0 : GeneratorType, E1 : GeneratorType> : GeneratorType {
    protocol _ArrayBufferType : MutableCollectionType {
    protocol _BidirectionalIndexType : _ForwardIndexType {
    protocol _CocoaStringType {
    protocol _CollectionType : _SequenceType {
    protocol _Comparable {
    protocol _ExtensibleCollectionType : CollectionType {
    protocol _ForwardIndexType : _Incrementable {
    protocol _Incrementable : Equatable {
    protocol _IntegerArithmeticType {
    protocol _IntegerType : IntegerLiteralConvertible, Printable, ArrayBoundType, Hashable, IntegerArithmeticType, BitwiseOperationsType, _Incrementable {
    protocol _ObjectiveCBridgeable {
    protocol _PrintableNSObjectType {
    protocol _RandomAccessIndexType : _BidirectionalIndexType, Strideable {
    protocol _RawOptionSetType : RawRepresentable, Equatable {
    protocol _SequenceType {
    protocol _Sequence_Type : _SequenceType {
    protocol _SignedIntegerType : _IntegerType, SignedNumberType {
    protocol _SignedNumberType : Comparable, IntegerLiteralConvertible {
    protocol _Sliceable : CollectionType {
    protocol _Strideable {
    protocol _SwiftNSArrayRequiredOverridesType : _SwiftNSCopyingType, _SwiftNSFastEnumerationType {
    protocol _SwiftNSCopyingType {
    protocol _SwiftNSDictionaryRequiredOverridesType : _SwiftNSCopyingType, _SwiftNSFastEnumerationType {
    protocol _SwiftNSEnumeratorType {
    protocol _SwiftNSFastEnumerationType {
    protocol _SwiftNSStringRequiredOverridesType : _SwiftNSCopyingType, _SwiftNSFastEnumerationType {
    protocol _SwiftNSStringType : _SwiftNSStringRequiredOverridesType {
    protocol _UnsignedIntegerType : _IntegerType {
    

    【讨论】:

    • 我能说服你在 Swift 4.1 下再次运行它并用输出更新它吗?我会自己做,但我使用的是来自 Linux 的 Swift,它当然没有 XCode...
    • @kirbyfan64sos 你的愿望已经实现。
    【解决方案2】:

    Swift 的基本数据类型是

    字符串、布尔值、Int、Int8、Int16、Int32、Int64、UInt、UInt8、UInt16、UInt32、UInt64、Float、Float80、Double

    【讨论】:

      【解决方案3】:

      Rob 的回答(像往常一样)很棒,但还有一些细微差别(比评论更适合)值得一提——您说的是 Swift 的设计与基于 C 的语言的设计不同的一个领域。

      在 C(以及扩展的 ObjC)中,存在由语言(intfloatunsigned long 等)指定和/或由编译器(__darwin_size_t 等)。还有更高级别的类型,您(或库作者)根据基本类型进行综合:您将新类型定义为基本类型的别名(使用typedef#define)或作为基本类型的复合(使用structs、指针/数组或 ObjC 类)。

      在 Swift 中,没有基本类型。1 标准库类型不是一些特殊类型,因为它们是其他类型的基础,而是与您可以自己定义的类型。 (或者换一种说法,所有 Swift 类型都是基础类型。)您甚至可以使用 LiteralConvertible 协议定义自己的类型,这些类型就像“基础”类型一样。

      我认为文档中的那段内容是 Swift 类型等同于所有基本的 C 类型,而不是 Swift 有自己的一组“基本”类型。


      1 好的,Swift 确实有一些特殊的编译器实现的类型:例如,您会在 Int 的标准库定义中看到它基于编译器的 Builtin.Word 类型。但与在 C 中“内置”类型是您使用的语言的一部分不同,在 Swift 中它们只是底层实现细节。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-04
        • 2011-02-24
        • 2014-04-19
        • 2011-12-25
        • 2013-06-20
        • 1970-01-01
        相关资源
        最近更新 更多