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

String type as Collection

The String type is defined as a collection of Character types that represent a single character, and has functions such as character enumeration and character count.

chapter4.swift



//StringIndex type How to represent the position in the character string
let string = "abcdefghijelmnopqustuvwxyza"
let startString = string[string.startIndex]
print(startString)

//a

//To get the nth and last index
let string2 = "abcdefghijelmnopqustuvwxyza"
let startString2 = string2.startIndex

let bIndex = string.index(startString2, offsetBy: 8)
print(bIndex)
let bb = string[bIndex]
print(bb)

//i

let endString = string2.endIndex
let cIndex = string.index(endString, offsetBy: -1)
let cc = string[cIndex]
print(cc)

//Get the number of elements using the count property
print(string2.count)
//27

sequence

A data structure that allows you to access the elements sequentially from one direction

Collection is a concept that includes sequences A data structure that allows sequential access from one direction and direct access to specific index values

Sequential access to Sequence protocol elements

chapter4.swift


//forEach(_:)Sequential access to method elements

let darray = [1,2,3,4,5,6,7]
var enumerated = [] as [Int]
darray.forEach ({ element in enumerated.append(element)
})

print(enumerated)

//[1, 2, 3, 4, 5, 6, 7]

//filterd(_:)Narrow down method elements
let filtered = darray.filter ({ element  in element % 2 == 0
})
print(filtered)
//[2, 4, 6]

//map(_:)Convert method elements
//Convert all elements with specific elements
let double = darray.map({element in element * 2})
print(double)
//[2, 4, 6, 8, 10, 12, 14]


//Convert to another type of sequence Int → String

let farray = [1,2,3,4,5]
let converted = farray.map({element in String(element)})
print(converted)

//["1", "2", "3", "4", "5"]


//flatMap(_:)Convert method elements into sequences and add them to one sequence

let sss = [1,2,3]
let ttt = sss.flatMap({value in [value, value * 2]})
print(ttt)

//compacttMap(_:)Convert method elements using processing that can fail

//Converts all elements with a specific process, but ignores values that cannot be converted

let ggg = ["abc","123","kjg","456"]
let integers = ggg.compactMap({value in Int(value)})
print(integers)
//[123, 456]


//reduce(_:)Combine method elements into one value
let sarray = [1,2,3,4,5,6,7,8,9]

let sum = sarray.reduce(0, {result, element in result + element} )
print(sum)
//45

let concat = sarray.reduce("", {result, element in result + String(element)})
print(concat)

//123456789




//Collection protocol Access to elements by subscript
let warray = [1,2,2,3,2,1,2,3,4]

print(warray.count)
//9

print(warray.isEmpty)
//false

print(warray.first)
//Optional(1)

print(warray.last)
//Optional(4)

print(warray[3])
//3


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)