Skip to content

Instantly share code, notes, and snippets.

@mattt
Last active December 9, 2019 20:32
Show Gist options
  • Save mattt/95669a73cb395894144ec6e5f0557748 to your computer and use it in GitHub Desktop.
Save mattt/95669a73cb395894144ec6e5f0557748 to your computer and use it in GitHub Desktop.
Early Swift Doc output JSON for Swift standard library
This file has been truncated, but you can view the full file.
{
"files" : [
{
"enumerations" : [
{
"documentation" : [],
"declaration" : {
"attributes" : [],
"modifiers" : [
{
"name" : "public"
}
],
"cases" : [
{
"name" : "none",
"keyword" : "case",
"attributes" : [],
"context" : "ArithmeticOverflow",
"modifiers" : []
},
{
"context" : "ArithmeticOverflow",
"attributes" : [],
"modifiers" : [],
"keyword" : "case",
"name" : "overflow"
}
],
"name" : "ArithmeticOverflow",
"keyword" : "enum"
}
},
{
"documentation" : [
[
"summary",
"Command-line arguments for the current process.\n"
]
],
"declaration" : {
"modifiers" : [
{
"name" : "public"
}
],
"cases" : [],
"attributes" : [],
"keyword" : "enum",
"name" : "CommandLine"
}
},
{
"declaration" : {
"attributes" : [],
"modifiers" : [
{
"name" : "public"
}
],
"cases" : [
{
"name" : "signalingNaN",
"keyword" : "case",
"modifiers" : [],
"context" : "FloatingPointClassification",
"attributes" : []
},
{
"attributes" : [],
"context" : "FloatingPointClassification",
"modifiers" : [],
"name" : "quietNaN",
"keyword" : "case"
},
{
"keyword" : "case",
"name" : "negativeInfinity",
"modifiers" : [],
"context" : "FloatingPointClassification",
"attributes" : []
},
{
"keyword" : "case",
"name" : "negativeNormal",
"context" : "FloatingPointClassification",
"attributes" : [],
"modifiers" : []
},
{
"attributes" : [],
"context" : "FloatingPointClassification",
"modifiers" : [],
"name" : "negativeSubnormal",
"keyword" : "case"
},
{
"modifiers" : [],
"attributes" : [],
"context" : "FloatingPointClassification",
"name" : "negativeZero",
"keyword" : "case"
},
{
"context" : "FloatingPointClassification",
"attributes" : [],
"modifiers" : [],
"name" : "positiveZero",
"keyword" : "case"
},
{
"modifiers" : [],
"attributes" : [],
"context" : "FloatingPointClassification",
"name" : "positiveSubnormal",
"keyword" : "case"
},
{
"context" : "FloatingPointClassification",
"attributes" : [],
"modifiers" : [],
"name" : "positiveNormal",
"keyword" : "case"
},
{
"name" : "positiveInfinity",
"keyword" : "case",
"attributes" : [],
"context" : "FloatingPointClassification",
"modifiers" : []
}
],
"keyword" : "enum",
"name" : "FloatingPointClassification"
},
"documentation" : [
[
"summary",
"The IEEE 754 floating-point classes.\n"
]
]
},
{
"documentation" : [
[
"summary",
"A rule for rounding a floating-point number.\n"
]
],
"declaration" : {
"name" : "FloatingPointRoundingRule",
"keyword" : "enum",
"cases" : [
{
"context" : "FloatingPointRoundingRule",
"attributes" : [],
"modifiers" : [],
"keyword" : "case",
"name" : "toNearestOrAwayFromZero"
},
{
"attributes" : [],
"context" : "FloatingPointRoundingRule",
"modifiers" : [],
"name" : "toNearestOrEven",
"keyword" : "case"
},
{
"keyword" : "case",
"name" : "up",
"context" : "FloatingPointRoundingRule",
"attributes" : [],
"modifiers" : []
},
{
"modifiers" : [],
"attributes" : [],
"context" : "FloatingPointRoundingRule",
"name" : "down",
"keyword" : "case"
},
{
"attributes" : [],
"context" : "FloatingPointRoundingRule",
"modifiers" : [],
"keyword" : "case",
"name" : "towardZero"
},
{
"name" : "awayFromZero",
"keyword" : "case",
"modifiers" : [],
"attributes" : [],
"context" : "FloatingPointRoundingRule"
}
],
"modifiers" : [
{
"name" : "public"
}
],
"attributes" : []
}
},
{
"documentation" : [
[
"summary",
"The sign of a floating-point value.\n"
]
],
"declaration" : {
"attributes" : [],
"cases" : [
{
"keyword" : "case",
"name" : "plus",
"modifiers" : [],
"context" : "FloatingPointSign",
"attributes" : []
},
{
"keyword" : "case",
"name" : "minus",
"context" : "FloatingPointSign",
"attributes" : [],
"modifiers" : []
}
],
"modifiers" : [
{
"name" : "public"
}
],
"name" : "FloatingPointSign",
"keyword" : "enum"
}
},
{
"declaration" : {
"modifiers" : [
{
"name" : "public"
}
],
"cases" : [
{
"keyword" : "case",
"name" : "none",
"context" : "ImplicitlyUnwrappedOptional",
"attributes" : [],
"modifiers" : []
},
{
"keyword" : "case",
"name" : "some",
"associatedValue" : [
{
"attributes" : [],
"type" : "Wrapped"
}
],
"modifiers" : [],
"context" : "ImplicitlyUnwrappedOptional",
"attributes" : []
}
],
"attributes" : [],
"name" : "ImplicitlyUnwrappedOptional",
"keyword" : "enum"
},
"documentation" : [
[
"summary",
"An optional type that allows implicit member access.\n"
],
[
"discussion",
"*Deprecated.*\n"
]
]
},
{
"declaration" : {
"name" : "MemoryLayout",
"keyword" : "enum",
"attributes" : [],
"cases" : [],
"modifiers" : [
{
"name" : "public"
}
]
},
"documentation" : [
[
"summary",
"The memory layout of a type, describing its size, stride, and alignment.\n"
],
[
"discussion",
"You can use `MemoryLayout` as a source of information about a type when\nallocating or binding memory using unsafe pointers. The following example\ndeclares a `Point` type with `x` and `y` coordinates and a Boolean\n`isFilled` property.\n"
],
[
"discussion",
"``` \n struct Point {\n let x: Double\n let y: Double\n let isFilled: Bool\n }\n```\n"
],
[
"discussion",
"The size, stride, and alignment of the `Point` type are accessible as\nstatic properties of `MemoryLayout<Point>`.\n"
],
[
"discussion",
"``` \n // MemoryLayout<Point>.size == 17\n // MemoryLayout<Point>.stride == 24\n // MemoryLayout<Point>.alignment == 8\n```\n"
],
[
"discussion",
"Always use a multiple of a type's `stride` instead of its `size` when\nallocating memory or accounting for the distance between instances in\nmemory. This example allocates untyped, uninitialized memory with space\nfor four instances of `Point`.\n"
],
[
"discussion",
"``` \n let count = 4\n let pointPointer = UnsafeMutableRawPointer.allocate(\n bytes: count * MemoryLayout<Point>.stride,\n alignedTo: MemoryLayout<Point>.alignment)\n```\n"
]
]
},
{
"declaration" : {
"modifiers" : [
{
"name" : "public"
}
],
"cases" : [
{
"modifiers" : [],
"attributes" : [],
"context" : "Mirror.AncestorRepresentation",
"keyword" : "case",
"name" : "generated"
},
{
"keyword" : "case",
"associatedValue" : [
{
"attributes" : [],
"type" : "() -> Mirror"
}
],
"name" : "customized",
"modifiers" : [],
"context" : "Mirror.AncestorRepresentation",
"attributes" : []
},
{
"name" : "suppressed",
"keyword" : "case",
"attributes" : [],
"context" : "Mirror.AncestorRepresentation",
"modifiers" : []
}
],
"context" : "Mirror",
"attributes" : [],
"name" : "AncestorRepresentation",
"keyword" : "enum"
},
"documentation" : [
[
"summary",
"Representation of ancestor classes.\n"
],
[
"discussion",
"A `CustomReflectable` class can control how its mirror will\nrepresent ancestor classes by initializing the mirror with a\n`AncestorRepresentation`. This setting has no effect on mirrors\nreflecting value type instances.\n"
]
]
},
{
"declaration" : {
"keyword" : "enum",
"name" : "DisplayStyle",
"modifiers" : [
{
"name" : "public"
}
],
"cases" : [
{
"modifiers" : [],
"attributes" : [],
"context" : "Mirror.DisplayStyle",
"name" : "struct",
"keyword" : "case"
},
{
"modifiers" : [],
"attributes" : [],
"context" : "Mirror.DisplayStyle",
"keyword" : "case",
"name" : "class"
},
{
"name" : "enum",
"keyword" : "case",
"modifiers" : [],
"context" : "Mirror.DisplayStyle",
"attributes" : []
},
{
"name" : "tuple",
"keyword" : "case",
"context" : "Mirror.DisplayStyle",
"attributes" : [],
"modifiers" : []
},
{
"keyword" : "case",
"name" : "optional",
"context" : "Mirror.DisplayStyle",
"attributes" : [],
"modifiers" : []
},
{
"name" : "collection",
"keyword" : "case",
"attributes" : [],
"context" : "Mirror.DisplayStyle",
"modifiers" : []
},
{
"context" : "Mirror.DisplayStyle",
"attributes" : [],
"modifiers" : [],
"keyword" : "case",
"name" : "dictionary"
},
{
"keyword" : "case",
"name" : "set",
"attributes" : [],
"context" : "Mirror.DisplayStyle",
"modifiers" : []
}
],
"attributes" : [],
"context" : "Mirror"
},
"documentation" : [
[
"summary",
"A suggestion of how a `Mirror`'s `subject` is to be interpreted.\n"
],
[
"discussion",
"Playgrounds and the debugger will show a representation similar\nto the one used for instances of the kind indicated by the\n`DisplayStyle` case name when the `Mirror` is used for display.\n"
]
]
},
{
"documentation" : [
[
"summary",
"The return type of functions that do not return normally; a type with no\nvalues.\n"
],
[
"discussion",
"Use `Never` as the return type when declaring a closure, function, or\nmethod that unconditionally throws an error, traps, or otherwise does\nnot terminate.\n"
],
[
"discussion",
"``` \n func crashAndBurn() -> Never {\n fatalError(\"Something very, very bad happened\")\n }\n```\n"
]
],
"declaration" : {
"keyword" : "enum",
"name" : "Never",
"attributes" : [],
"modifiers" : [
{
"name" : "public"
}
],
"cases" : []
}
},
{
"declaration" : {
"name" : "Optional",
"keyword" : "enum",
"cases" : [
{
"name" : "none",
"keyword" : "case",
"context" : "Optional",
"attributes" : [],
"modifiers" : []
},
{
"context" : "Optional",
"attributes" : [],
"modifiers" : [],
"keyword" : "case",
"associatedValue" : [
{
"attributes" : [],
"type" : "Wrapped"
}
],
"name" : "some"
}
],
"modifiers" : [
{
"name" : "public"
}
],
"attributes" : []
},
"documentation" : [
[
"summary",
"A type that represents either a wrapped value or `nil`, the absence of a\nvalue.\n"
],
[
"discussion",
"You use the `Optional` type whenever you use optional values, even if you\nnever type the word `Optional`. Swift's type system usually shows the\nwrapped type's name with a trailing question mark (`?`) instead of showing\nthe full type name. For example, if a variable has the type `Int?`, that's\njust another way of writing `Optional<Int>`. The shortened form is\npreferred for ease of reading and writing code.\n"
],
[
"discussion",
"The types of `shortForm` and `longForm` in the following code sample are\nthe same:\n"
],
[
"discussion",
"``` \n let shortForm: Int? = Int(\"42\")\n let longForm: Optional<Int> = Int(\"42\")\n```\n"
],
[
"discussion",
"The `Optional` type is an enumeration with two cases. `Optional.none` is\nequivalent to the `nil` literal. `Optional.some(Wrapped)` stores a wrapped\nvalue. For example:\n"
],
[
"discussion",
"``` \n let number: Int? = Optional.some(42)\n let noNumber: Int? = Optional.none\n print(noNumber == nil)\n // Prints \"true\"\n```\n"
],
[
"discussion",
"You must unwrap the value of an `Optional` instance before you can use it\nin many contexts. Because Swift provides several ways to safely unwrap\noptional values, you can choose the one that helps you write clear,\nconcise code.\n"
],
[
"discussion",
"The following examples use this dictionary of image names and file paths:\n"
],
[
"discussion",
"``` \n let imagePaths = [\"star\": \"/glyphs/star.png\",\n \"portrait\": \"/images/content/portrait.jpg\",\n \"spacer\": \"/images/shared/spacer.gif\"]\n```\n"
],
[
"discussion",
"Getting a dictionary's value using a key returns an optional value, so\n`imagePaths[\"star\"]` has type `Optional<String>` or, written in the\npreferred manner, `String?`.\n"
],
[
"discussion",
"## Optional Binding\n"
],
[
"discussion",
"To conditionally bind the wrapped value of an `Optional` instance to a new\nvariable, use one of the optional binding control structures, including\n`if let`, `guard let`, and `switch`.\n"
],
[
"discussion",
"``` \n if let starPath = imagePaths[\"star\"] {\n print(\"The star image is at '\\(starPath)'\")\n } else {\n print(\"Couldn't find the star image\")\n }\n // Prints \"The star image is at '/glyphs/star.png'\"\n```\n"
],
[
"discussion",
"## Optional Chaining\n"
],
[
"discussion",
"To safely access the properties and methods of a wrapped instance, use the\npostfix optional chaining operator (`?`). The following example uses\noptional chaining to access the `hasSuffix(_:)` method on a `String?`\ninstance.\n"
],
[
"discussion",
"``` \n if let isPNG = imagePaths[\"star\"]?.hasSuffix(\".png\") {\n print(\"The star image is in PNG format\")\n }\n // Prints \"The star image is in PNG format\"\n```\n"
],
[
"discussion",
"## Using the Nil-Coalescing Operator\n"
],
[
"discussion",
"Use the nil-coalescing operator (`??`) to supply a default value in case\nthe `Optional` instance is `nil`. Here a default path is supplied for an\nimage that is missing from `imagePaths`.\n"
],
[
"discussion",
"``` \n let defaultImagePath = \"/images/default.png\"\n let heartPath = imagePaths[\"heart\"] ?? defaultImagePath\n print(heartPath)\n // Prints \"/images/default.png\"\n```\n"
],
[
"discussion",
"The `??` operator also works with another `Optional` instance on the\nright-hand side. As a result, you can chain multiple `??` operators\ntogether.\n"
],
[
"discussion",
"``` \n let shapePath = imagePaths[\"cir\"] ?? imagePaths[\"squ\"] ?? defaultImagePath\n print(shapePath)\n // Prints \"/images/default.png\"\n```\n"
],
[
"discussion",
"## Unconditional Unwrapping\n"
],
[
"discussion",
"When you're certain that an instance of `Optional` contains a value, you\ncan unconditionally unwrap the value by using the forced\nunwrap operator (postfix `!`). For example, the result of the failable `Int`\ninitializer is unconditionally unwrapped in the example below.\n"
],
[
"discussion",
"``` \n let number = Int(\"42\")!\n print(number)\n // Prints \"42\"\n```\n"
],
[
"discussion",
"You can also perform unconditional optional chaining by using the postfix\n`!` operator.\n"
],
[
"discussion",
"``` \n let isPNG = imagePaths[\"star\"]!.hasSuffix(\".png\")\n print(isPNG)\n // Prints \"true\"\n```\n"
],
[
"discussion",
"Unconditionally unwrapping a `nil` instance with `!` triggers a runtime\nerror.\n"
]
]
},
{
"documentation" : [
[
"summary",
"The sum of types that can be used as a Quick Look representation.\n"
]
],
"declaration" : {
"keyword" : "enum",
"name" : "PlaygroundQuickLook",
"cases" : [
{
"keyword" : "case",
"name" : "text",
"associatedValue" : [
{
"attributes" : [],
"type" : "String"
}
],
"modifiers" : [],
"attributes" : [],
"context" : "PlaygroundQuickLook"
},
{
"modifiers" : [],
"context" : "PlaygroundQuickLook",
"attributes" : [],
"associatedValue" : [
{
"attributes" : [],
"type" : "Int64"
}
],
"name" : "int",
"keyword" : "case"
},
{
"keyword" : "case",
"name" : "uInt",
"associatedValue" : [
{
"type" : "UInt64",
"attributes" : []
}
],
"modifiers" : [],
"context" : "PlaygroundQuickLook",
"attributes" : []
},
{
"attributes" : [],
"context" : "PlaygroundQuickLook",
"modifiers" : [],
"associatedValue" : [
{
"attributes" : [],
"type" : "Float32"
}
],
"name" : "float",
"keyword" : "case"
},
{
"keyword" : "case",
"name" : "double",
"associatedValue" : [
{
"type" : "Float64",
"attributes" : []
}
],
"context" : "PlaygroundQuickLook",
"attributes" : [],
"modifiers" : []
},
{
"attributes" : [],
"context" : "PlaygroundQuickLook",
"modifiers" : [],
"name" : "image",
"associatedValue" : [
{
"type" : "Any",
"attributes" : []
}
],
"keyword" : "case"
},
{
"keyword" : "case",
"name" : "sound",
"associatedValue" : [
{
"type" : "Any",
"attributes" : []
}
],
"modifiers" : [],
"attributes" : [],
"context" : "PlaygroundQuickLook"
},
{
"associatedValue" : [
{
"attributes" : [],
"type" : "Any"
}
],
"name" : "color",
"keyword" : "case",
"modifiers" : [],
"attributes" : [],
"context" : "PlaygroundQuickLook"
},
{
"modifiers" : [],
"context" : "PlaygroundQuickLook",
"attributes" : [],
"keyword" : "case",
"name" : "bezierPath",
"associatedValue" : [
{
"attributes" : [],
"type" : "Any"
}
]
},
{
"modifiers" : [],
"context" : "PlaygroundQuickLook",
"attributes" : [],
"name" : "attributedString",
"associatedValue" : [
{
"type" : "Any",
"attributes" : []
}
],
"keyword" : "case"
},
{
"keyword" : "case",
"associatedValue" : [
{
"type" : "Float64",
"attributes" : []
},
{
"type" : "Float64",
"attributes" : []
},
{
"type" : "Float64",
"attributes" : []
},
{
"attributes" : [],
"type" : "Float64"
}
],
"name" : "rectangle",
"attributes" : [],
"context" : "PlaygroundQuickLook",
"modifiers" : []
},
{
"keyword" : "case",
"name" : "point",
"associatedValue" : [
{
"attributes" : [],
"type" : "Float64"
},
{
"attributes" : [],
"type" : "Float64"
}
],
"modifiers" : [],
"context" : "PlaygroundQuickLook",
"attributes" : []
},
{
"modifiers" : [],
"context" : "PlaygroundQuickLook",
"attributes" : [],
"keyword" : "case",
"name" : "size",
"associatedValue" : [
{
"type" : "Float64",
"attributes" : []
},
{
"attributes" : [],
"type" : "Float64"
}
]
},
{
"modifiers" : [],
"attributes" : [],
"context" : "PlaygroundQuickLook",
"keyword" : "case",
"name" : "bool",
"associatedValue" : [
{
"type" : "Bool",
"attributes" : []
}
]
},
{
"attributes" : [],
"context" : "PlaygroundQuickLook",
"modifiers" : [],
"associatedValue" : [
{
"attributes" : [],
"type" : "Int64"
},
{
"attributes" : [],
"type" : "Int64"
}
],
"name" : "range",
"keyword" : "case"
},
{
"associatedValue" : [
{
"type" : "Any",
"attributes" : []
}
],
"name" : "view",
"keyword" : "case",
"attributes" : [],
"context" : "PlaygroundQuickLook",
"modifiers" : []
},
{
"name" : "sprite",
"associatedValue" : [
{
"type" : "Any",
"attributes" : []
}
],
"keyword" : "case",
"modifiers" : [],
"attributes" : [],
"context" : "PlaygroundQuickLook"
},
{
"associatedValue" : [
{
"attributes" : [],
"type" : "String"
}
],
"name" : "url",
"keyword" : "case",
"modifiers" : [],
"context" : "PlaygroundQuickLook",
"attributes" : []
},
{
"keyword" : "case",
"associatedValue" : [
{
"attributes" : [],
"type" : "[UInt8]"
},
{
"attributes" : [],
"type" : "String"
}
],
"name" : "_raw",
"modifiers" : [],
"context" : "PlaygroundQuickLook",
"attributes" : []
}
],
"modifiers" : [
{
"name" : "public"
}
],
"attributes" : []
}
},
{
"documentation" : [
[
"summary",
"The result of one Unicode decoding step.\n"
],
[
"discussion",
"Each `UnicodeDecodingResult` instance can represent a Unicode scalar value,\nan indication that no more Unicode scalars are available, or an indication\nof a decoding error.\n"
],
[
"seealso",
"`UnicodeCodec.decode(next:)`"
]
],
"declaration" : {
"attributes" : [],
"modifiers" : [
{
"name" : "public"
}
],
"cases" : [
{
"keyword" : "case",
"name" : "scalarValue",
"associatedValue" : [
{
"attributes" : [],
"type" : "UnicodeScalar"
}
],
"modifiers" : [],
"context" : "UnicodeDecodingResult",
"attributes" : []
},
{
"name" : "emptyInput",
"keyword" : "case",
"attributes" : [],
"context" : "UnicodeDecodingResult",
"modifiers" : []
},
{
"name" : "error",
"keyword" : "case",
"context" : "UnicodeDecodingResult",
"attributes" : [],
"modifiers" : []
}
],
"keyword" : "enum",
"name" : "UnicodeDecodingResult"
}
}
],
"structures" : [
{
"declaration" : {
"keyword" : "struct",
"methods" : [
{
"attributes" : [],
"name" : "index(after:)",
"keyword" : "func",
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyBidirectionalCollection",
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"output" : "AnyIndex",
"input" : [
{
"name" : "after",
"attributes" : [],
"type" : "AnyIndex"
}
]
}
},
{
"attributes" : [],
"name" : "formIndex(after:)",
"keyword" : "func",
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyBidirectionalCollection",
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"name" : "after",
"attributes" : [],
"type" : "inout AnyIndex"
}
]
}
},
{
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyBidirectionalCollection",
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"output" : "AnyIndex",
"input" : [
{
"name" : "_",
"type" : "AnyIndex",
"attributes" : []
},
{
"name" : "offsetBy",
"attributes" : [],
"type" : "IntMax"
}
]
},
"attributes" : [],
"name" : "index(_:offsetBy:)",
"keyword" : "func"
},
{
"context" : "AnyBidirectionalCollection",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"input" : [
{
"name" : "_",
"type" : "AnyIndex",
"attributes" : []
},
{
"name" : "offsetBy",
"attributes" : [],
"type" : "IntMax"
},
{
"attributes" : [],
"type" : "AnyIndex",
"name" : "limitedBy"
}
],
"output" : "AnyIndex?"
},
"genericRequirements" : [],
"genericParameters" : [],
"attributes" : [],
"name" : "index(_:offsetBy:limitedBy:)",
"keyword" : "func"
},
{
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyBidirectionalCollection",
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"name" : "_",
"attributes" : [],
"type" : "inout AnyIndex"
},
{
"attributes" : [],
"type" : "IntMax",
"name" : "offsetBy"
}
]
},
"attributes" : [],
"name" : "formIndex(_:offsetBy:)",
"keyword" : "func"
},
{
"keyword" : "func",
"name" : "formIndex(_:offsetBy:limitedBy:)",
"attributes" : [],
"signature" : {
"input" : [
{
"name" : "_",
"type" : "inout AnyIndex",
"attributes" : []
},
{
"name" : "offsetBy",
"attributes" : [],
"type" : "IntMax"
},
{
"name" : "limitedBy",
"attributes" : [],
"type" : "AnyIndex"
}
],
"output" : "Bool"
},
"genericParameters" : [],
"genericRequirements" : [],
"context" : "AnyBidirectionalCollection",
"modifiers" : [
{
"name" : "public"
}
]
},
{
"signature" : {
"output" : "IntMax",
"input" : [
{
"name" : "from",
"type" : "AnyIndex",
"attributes" : []
},
{
"type" : "AnyIndex",
"attributes" : [],
"name" : "to"
}
]
},
"genericRequirements" : [],
"genericParameters" : [],
"context" : "AnyBidirectionalCollection",
"modifiers" : [
{
"name" : "public"
}
],
"name" : "distance(from:to:)",
"keyword" : "func",
"attributes" : []
},
{
"attributes" : [],
"keyword" : "func",
"name" : "index(before:)",
"context" : "AnyBidirectionalCollection",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"output" : "AnyIndex",
"input" : [
{
"type" : "AnyIndex",
"attributes" : [],
"name" : "before"
}
]
},
"genericParameters" : [],
"genericRequirements" : []
},
{
"name" : "formIndex(before:)",
"keyword" : "func",
"attributes" : [],
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"type" : "inout AnyIndex",
"attributes" : [],
"name" : "before"
}
]
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyBidirectionalCollection"
}
],
"name" : "AnyBidirectionalCollection",
"attributes" : [],
"inheritence" : [],
"genericParameters" : [
{
"attributes" : [],
"name" : "Element"
}
],
"genericRequirements" : [],
"modifiers" : [
{
"name" : "public"
}
]
},
"documentation" : [
[
"summary",
"A type-erased wrapper over any collection with indices that\nsupport bidirectional traversal.\n"
],
[
"discussion",
"An `AnyBidirectionalCollection` instance forwards its operations to a base collection having the\nsame `Element` type, hiding the specifics of the underlying\ncollection.\n"
],
[
"seealso",
"`AnyRandomAccessCollection`, `AnyForwardCollection`"
]
]
},
{
"declaration" : {
"genericRequirements" : [],
"genericParameters" : [
{
"attributes" : [],
"name" : "Element"
}
],
"inheritence" : [],
"modifiers" : [
{
"name" : "public"
}
],
"name" : "AnyCollection",
"methods" : [
{
"genericParameters" : [],
"genericRequirements" : [],
"signature" : {
"output" : "AnyIndex",
"input" : [
{
"attributes" : [],
"type" : "AnyIndex",
"name" : "after"
}
]
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyCollection",
"keyword" : "func",
"name" : "index(after:)",
"attributes" : []
},
{
"attributes" : [],
"name" : "formIndex(after:)",
"keyword" : "func",
"context" : "AnyCollection",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"input" : [
{
"name" : "after",
"type" : "inout AnyIndex",
"attributes" : []
}
]
},
"genericRequirements" : [],
"genericParameters" : []
},
{
"context" : "AnyCollection",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"output" : "AnyIndex",
"input" : [
{
"attributes" : [],
"type" : "AnyIndex",
"name" : "_"
},
{
"attributes" : [],
"type" : "IntMax",
"name" : "offsetBy"
}
]
},
"genericParameters" : [],
"genericRequirements" : [],
"attributes" : [],
"keyword" : "func",
"name" : "index(_:offsetBy:)"
},
{
"genericParameters" : [],
"genericRequirements" : [],
"signature" : {
"output" : "AnyIndex?",
"input" : [
{
"name" : "_",
"type" : "AnyIndex",
"attributes" : []
},
{
"name" : "offsetBy",
"attributes" : [],
"type" : "IntMax"
},
{
"name" : "limitedBy",
"type" : "AnyIndex",
"attributes" : []
}
]
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyCollection",
"keyword" : "func",
"name" : "index(_:offsetBy:limitedBy:)",
"attributes" : []
},
{
"attributes" : [],
"name" : "formIndex(_:offsetBy:)",
"keyword" : "func",
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyCollection",
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"attributes" : [],
"type" : "inout AnyIndex",
"name" : "_"
},
{
"name" : "offsetBy",
"type" : "IntMax",
"attributes" : []
}
]
}
},
{
"genericParameters" : [],
"genericRequirements" : [],
"signature" : {
"output" : "Bool",
"input" : [
{
"type" : "inout AnyIndex",
"attributes" : [],
"name" : "_"
},
{
"type" : "IntMax",
"attributes" : [],
"name" : "offsetBy"
},
{
"attributes" : [],
"type" : "AnyIndex",
"name" : "limitedBy"
}
]
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyCollection",
"keyword" : "func",
"name" : "formIndex(_:offsetBy:limitedBy:)",
"attributes" : []
},
{
"context" : "AnyCollection",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"output" : "IntMax",
"input" : [
{
"name" : "from",
"type" : "AnyIndex",
"attributes" : []
},
{
"name" : "to",
"type" : "AnyIndex",
"attributes" : []
}
]
},
"genericRequirements" : [],
"genericParameters" : [],
"attributes" : [],
"name" : "distance(from:to:)",
"keyword" : "func"
}
],
"keyword" : "struct",
"attributes" : []
},
"documentation" : [
[
"summary",
"A type-erased wrapper over any collection with indices that\nsupport forward traversal.\n"
],
[
"discussion",
"An `AnyCollection` instance forwards its operations to a base collection having the\nsame `Element` type, hiding the specifics of the underlying\ncollection.\n"
],
[
"seealso",
"`AnyBidirectionalCollection`, `AnyRandomAccessCollection`"
]
]
},
{
"declaration" : {
"modifiers" : [
{
"name" : "public"
}
],
"genericParameters" : [],
"inheritence" : [],
"genericRequirements" : [],
"attributes" : [],
"keyword" : "struct",
"methods" : [],
"name" : "AnyHashable"
},
"documentation" : [
[
"summary",
"A type-erased hashable value.\n"
],
[
"discussion",
"The `AnyHashable` type forwards equality comparisons and hashing operations\nto an underlying hashable value, hiding its specific underlying type.\n"
],
[
"discussion",
"You can store mixed-type keys in dictionaries and other collections that\nrequire `Hashable` conformance by wrapping mixed-type keys in\n`AnyHashable` instances:\n"
],
[
"discussion",
"``` \n let descriptions: [AnyHashable: Any] = [\n AnyHashable(\"😄\"): \"emoji\",\n AnyHashable(42): \"an Int\",\n AnyHashable(Int8(43)): \"an Int8\",\n AnyHashable(Set([\"a\", \"b\"])): \"a set of strings\"\n ]\n print(descriptions[AnyHashable(42)]!) // prints \"an Int\"\n print(descriptions[AnyHashable(43)]) // prints \"nil\"\n print(descriptions[AnyHashable(Int8(43))]!) // prints \"an Int8\"\n print(descriptions[AnyHashable(Set([\"a\", \"b\"]))]!) // prints \"a set of strings\"\n```\n"
]
]
},
{
"documentation" : [
[
"summary",
"A wrapper over an underlying index that hides the specific underlying type.\n"
],
[
"seealso",
"`AnyCollection`"
]
],
"declaration" : {
"attributes" : [],
"keyword" : "struct",
"methods" : [],
"name" : "AnyIndex",
"modifiers" : [
{
"name" : "public"
}
],
"genericParameters" : [],
"inheritence" : [],
"genericRequirements" : []
}
},
{
"documentation" : [
[
"summary",
"A type-erased iterator of `Element`.\n"
],
[
"discussion",
"This iterator forwards its `next()` method to an arbitrary underlying\niterator having the same `Element` type, hiding the specifics of the\nunderlying `IteratorProtocol`.\n"
],
[
"seealso",
"`AnySequence`"
]
],
"declaration" : {
"attributes" : [],
"name" : "AnyIterator",
"methods" : [
{
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyIterator",
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"output" : "Element?",
"input" : []
},
"attributes" : [],
"name" : "next()",
"keyword" : "func"
}
],
"keyword" : "struct",
"modifiers" : [
{
"name" : "public"
}
],
"genericRequirements" : [],
"inheritence" : [],
"genericParameters" : [
{
"attributes" : [],
"name" : "Element"
}
]
}
},
{
"declaration" : {
"modifiers" : [
{
"name" : "public"
}
],
"genericParameters" : [
{
"name" : "Element",
"attributes" : []
}
],
"inheritence" : [],
"genericRequirements" : [],
"attributes" : [],
"keyword" : "struct",
"name" : "AnyRandomAccessCollection",
"methods" : [
{
"keyword" : "func",
"name" : "index(after:)",
"attributes" : [],
"genericParameters" : [],
"genericRequirements" : [],
"signature" : {
"input" : [
{
"name" : "after",
"attributes" : [],
"type" : "AnyIndex"
}
],
"output" : "AnyIndex"
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyRandomAccessCollection"
},
{
"genericParameters" : [],
"genericRequirements" : [],
"signature" : {
"input" : [
{
"name" : "after",
"type" : "inout AnyIndex",
"attributes" : []
}
]
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyRandomAccessCollection",
"keyword" : "func",
"name" : "formIndex(after:)",
"attributes" : []
},
{
"name" : "index(_:offsetBy:)",
"keyword" : "func",
"attributes" : [],
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"name" : "_",
"attributes" : [],
"type" : "AnyIndex"
},
{
"type" : "IntMax",
"attributes" : [],
"name" : "offsetBy"
}
],
"output" : "AnyIndex"
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyRandomAccessCollection"
},
{
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"type" : "AnyIndex",
"attributes" : [],
"name" : "_"
},
{
"name" : "offsetBy",
"type" : "IntMax",
"attributes" : []
},
{
"name" : "limitedBy",
"type" : "AnyIndex",
"attributes" : []
}
],
"output" : "AnyIndex?"
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyRandomAccessCollection",
"name" : "index(_:offsetBy:limitedBy:)",
"keyword" : "func",
"attributes" : []
},
{
"attributes" : [],
"keyword" : "func",
"name" : "formIndex(_:offsetBy:)",
"context" : "AnyRandomAccessCollection",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"input" : [
{
"attributes" : [],
"type" : "inout AnyIndex",
"name" : "_"
},
{
"name" : "offsetBy",
"type" : "IntMax",
"attributes" : []
}
]
},
"genericParameters" : [],
"genericRequirements" : []
},
{
"attributes" : [],
"name" : "formIndex(_:offsetBy:limitedBy:)",
"keyword" : "func",
"context" : "AnyRandomAccessCollection",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"input" : [
{
"name" : "_",
"attributes" : [],
"type" : "inout AnyIndex"
},
{
"type" : "IntMax",
"attributes" : [],
"name" : "offsetBy"
},
{
"type" : "AnyIndex",
"attributes" : [],
"name" : "limitedBy"
}
],
"output" : "Bool"
},
"genericRequirements" : [],
"genericParameters" : []
},
{
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyRandomAccessCollection",
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"attributes" : [],
"type" : "AnyIndex",
"name" : "from"
},
{
"name" : "to",
"attributes" : [],
"type" : "AnyIndex"
}
],
"output" : "IntMax"
},
"attributes" : [],
"name" : "distance(from:to:)",
"keyword" : "func"
},
{
"signature" : {
"input" : [
{
"attributes" : [],
"type" : "AnyIndex",
"name" : "before"
}
],
"output" : "AnyIndex"
},
"genericParameters" : [],
"genericRequirements" : [],
"context" : "AnyRandomAccessCollection",
"modifiers" : [
{
"name" : "public"
}
],
"keyword" : "func",
"name" : "index(before:)",
"attributes" : []
},
{
"name" : "formIndex(before:)",
"keyword" : "func",
"attributes" : [],
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"name" : "before",
"type" : "inout AnyIndex",
"attributes" : []
}
]
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "AnyRandomAccessCollection"
}
]
},
"documentation" : [
[
"summary",
"A type-erased wrapper over any collection with indices that\nsupport random access traversal.\n"
],
[
"discussion",
"An `AnyRandomAccessCollection` instance forwards its operations to a base collection having the\nsame `Element` type, hiding the specifics of the underlying\ncollection.\n"
],
[
"seealso",
"`AnyForwardCollection`, `AnyBidirectionalCollection`"
]
]
},
{
"documentation" : [
[
"summary",
"A type-erased sequence.\n"
],
[
"discussion",
"An instance of `AnySequence` forwards its operations to an underlying base\nsequence having the same `Element` type, hiding the specifics of the\nunderlying sequence.\n"
],
[
"seealso",
"`AnyIterator`"
]
],
"declaration" : {
"modifiers" : [
{
"name" : "public"
}
],
"genericRequirements" : [],
"genericParameters" : [
{
"name" : "Element",
"attributes" : []
}
],
"inheritence" : [],
"attributes" : [],
"methods" : [],
"name" : "AnySequence",
"keyword" : "struct"
}
},
{
"declaration" : {
"attributes" : [],
"keyword" : "struct",
"methods" : [
{
"keyword" : "func",
"name" : "index(after:)",
"attributes" : [],
"signature" : {
"output" : "Int",
"input" : [
{
"attributes" : [],
"type" : "Int",
"name" : "after"
}
]
},
"genericParameters" : [],
"genericRequirements" : [],
"context" : "Array",
"modifiers" : [
{
"name" : "public"
}
]
},
{
"genericParameters" : [],
"genericRequirements" : [],
"signature" : {
"input" : [
{
"attributes" : [],
"type" : "inout Int",
"name" : "after"
}
]
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "Array",
"keyword" : "func",
"name" : "formIndex(after:)",
"attributes" : []
},
{
"signature" : {
"input" : [
{
"attributes" : [],
"type" : "Int",
"name" : "before"
}
],
"output" : "Int"
},
"genericParameters" : [],
"genericRequirements" : [],
"context" : "Array",
"modifiers" : [
{
"name" : "public"
}
],
"keyword" : "func",
"name" : "index(before:)",
"attributes" : []
},
{
"modifiers" : [
{
"name" : "public"
}
],
"context" : "Array",
"genericParameters" : [],
"genericRequirements" : [],
"signature" : {
"input" : [
{
"type" : "inout Int",
"attributes" : [],
"name" : "before"
}
]
},
"attributes" : [],
"keyword" : "func",
"name" : "formIndex(before:)"
},
{
"modifiers" : [
{
"name" : "public"
}
],
"context" : "Array",
"genericParameters" : [],
"genericRequirements" : [],
"signature" : {
"input" : [
{
"name" : "_",
"type" : "Int",
"attributes" : []
},
{
"type" : "Int",
"attributes" : [],
"name" : "offsetBy"
}
],
"output" : "Int"
},
"attributes" : [],
"keyword" : "func",
"name" : "index(_:offsetBy:)"
},
{
"context" : "Array",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"output" : "Int?",
"input" : [
{
"type" : "Int",
"attributes" : [],
"name" : "_"
},
{
"attributes" : [],
"type" : "Int",
"name" : "offsetBy"
},
{
"name" : "limitedBy",
"type" : "Int",
"attributes" : []
}
]
},
"genericParameters" : [],
"genericRequirements" : [],
"attributes" : [],
"keyword" : "func",
"name" : "index(_:offsetBy:limitedBy:)"
},
{
"name" : "distance(from:to:)",
"keyword" : "func",
"attributes" : [],
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"output" : "Int",
"input" : [
{
"name" : "from",
"type" : "Int",
"attributes" : []
},
{
"name" : "to",
"type" : "Int",
"attributes" : []
}
]
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "Array"
}
],
"name" : "Array",
"modifiers" : [
{
"name" : "public"
}
],
"inheritence" : [],
"genericParameters" : [
{
"attributes" : [],
"name" : "Element"
}
],
"genericRequirements" : []
},
"documentation" : [
[
"summary",
"An ordered, random-access collection.\n"
],
[
"discussion",
"Arrays are one of the most commonly used data types in an app. You use\narrays to organize your app's data. Specifically, you use the `Array` type\nto hold elements of a single type, the array's `Element` type. An array\ncan store any kind of elements---from integers to strings to classes.\n"
],
[
"discussion",
"Swift makes it easy to create arrays in your code using an array literal:\nsimply surround a comma separated list of values with square brackets.\nWithout any other information, Swift creates an array that includes the\nspecified values, automatically inferring the array's `Element` type. For\nexample:\n"
],
[
"discussion",
"``` \n // An array of 'Int' elements\n let oddNumbers = [1, 3, 5, 7, 9, 11, 13, 15]\n\n // An array of 'String' elements\n let streets = [\"Albemarle\", \"Brandywine\", \"Chesapeake\"]\n```\n"
],
[
"discussion",
"You can create an empty array by specifying the `Element` type of your\narray in the declaration. For example:\n"
],
[
"discussion",
"``` \n // Shortened forms are preferred\n var emptyDoubles: [Double] = []\n\n // The full type name is also allowed\n var emptyFloats: Array<Float> = Array()\n```\n"
],
[
"discussion",
"If you need an array that is preinitialized with a fixed number of default\nvalues, use the `Array(repeating:count:)` initializer.\n"
],
[
"discussion",
"``` \n var digitCounts = Array(repeating: 0, count: 10)\n print(digitCounts)\n // Prints \"[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\"\n```\n"
],
[
"discussion",
"# Accessing Array Values\n"
],
[
"discussion",
"When you need to perform an operation on all of an array's elements, use a\n`for`-`in` loop to iterate through the array's contents.\n"
],
[
"discussion",
"``` \n for street in streets {\n print(\"I don't live on \\(street).\")\n }\n // Prints \"I don't live on Albemarle.\"\n // Prints \"I don't live on Brandywine.\"\n // Prints \"I don't live on Chesapeake.\"\n```\n"
],
[
"discussion",
"Use the `isEmpty` property to check quickly whether an array has any\nelements, or use the `count` property to find the number of elements in\nthe array.\n"
],
[
"discussion",
"``` \n if oddNumbers.isEmpty {\n print(\"I don't know any odd numbers.\")\n } else {\n print(\"I know \\(oddNumbers.count) odd numbers.\")\n }\n // Prints \"I know 8 odd numbers.\"\n```\n"
],
[
"discussion",
"Use the `first` and `last` properties for safe access to the value of the\narray's first and last elements. If the array is empty, these properties\nare `nil`.\n"
],
[
"discussion",
"``` \n if let firstElement = oddNumbers.first, let lastElement = oddNumbers.last {\n print(firstElement, lastElement, separator: \", \")\n }\n // Prints \"1, 15\"\n\n print(emptyDoubles.first, emptyDoubles.last, separator: \", \")\n // Prints \"nil, nil\"\n```\n"
],
[
"discussion",
"You can access individual array elements through a subscript. The first\nelement of a nonempty array is always at index zero. You can subscript an\narray with any integer from zero up to, but not including, the count of\nthe array. Using a negative number or an index equal to or greater than\n`count` triggers a runtime error. For example:\n"
],
[
"discussion",
"``` \n print(oddNumbers[0], oddNumbers[3], separator: \", \")\n // Prints \"1, 7\"\n\n print(emptyDoubles[0])\n // Triggers runtime error: Index out of range\n```\n"
],
[
"discussion",
"# Adding and Removing Elements\n"
],
[
"discussion",
"Suppose you need to store a list of the names of students that are signed\nup for a class you're teaching. During the registration period, you need\nto add and remove names as students add and drop the class.\n"
],
[
"discussion",
"``` \n var students = [\"Ben\", \"Ivy\", \"Jordell\"]\n```\n"
],
[
"discussion",
"To add single elements to the end of an array, use the `append(_:)` method.\nAdd multiple elements at the same time by passing another array or a\nsequence of any kind to the `append(contentsOf:)` method.\n"
],
[
"discussion",
"``` \n students.append(\"Maxime\")\n students.append(contentsOf: [\"Shakia\", \"William\"])\n // [\"Ben\", \"Ivy\", \"Jordell\", \"Maxime\", \"Shakia\", \"William\"]\n```\n"
],
[
"discussion",
"You can add new elements in the middle of an array by using the\n`insert(_:at:)` method for single elements and by using\n`insert(contentsOf:at:)` to insert multiple elements from another\ncollection or array literal. The elements at that index and later indices\nare shifted back to make room.\n"
],
[
"discussion",
"``` \n students.insert(\"Liam\", at: 3)\n // [\"Ben\", \"Ivy\", \"Jordell\", \"Liam\", \"Maxime\", \"Shakia\", \"William\"]\n```\n"
],
[
"discussion",
"To remove elements from an array, use the `remove(at:)`,\n`removeSubrange(_:)`, and `removeLast()` methods.\n"
],
[
"discussion",
"``` \n // Ben's family is moving to another state\n students.remove(at: 0)\n // [\"Ivy\", \"Jordell\", \"Liam\", \"Maxime\", \"Shakia\", \"William\"]\n\n // William is signing up for a different class\n students.removeLast()\n // [\"Ivy\", \"Jordell\", \"Liam\", \"Maxime\", \"Shakia\"]\n```\n"
],
[
"discussion",
"You can replace an existing element with a new value by assigning the new\nvalue to the subscript.\n"
],
[
"discussion",
"``` \n if let i = students.index(of: \"Maxime\") {\n students[i] = \"Max\"\n }\n // [\"Ivy\", \"Jordell\", \"Liam\", \"Max\", \"Shakia\"]\n```\n"
],
[
"discussion",
"## Growing the Size of an Array\n"
],
[
"discussion",
"Every array reserves a specific amount of memory to hold its contents. When\nyou add elements to an array and that array begins to exceed its reserved\ncapacity, the array allocates a larger region of memory and copies its\nelements into the new storage. The new storage is a multiple of the old\nstorage's size. This exponential growth strategy means that appending an\nelement happens in constant time, averaging the performance of many append\noperations. Append operations that trigger reallocation have a performance\ncost, but they occur less and less often as the array grows larger.\n"
],
[
"discussion",
"If you know approximately how many elements you will need to store, use the\n`reserveCapacity(_:)` method before appending to the array to avoid\nintermediate reallocations. Use the `capacity` and `count` properties to\ndetermine how many more elements the array can store without allocating\nlarger storage.\n"
],
[
"discussion",
"For arrays of most `Element` types, this storage is a contiguous block of\nmemory. For arrays with an `Element` type that is a class or `@objc`\nprotocol type, this storage can be a contiguous block of memory or an\ninstance of `NSArray`. Because any arbitrary subclass of `NSArray` can\nbecome an `Array`, there are no guarantees about representation or\nefficiency in this case.\n"
],
[
"discussion",
"# Modifying Copies of Arrays\n"
],
[
"discussion",
"Each array has an independent value that includes the values of all of its\nelements. For simple types such as integers and other structures, this\nmeans that when you change a value in one array, the value of that element\ndoes not change in any copies of the array. For example:\n"
],
[
"discussion",
"``` \n var numbers = [1, 2, 3, 4, 5]\n var numbersCopy = numbers\n numbers[0] = 100\n print(numbers)\n // Prints \"[100, 2, 3, 4, 5]\"\n print(numbersCopy)\n // Prints \"[1, 2, 3, 4, 5]\"\n```\n"
],
[
"discussion",
"If the elements in an array are instances of a class, the semantics are the\nsame, though they might appear different at first. In this case, the\nvalues stored in the array are references to objects that live outside the\narray. If you change a reference to an object in one array, only that\narray has a reference to the new object. However, if two arrays contain\nreferences to the same object, you can observe changes to that object's\nproperties from both arrays. For example:\n"
],
[
"discussion",
"``` \n // An integer type with reference semantics\n class IntegerReference {\n var value = 10\n }\n var firstIntegers = [IntegerReference(), IntegerReference()]\n var secondIntegers = firstIntegers\n\n // Modifications to an instance are visible from either array\n firstIntegers[0].value = 100\n print(secondIntegers[0].value)\n // Prints \"100\"\n\n // Replacements, additions, and removals are still visible\n // only in the modified array\n firstIntegers[0] = IntegerReference()\n print(firstIntegers[0].value)\n // Prints \"10\"\n print(secondIntegers[0].value)\n // Prints \"100\"\n```\n"
],
[
"discussion",
"Arrays, like all variable-size collections in the standard library, use\ncopy-on-write optimization. Multiple copies of an array share the same\nstorage until you modify one of the copies. When that happens, the array\nbeing modified replaces its storage with a uniquely owned copy of itself,\nwhich is then modified in place. Optimizations are sometimes applied that\ncan reduce the amount of copying.\n"
],
[
"discussion",
"This means that if an array is sharing storage with other copies, the first\nmutating operation on that array incurs the cost of copying the array. An\narray that is the sole owner of its storage can perform mutating\noperations in place.\n"
],
[
"discussion",
"In the example below, a `numbers` array is created along with two copies\nthat share the same storage. When the original `numbers` array is\nmodified, it makes a unique copy of its storage before making the\nmodification. Further modifications to `numbers` are made in place, while\nthe two copies continue to share the original storage.\n"
],
[
"discussion",
"``` \n var numbers = [1, 2, 3, 4, 5]\n var firstCopy = numbers\n var secondCopy = numbers\n\n // The storage for 'numbers' is copied here\n numbers[0] = 100\n numbers[1] = 200\n numbers[2] = 300\n // 'numbers' is [100, 200, 300, 4, 5]\n // 'firstCopy' and 'secondCopy' are [1, 2, 3, 4, 5]\n```\n"
],
[
"discussion",
"# Bridging Between Array and NSArray\n"
],
[
"discussion",
"When you need to access APIs that require data in an `NSArray` instance\ninstead of `Array`, use the type-cast operator (`as`) to bridge your\ninstance. For bridging to be possible, the `Element` type of your array\nmust be a class, an `@objc` protocol (a protocol imported from Objective-C\nor marked with the `@objc` attribute), or a type that bridges to a\nFoundation type.\n"
],
[
"discussion",
"The following example shows how you can bridge an `Array` instance to\n`NSArray` to use the `write(to:atomically:)` method. In this example, the\n`colors` array can be bridged to `NSArray` because the `colors` array's\n`String` elements bridge to `NSString`. The compiler prevents bridging the\n`moreColors` array, on the other hand, because its `Element` type is\n`Optional<String>`, which does *not* bridge to a Foundation type.\n"
],
[
"discussion",
"``` \n let colors = [\"periwinkle\", \"rose\", \"moss\"]\n let moreColors: [String?] = [\"ochre\", \"pine\"]\n\n let url = NSURL(fileURLWithPath: \"names.plist\")\n (colors as NSArray).write(to: url, atomically: true)\n // true\n\n (moreColors as NSArray).write(to: url, atomically: true)\n // error: cannot convert value of type '[String?]' to type 'NSArray'\n```\n"
],
[
"discussion",
"Bridging from `Array` to `NSArray` takes O(1) time and O(1) space if the\narray's elements are already instances of a class or an `@objc` protocol;\notherwise, it takes O(*n*) time and space.\n"
],
[
"discussion",
"When the destination array's element type is a class or an `@objc`\nprotocol, bridging from `NSArray` to `Array` first calls the `copy(with:)`\n(`- copyWithZone:` in Objective-C) method on the array to get an immutable\ncopy and then performs additional Swift bookkeeping work that takes O(1)\ntime. For instances of `NSArray` that are already immutable, `copy(with:)`\nusually returns the same array in O(1) time; otherwise, the copying\nperformance is unspecified. If `copy(with:)` returns the same array, the\ninstances of `NSArray` and `Array` share storage using the same\ncopy-on-write optimization that is used when two instances of `Array`\nshare storage.\n"
],
[
"discussion",
"When the destination array's element type is a nonclass type that bridges\nto a Foundation type, bridging from `NSArray` to `Array` performs a\nbridging copy of the elements to contiguous storage in O(*n*) time. For\nexample, bridging from `NSArray` to `Array<Int>` performs such a copy. No\nfurther bridging is required when accessing elements of the `Array`\ninstance.\n"
],
[
"note",
"The `ContiguousArray` and `ArraySlice` types are not bridged;\ninstances of those types always have a contiguous block of memory as\ntheir storage."
],
[
"seealso",
"`ContiguousArray`, `ArraySlice`, `Sequence`, `Collection`,\n`RangeReplaceableCollection`"
]
]
},
{
"declaration" : {
"attributes" : [],
"methods" : [
{
"signature" : {
"output" : "Int",
"input" : [
{
"attributes" : [],
"type" : "Int",
"name" : "after"
}
]
},
"genericRequirements" : [],
"genericParameters" : [],
"context" : "ArraySlice",
"modifiers" : [
{
"name" : "public"
}
],
"name" : "index(after:)",
"keyword" : "func",
"attributes" : []
},
{
"context" : "ArraySlice",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"input" : [
{
"name" : "after",
"attributes" : [],
"type" : "inout Int"
}
]
},
"genericRequirements" : [],
"genericParameters" : [],
"attributes" : [],
"name" : "formIndex(after:)",
"keyword" : "func"
},
{
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"name" : "before",
"attributes" : [],
"type" : "Int"
}
],
"output" : "Int"
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "ArraySlice",
"name" : "index(before:)",
"keyword" : "func",
"attributes" : []
},
{
"attributes" : [],
"name" : "formIndex(before:)",
"keyword" : "func",
"modifiers" : [
{
"name" : "public"
}
],
"context" : "ArraySlice",
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"type" : "inout Int",
"attributes" : [],
"name" : "before"
}
]
}
},
{
"name" : "index(_:offsetBy:)",
"keyword" : "func",
"attributes" : [],
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"output" : "Int",
"input" : [
{
"name" : "_",
"type" : "Int",
"attributes" : []
},
{
"name" : "offsetBy",
"type" : "Int",
"attributes" : []
}
]
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "ArraySlice"
},
{
"context" : "ArraySlice",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"output" : "Int?",
"input" : [
{
"name" : "_",
"attributes" : [],
"type" : "Int"
},
{
"name" : "offsetBy",
"type" : "Int",
"attributes" : []
},
{
"attributes" : [],
"type" : "Int",
"name" : "limitedBy"
}
]
},
"genericParameters" : [],
"genericRequirements" : [],
"attributes" : [],
"keyword" : "func",
"name" : "index(_:offsetBy:limitedBy:)"
},
{
"attributes" : [],
"keyword" : "func",
"name" : "distance(from:to:)",
"context" : "ArraySlice",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"output" : "Int",
"input" : [
{
"attributes" : [],
"type" : "Int",
"name" : "from"
},
{
"name" : "to",
"attributes" : [],
"type" : "Int"
}
]
},
"genericParameters" : [],
"genericRequirements" : []
}
],
"name" : "ArraySlice",
"keyword" : "struct",
"modifiers" : [
{
"name" : "public"
}
],
"genericRequirements" : [],
"inheritence" : [],
"genericParameters" : [
{
"name" : "Element",
"attributes" : []
}
]
},
"documentation" : [
[
"summary",
"A slice of an `Array`, `ContiguousArray`, or `ArraySlice` instance.\n"
],
[
"discussion",
"The `ArraySlice` type makes it fast and efficient for you to perform\noperations on sections of a larger array. Instead of copying over the\nelements of a slice to new storage, an `ArraySlice` instance presents a\nview onto the storage of a larger array. And because `ArraySlice`\npresents the same interface as `Array`, you can generally perform the\nsame operations on a slice as you could on the original array.\n"
],
[
"discussion",
"For more information about using arrays, see `Array` and `ContiguousArray`,\nwith which `ArraySlice` shares most properties and methods.\n"
],
[
"discussion",
"# Slices Are Views onto Arrays\n"
],
[
"discussion",
"For example, suppose you have an array holding the number of absences\nfrom each class during a session.\n"
],
[
"discussion",
"``` \n let absences = [0, 2, 0, 4, 0, 3, 1, 0]\n```\n"
],
[
"discussion",
"You want to compare the absences in the first half of the session with\nthose in the second half. To do so, start by creating two slices of the\n`absences` array.\n"
],
[
"discussion",
"``` \n let midpoint = absences.count / 2\n\n let firstHalf = absences.prefix(upTo: midpoint)\n let secondHalf = absences.suffix(from: midpoint)\n```\n"
],
[
"discussion",
"Neither the `firstHalf` nor `secondHalf` slices allocate any new storage\nof their own. Instead, each presents a view onto the storage of the\n`absences` array.\n"
],
[
"discussion",
"You can call any method on the slices that you might have called on the\n`absences` array. To learn which half had more absences, use the\n`reduce(_:_:)` method to calculate each sum.\n"
],
[
"discussion",
"``` \n let firstHalfSum = firstHalf.reduce(0, +)\n let secondHalfSum = secondHalf.reduce(0, +)\n\n if firstHalfSum > secondHalfSum {\n print(\"More absences in the first half.\")\n } else {\n print(\"More absences in the second half.\")\n }\n // Prints \"More absences in the second half.\"\n```\n"
],
[
"important",
"Long-term storage of `ArraySlice` instances is discouraged. A\nslice holds a reference to the entire storage of a larger array, not\njust to the portion it presents, even after the original array's lifetime\nends. Long-term storage of a slice may therefore prolong the lifetime of\nelements that are no longer otherwise accessible, which can appear to be\nmemory and object leakage."
],
[
"discussion",
"# Slices Maintain Indices\n"
],
[
"discussion",
"Unlike `Array` and `ContiguousArray`, the starting index for an\n`ArraySlice` instance isn't always zero. Slices maintain the same\nindices of the larger array for the same elements, so the starting\nindex of a slice depends on how it was created, letting you perform\nindex-based operations on either a full array or a slice.\n"
],
[
"discussion",
"Sharing indices between collections and their subsequences is an important\npart of the design of Swift's collection algorithms. Suppose you are\ntasked with finding the first two days with absences in the session. To\nfind the indices of the two days in question, follow these steps:\n"
],
[
"discussion",
"1) Call `index(where:)` to find the index of the first element in the\n `absences` array that is greater than zero.\n2) Create a slice of the `absences` array starting after the index found in\n step 1.\n3) Call `index(where:)` again, this time on the slice created in step 2.\n Where in some languages you might pass a starting index into an\n `indexOf` method to find the second day, in Swift you perform the same\n operation on a slice of the original array.\n4) Print the results using the indices found in steps 1 and 3 on the\n original `absences` array.\n"
],
[
"discussion",
"Here's an implementation of those steps:\n"
],
[
"discussion",
"``` \n if let i = absences.index(where: { $0 > 0 }) { // 1\n let absencesAfterFirst = absences.suffix(from: i + 1) // 2\n if let j = absencesAfterFirst.index(where: { $0 > 0 }) { // 3\n print(\"The first day with absences had \\(absences[i]).\") // 4\n print(\"The second day with absences had \\(absences[j]).\")\n }\n }\n // Prints \"The first day with absences had 2.\"\n // Prints \"The second day with absences had 4.\"\n```\n"
],
[
"discussion",
"In particular, note that `j`, the index of the second day with absences,\nwas found in a slice of the original array and then used to access a value\nin the original `absences` array itself.\n"
],
[
"note",
"To safely reference the starting and ending indices of a slice,\nalways use the `startIndex` and `endIndex` properties instead of\nspecific values."
]
]
},
{
"documentation" : [
[
"summary",
"A mutable pointer-to-ObjC-pointer argument.\n"
],
[
"discussion",
"This type has implicit conversions to allow passing any of the following\nto a C or ObjC API:\n"
],
[
"discussion",
" - `nil`, which gets passed as a null pointer,\n - an inout argument of the referenced type, which gets passed as a pointer\n to a writeback temporary with autoreleasing ownership semantics,\n - an `UnsafeMutablePointer<Pointee>`, which is passed as-is.\n"
],
[
"discussion",
"Passing pointers to mutable arrays of ObjC class pointers is not\ndirectly supported. Unlike `UnsafeMutablePointer<Pointee>`,\n`AutoreleasingUnsafeMutablePointer<Pointee>` must reference storage that\ndoes not own a reference count to the referenced\nvalue. UnsafeMutablePointer's operations, by contrast, assume that\nthe referenced storage owns values loaded from or stored to it.\n"
],
[
"discussion",
"This type does not carry an owner pointer unlike the other C\\*Pointer types\nbecause it only needs to reference the results of inout conversions, which\nalready have writeback-scoped lifetime.\n"
]
],
"declaration" : {
"attributes" : [],
"name" : "AutoreleasingUnsafeMutablePointer",
"methods" : [],
"keyword" : "struct",
"modifiers" : [
{
"name" : "public"
}
],
"genericRequirements" : [],
"inheritence" : [],
"genericParameters" : [
{
"name" : "Pointee",
"attributes" : []
}
]
}
},
{
"declaration" : {
"modifiers" : [
{
"name" : "public"
}
],
"inheritence" : [],
"genericParameters" : [
{
"attributes" : [],
"name" : "Base"
}
],
"genericRequirements" : [
"Base : _BidirectionalIndexable "
],
"attributes" : [],
"keyword" : "struct",
"methods" : [
{
"context" : "BidirectionalSlice",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"input" : [
{
"name" : "after",
"attributes" : [],
"type" : "BidirectionalSlice.Index"
}
],
"output" : "BidirectionalSlice.Index"
},
"genericRequirements" : [],
"genericParameters" : [],
"attributes" : [],
"name" : "index(after:)",
"keyword" : "func"
},
{
"signature" : {
"input" : [
{
"type" : "inout BidirectionalSlice.Index",
"attributes" : [],
"name" : "after"
}
]
},
"genericParameters" : [],
"genericRequirements" : [],
"context" : "BidirectionalSlice",
"modifiers" : [
{
"name" : "public"
}
],
"keyword" : "func",
"name" : "formIndex(after:)",
"attributes" : []
},
{
"modifiers" : [
{
"name" : "public"
}
],
"context" : "BidirectionalSlice",
"genericParameters" : [],
"genericRequirements" : [],
"signature" : {
"output" : "BidirectionalSlice.Index",
"input" : [
{
"type" : "BidirectionalSlice.Index",
"attributes" : [],
"name" : "before"
}
]
},
"attributes" : [],
"keyword" : "func",
"name" : "index(before:)"
},
{
"signature" : {
"input" : [
{
"name" : "before",
"attributes" : [],
"type" : "inout BidirectionalSlice.Index"
}
]
},
"genericRequirements" : [],
"genericParameters" : [],
"context" : "BidirectionalSlice",
"modifiers" : [
{
"name" : "public"
}
],
"name" : "formIndex(before:)",
"keyword" : "func",
"attributes" : []
},
{
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"output" : "BidirectionalSlice.Index",
"input" : [
{
"name" : "_",
"attributes" : [],
"type" : "BidirectionalSlice.Index"
},
{
"name" : "offsetBy",
"type" : "BidirectionalSlice.IndexDistance",
"attributes" : []
}
]
},
"modifiers" : [
{
"name" : "public"
}
],
"context" : "BidirectionalSlice",
"name" : "index(_:offsetBy:)",
"keyword" : "func",
"attributes" : []
},
{
"modifiers" : [
{
"name" : "public"
}
],
"context" : "BidirectionalSlice",
"genericRequirements" : [],
"genericParameters" : [],
"signature" : {
"input" : [
{
"name" : "_",
"attributes" : [],
"type" : "BidirectionalSlice.Index"
},
{
"type" : "BidirectionalSlice.IndexDistance",
"attributes" : [],
"name" : "offsetBy"
},
{
"type" : "BidirectionalSlice.Index",
"attributes" : [],
"name" : "limitedBy"
}
],
"output" : "BidirectionalSlice.Index?"
},
"attributes" : [],
"name" : "index(_:offsetBy:limitedBy:)",
"keyword" : "func"
},
{
"context" : "BidirectionalSlice",
"modifiers" : [
{
"name" : "public"
}
],
"signature" : {
"output" : "BidirectionalSlice.IndexDistance",
"input" : [
{
"name" : "from",
"attributes" : [],
"type" : "BidirectionalSlice.Index"
},
{
"type" : "BidirectionalSlice.Index",
"attributes" : [],
"name" : "to"
}
]
},
"genericParameters" : [],
"genericRequirements" : [],
"attributes" : [],
"keyword" : "func",
"name" : "distance(from:to:)"
}
],
"name" : "BidirectionalSlice"
},
"documentation" : [
[
"summary",
"A view into a subsequence of elements of another collection.\n"
],
[
"discussion",
"A slice stores a base collection and the start and end indices of the view.\nIt does not copy the elements from the collection into separate storage.\nThus, creating a slice has O(1) complexity.\n"
],
[
"discussion",
"## Slices Share Indices\n"
],
[
"discussion",
"Indices of a slice can be used interchangeably with indices of the base\ncollection. An element of a slice is located under the same index in the\nslice and in the base collection, as long as neither the collection nor\nthe slice has been mutated since the slice was created.\n"
],
[
"discussion",
"For example, suppose you have an array holding the number of absences from\neach class during a session.\n"
],
[
"discussion",
"``` \n var absences = [0, 2, 0, 4, 0, 3, 1, 0]\n```\n"
],
[
"discussion",
"You're tasked with finding the day with the most absences in the second\nhalf of the session. To find the index of the day in question, follow\nthese setps:\n"
],
[
"discussion",
"1) Create a slice of the `absences` array that holds the second half of the\n days.\n2) Use the `max(by:)` method to determine the index of the day\n with the most absences.\n3) Print the result using the index found in step 2 on the original\n `absences` array.\n"
],
[
"discussion",
"Here's an implementation of those steps:\n"
],
[
"discussion",
"``` \n let secondHalf = absences.suffix(absences.count / 2)\n if let i = secondHalf.indices.max(by: { secondHalf[$0] < secondHalf[$1] }) {\n print(\"Highest second-half absences: \\(absences[i])\")\n }\n // Prints \"Highest second-half absences: 3\"\n```\n"
],
[
"discussion",