iOS 🍎/Swift

Swift-02 κΈ°λ³Έ λ°μ΄ν„°νƒ€μž…

yongmin.Lee 2021. 1. 26. 09:48

λ³€μˆ˜ ν‚€μ›Œλ“œ

var λ³€μˆ˜μ΄λ¦„: λ°μ΄ν„°νƒ€μž… = κ°’

ex) var varName: Int = 2

 

μƒμˆ˜ν‚€μ›Œλ“œ

let λ³€μˆ˜μ΄λ¦„: λ°μ΄ν„°νƒ€μž… = κ°’

ex) let varName: String = "abc"

 

λ°μ΄ν„°νƒ€μž…

  • Int or UInt − This is used for whole numbers. More specifically, you can use Int32, Int64 to define 32 or 64 bit signed integer, whereas UInt32 or UInt64 to define 32 or 64 bit unsigned integer variables. For example, 42 and -23.
  • Float − This is used to represent a 32-bit floating-point number and numbers with smaller decimal points. For example, 3.14159, 0.1, and -273.158.
  • Double − This is used to represent a 64-bit floating-point number and used when floating-point values must be very large. For example, 3.14159, 0.1, and -273.158.
  • Bool − This represents a Boolean value which is either true or false.
  • String − This is an ordered collection of characters. For example, "Hello, World!"
  • Character − This is a single-character string literal. For example, "C"
  • Optional − This represents a variable that can hold either a value or no value.
  • Tuples − This is used to group multiple values in single Compound Value.

Any, AnyObject, nil

  • Any : Swift의 λͺ¨λ“  νƒ€μž…μ„ μ§€μΉ­ν•˜λŠ” ν‚€μ›Œλ“œ
  • AnyObject : λͺ¨λ“  클래슀 νƒ€μž…μ„ μ§€μΉ­ν•˜λŠ” ν”„λ‘œν† μ½œ
  • nil : μ—†μŒμ„ μ˜λ―Έν•˜λŠ” ν‚€μ›Œλ“œ

Collection, μ»¬λ ‰μ…˜νƒ€μž…

Array : μˆœμ„œκ°€ μžˆλŠ” 리슀트 μ»¬λ ‰μ…˜

Dictionary : 킀와 κ°’μ˜ 쌍으둜 이루어진 μ»¬λ ‰μ…˜

Set : μˆœμ„œκ°€ μ—†κ³ , 멀버가 μœ μΌν•œ μ»¬λ ‰μ…˜

 

1. Array

// 빈 Int Array 생성
var integers: Array<Int> = Array<Int>()
// var integers: Array<Int> = [Int]()
// var integers: Array<Int> = []
// var integers: [Int] = Array<Int>()
// var integers: [Int] = [Int]()
 var integers: [Int] = []
// var integers = [Int]()

integers.append(1)
integers.append(100)

// Int νƒ€μž…μ΄ μ•„λ‹ˆλ―€λ‘œ Array에 μΆ”κ°€ν•  수 μ—†μŠ΅λ‹ˆλ‹€
//integers.append(101.1)

print(integers)	// [1, 100]

// 멀버 포함 μ—¬λΆ€ 확인
print(integers.contains(100)) // true
print(integers.contains(99)) // false

// 멀버 ꡐ체
integers[0] = 99

// 멀버 μ‚­μ œ
integers.remove(at: 0)
integers.removeLast()
integers.removeAll()

// 멀버 수 확인
print(integers.count)

 

2. Dictionary

// Keyκ°€ String νƒ€μž…μ΄κ³  Valueκ°€ Any인 빈 Dictionary 생성
var anyDictionary: Dictionary<String, Any> = [String: Any]()

// 같은 ν‘œν˜„
// var anyDictionary: Dictionary <String, Any> = Dictionary<String, Any>()
// var anyDictionary: Dictionary <String, Any> = [:]
 var anyDictionary: [String: Any] = Dictionary<String, Any>()
 var anyDictionary: [String: Any] = [String: Any]()
 var anyDictionary: [String: Any] = [:]
// var anyDictionary = [String: Any]()

// 킀에 ν•΄λ‹Ήν•˜λŠ” κ°’ ν• λ‹Ή
anyDictionary["someKey"] = "value"
anyDictionary["anotherKey"] = 100

print(anyDictionary) // ["someKey": "value", "anotherKey": 100]

// 킀에 ν•΄λ‹Ήν•˜λŠ” κ°’ λ³€κ²½
anyDictionary["someKey"] = "dictionary"
print(anyDictionary) ["someKey": "dictionary", "anotherKey": 100]

// 킀에 ν•΄λ‹Ήν•˜λŠ” κ°’ 제거
anyDictionary.removeValue(forKey: "anotherKey")
anyDictionary["someKey"] = nil
print(anyDictionary)

//킀에 ν•΄λ‹Ήν•˜λŠ” 값을 λ‹€λ₯Έ λ³€μˆ˜λ‚˜ μƒμˆ˜μ— ν• λ‹Ήν•˜κ³ μž ν•  λ•ŒλŠ” μ˜΅μ…”λ„κ³Ό νƒ€μž… μΊμŠ€νŒ… νŒŒνŠΈμ—μ„œ λ‹€λ£Έ

 

3. Set

// 빈 Int Set 생성
var integerSet: Set<Int> = Set<Int>()

// Set μ›μ†Œ μ‚½μž…
integerSet.insert(1)
integerSet.insert(100)
integerSet.insert(99)
integerSet.insert(99)
integerSet.insert(99)

print(integerSet) // [100, 99, 1]
print(integerSet.contains(1)) // true
print(integerSet.contains(2)) // false

integerSet.remove(100)
integerSet.removeFirst()

print(integerSet.count) // 1

// SetλŠ” 집합 연산에 κ½€ μœ μš©ν•©λ‹ˆλ‹€
let setA: Set<Int> = [1, 2, 3, 4, 5]
let setB: Set<Int> = [3, 4, 5, 6, 7]

// 합집합
let union: Set<Int> = setA.union(setB)
print(union) // [2, 4, 5, 6, 7, 3, 1]

// 합집합 μ˜€λ¦„μ°¨μˆœ μ •λ ¬
let sortedUnion: [Int] = union.sorted()
print(sortedUnion) // [1, 2, 3, 4, 5, 6, 7]

// ꡐ집합
let intersection: Set<Int> = setA.intersection(setB)
print(intersection) // [5, 3, 4]

// 차집합
let subtracting: Set<Int> = setA.subtracting(setB)
print(subtracting) // [2, 1]