gpt4 book ai didi

swift - 基本 Swift 类型的完整列表

转载 作者:可可西里 更新时间:2023-10-31 23:56:37 30 4
gpt4 key购买 nike

来自 Swift 文档:

Swift provides its own versions of all fundamental C and Objective-C types, including Int for integers, Double and Float for floating-point values, Bool for Boolean values, and String for textual data. Swift also provides powerful versions of the two primary collection types, Array and Dictionary, as described in Collection Types.

它是基本类型的完整列表还是除了上面提到的这七种之外还有任何其他基本类型?

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

最佳答案

更新

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

Xcode 9.4 demo

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

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,输入任何内置类型(或函数),然后按住命令单击它:

getting to 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 - 基本 Swift 类型的完整列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25754791/

30 4 0
文章推荐: php - 这两行 PHP 有何不同?
文章推荐: javascript - 在 puppeteer 请求中使用不同的 ip 地址
文章推荐: php - 如何使用 PHP 从