Introduction to swift practice output Type representing the Chapter 4 collection Part 1

A collection is a collection of values

Represents an array Array type

Represents a dictionary Dictionary <Key, Value> type

Represents a range Range type

Array type representing an array

Actually used like Array type, Array type

chapter4.swift


let a = [1,2,3]
let b = ["a","b","c"]

let array :[Int] = []

var strings = ["aaa","bbb","ccc"]
var strings1 = strings[0]
print(strings1)

//aaa

strings[2] = "gagaga"
print(strings)

["aaa", "bbb", "gagaga"]

//Add to the end
strings.append("yamato")
print(strings)

//["aaa", "bbb", "gagaga", "yamato"]

//Add to any position
strings.insert("mama", at: 2)
print(strings)

//["aaa", "bbb", "mama", "gagaga", "yamato"]


//There are 3 types of deletion
var integer = [1,2,3,4,5]
integer.remove(at: 2)
integer

integer.removeLast()
integer

integer.removeAll()
integer

Dictionary <Key, Value> type representing a dictionary

chapter4.swift


let dictionary = ["Key":1]
let value = dictionary["key"]
print(value)

//["key": 1]

//Change
var dictionary1 = ["key": 2]
dictionary1["key"] = 1
print(dictionary1)
//add to
var dictionary2 = ["key":3]
dictionary2["key2"] = 4
print(dictionary2)

//["key2": 4, "key": 3]

//Delete
var dictionary3 = ["key":5]
dictionary3["key"] = nil
print(dictionary3)

//[:]

Range type Type that represents a range

chapter4.swift


//Range not including the end countableRange<Bound>Type
let range = 1..<4
for value in range {
    print(value)
}

//1
//2
//3

//...Type including the end CountableClosedRange<Bound>Type

let range2 = 1...4
for value in range2 {
    print(value)
}

//1
//2
//3
//4


let range3 = 1...5
print(range3.upperBound)
print(range3.lowerBound)

//5
//1


//Determining if the value is in the range

let range4 = 1..<10

print(range4.contains(4))
print(range4.contains(10))

//true
//false


Recommended Posts

Introduction to swift practice output Type representing the Chapter 4 collection Part 1
Introduction to swift practice output Type representing the Chapter 4 collection Part 2
Introduction to swift practice output Chapter 5 Part 2
Introduction to swift practice output Chapter5
[Note] About the introduction to Swift practice Chapter 11
Output of the book "Introduction to Java"
Output about the method Part 1
[Swift] Type type-Class first part-
[Swift] Type component ~ Property Part 2 ~
Introduction to Spring Boot Part 1
Introduction to Linux Container / Docker (Part 1)
Introduction to Linux Container / Docker (Part 2)