๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/Swift

[Swift] ์Šค์œ„ํ”„ํŠธ ๋ฐ์ดํ„ฐ ํƒ€์ž…(Swift data type) - 1.๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…, Any, AnyObject, nil, Typealias

๐Ÿ’ก Swift ๋ฐ์ดํ„ฐ ํƒ€์ž… ํŠน์ง•

 

- ๋ชจ๋“  ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ์ด๋ฆ„์€  ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ์ž…๋ ฅํ•œ๋‹ค. 

- ๋ชจ๋“  ๋ฐ์ดํ„ฐ ํƒ€์ž…(IntFloatDoubleBoolStringArrayDictionarySet ๋“ฑ)์€ ๋ชจ๋‘ Struct (๊ตฌ์กฐ์ฒด)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.

(์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด, ์šฐ๋ฆฌ๊ฐ€ ์ˆซ์ž๋ฅผ ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์„ ๋•Œ Int ๋ผ๊ณ  ์ž…๋ ฅํ•ด์„œ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก Swift์— ๋ฏธ๋ฆฌ ๊ตฌ์กฐ์ฒด ๋ผ๋Š” ๊ฒƒ์œผ๋กœ ๋งŒ๋“ค์–ด์ ธ ์žˆ๋‹ค๋Š” ์˜๋ฏธ)

<-> Reference Type : class, function, closure ๋“ฑ

- ์Šค์œ„ํ”„ํŠธ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž… ์—„๊ฒฉํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๋ฏ€๋กœ ์•”์‹œ์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๋ณ€ํ˜•์œผ๋กœ๋ถ€ํ„ฐ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋Š” ์˜ˆ๊ธฐ์น˜ ์•Š์€ ๊ฒฐ๊ณผ๋ฅผ ์ผ์œผํ‚ค๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•ด์•ผ ํ•œ๋‹ค.

 


1. Swift ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…

   1 - 1. ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž… (Bool, Int, UInt, Float, Double, Character, String)

   1 - 2. Any, AnyObject, nil, Typealias


2. ์ปฌ๋ ‰์…˜ ํƒ€์ž… (Array, Dictionary, Set)

>> https://lxxyeon.tistory.com/140

 

[Swift] ์Šค์œ„ํ”„ํŠธ ๋ฐ์ดํ„ฐ ํƒ€์ž…(Swift data type) - 2. ์ปฌ๋ ‰์…˜ ํƒ€์ž…(Array, Dictionary, Set)

1. Swift ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…  1 - 1. ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž… (Bool, Int, UInt, Float, Double, Character, String)  1 - 2. Any, AnyObject, nil, Typealias 2. ์ปฌ๋ ‰์…˜ ํƒ€์ž… (Array, Dictionary, Set) 3. ์—ด๊ฑฐ..

lxxyeon.tistory.com

3. ์—ด๊ฑฐํ˜• ํƒ€์ž… (enum)

https://lxxyeon.tistory.com/143

 

[Swift] ์Šค์œ„ํ”„ํŠธ ๋ฐ์ดํ„ฐ ํƒ€์ž…(Swift data type) - 3. ์—ด๊ฑฐํ˜• ํƒ€์ž… (enum)

1. Swift ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…  1 - 1. ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž… (Bool, Int, UInt, Float, Double, Character, String)  1 - 2. Any, AnyObject, nil, Typealias >>https://lxxyeon.tistory.com/23 [Swift] ์Šค์œ„ํ”„ํŠธ..

lxxyeon.tistory.com


1.  Swift ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…

   1 - 1. ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…

Bool true์™€ false๋งŒ์„ ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๋Š” ํƒ€์ž…
Int ์ •์ˆ˜ ํƒ€์ž…. ํ˜„์žฌ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 64๋น„ํŠธ ์ •์ˆ˜ํ˜•.
UInt ์–‘์˜ ์ •์ˆ˜ ํƒ€์ž…. ํ˜„์žฌ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 64๋น„ํŠธ ์–‘์˜ ์ •์ˆ˜ํ˜•.
Float ์‹ค์ˆ˜ ํƒ€์ž…. 32๋น„ํŠธ ๋ถ€๋™์†Œ์ˆ˜ํ˜•.
Double ์‹ค์ˆ˜ํƒ€์ž…. 64๋น„ํŠธ ๋ถ€๋™์†Œ์ˆ˜ํ˜•.
Character ๋ฌธ์ž ํƒ€์ž…. ์œ ๋‹ˆ์ฝ”๋“œ ์‚ฌ์šฉ. ํฐ๋”ฐ์˜ดํ‘œ(“”) ์‚ฌ์šฉ.
String ๋ฌธ์ž์—ด ํƒ€์ž…. ์œ ๋‹ˆ์ฝ”๋“œ ์‚ฌ์šฉ. ํฐ๋”ฐ์˜ดํ‘œ(“”) ์‚ฌ์šฉ.

๐ŸŽ Bool

  • ๋ถˆ(bool) or ๋ถˆ๋ฆฌ์–ธ(boolean)
  • true, false์˜ ๋‹จ์ผ ๊ฐ’์„ ๊ฐ€์ง
var someBool: Bool = true
someBool = false
// someBool = 0 // ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๋ฐœ์ƒ >> 0,1 ์€ int ํƒ€์ž…!!!!์œผ๋กœ ์ธ์‹

 

[์‚ผํ•ญ์—ฐ์‚ฐ์ž]

  • a ?  b : c
  • a๊ฐ€ true๋ฉด b๋ฐ˜ํ™˜, false๋ฉด c๋ฐ˜ํ™˜
let test: Int = 1
let res = (test == 1) ? "true": "false"
print(res)	//true

๐ŸŽ Int, UInt

  • ์ •์ˆ˜(Integer)
  •  Int : ์–‘์ˆ˜๋ถ€ํ„ฐ ์Œ์ˆ˜(Int8 : -128 ~ 127)
// Int - Int8, Int16, Int32, Int64
// ex) Int8 - Int๋ฅผ 8bitํฌ๊ธฐ๋กœ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค.
// ์ผ๋ฐ˜์ ์œผ๋กœ Int๋Š” ์‹คํ–‰๋˜๋Š” ํ™˜๊ฒฝ์˜ CPU์— ๋งž์ถ”์–ด ๊ฐ’์˜ ๋ฒ”์œ„๊ฐ€ ๋ณ€ํ•จ
// ex) 64bit OS -> Int = Int64, 32bit OS -> Int = Int32
// i ๊ฐ€ ๋Œ€๋ฌธ์ž
var num : Int = 0;
var num8 : Int8 = 0;
var num16 : Int16 = 0;
var num32 : Int32 = 0;
var num64 : Int64 = 0;
         
// Int๋Š” SignedInteger๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌ์กฐ์ฒด์˜ ์ผ์ข…
//  -> Int.init(), Int.max, Int.min ๋“ฑ์˜ ๋ณ€์ˆ˜ ๋ฐ ๋ฉ”์†Œ๋“œ๋“ค์ด ์กด์žฌ
var no0 : Int = Int.init(); // 0์œผ๋กœ ์ดˆ๊ธฐํ™”
var no1 : Int = Int.init(1); // 1๋กœ ์ดˆ๊ธฐํ™”
print("Int   : " + String(Int.min) + " ~ " + String(Int.max));
print("Int8  : " + String(Int8.min) + " ~ " + String(Int8.max));
print("Int16 : " + String(Int16.min) + " ~ " + String(Int16.max));
print("Int32 : " + String(Int32.min) + " ~ " + String(Int32.max));
print("Int64 : " + String(Int64.min) + " ~ " + String(Int64.max));

[์‹คํ–‰ ๊ฒฐ๊ณผ]

 

  •  UInt : ์–‘์ˆ˜๋งŒ(UInt8 : 0~255)
// UInt - UInt8, UInt16, UInt32, UInt64
// UInt๋„ Int์™€ ๋™์ผํ•˜๊ฒŒ ์‹คํ–‰๋˜๋Š” ํ™˜๊ฒฝ์˜ CPU์— ๋งž์ถ”์–ด ๊ฐ’์˜ ๋ฒ”์œ„๊ฐ€ ๋ณ€ํ•จ
var num : UInt = 0;
var num8 : UInt8 = 0;
var num16 : UInt16 = 0;
var num32 : UInt32 = 0;
var num64 : UInt64 = 0;

// UInt๋Š” UnSignedInteger๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌ์กฐ์ฒด์˜ ์ผ์ข…
//  -> UInt.init(), UInt.max, UInt.min ๋“ฑ์˜ ๋ณ€์ˆ˜ ๋ฐ ๋ฉ”์†Œ๋“œ๋“ค์ด ์กด์žฌ
var no0 : UInt = UInt.init(); // 0์œผ๋กœ ์ดˆ๊ธฐํ™”
var no1 : UInt = UInt.init(1); // 1๋กœ ์ดˆ๊ธฐํ™”
print("UInt   : " + String(UInt.min) + " ~ " + String(UInt.max));
print("UInt8  : " + String(UInt8.min) + " ~ " + String(UInt8.max));
print("UInt16 : " + String(UInt16.min) + " ~ " + String(UInt16.max));
print("UInt32 : " + String(UInt32.min) + " ~ " + String(UInt32.max));
print("UInt64 : " + String(UInt64.min) + " ~ " + String(UInt64.max));

[์‹คํ–‰ ๊ฒฐ๊ณผ]


๐ŸŽ Float, Double

  • ์‹ค์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜์ (Floating-Point Numbers)
  •  Float : 32Bit > ์†Œ์ˆ˜์  ์ดํ•˜ 6์ž๋ฆฌ
  •  Double : 64Bit > ์†Œ์ˆ˜์  ์ดํ•˜ 15์ž๋ฆฌ
let float_pi : Float = 3.141592653589793238462643383279502884197169399375105820
let double_pi : Double = 3.141592653589793238462643383279502884197169399375105820
        
print("Float์œผ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅํ•œ ์‹ค์ˆ˜ : \(float_pi)")
print("Double์œผ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅํ•œ ์‹ค์ˆ˜ : \(double_pi)")

 

[์‹คํ–‰ ๊ฒฐ๊ณผ]


๐ŸŽ Character, String

  • ๋ฌธ์ž
    • ํฐ ๋”ฐ์˜ดํ‘œ(")๋กœ ํ‘œํ˜„
//Character 
var someCharacter: Character = "โค๏ธ"
someCharacter = "๊ฐ€"
// someCharacter = "ํ•˜ํ•˜ํ•˜" // ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๋ฐœ์ƒ >> ๋ฌธ์ž์—ด ํƒ€์ž…

//for-in loop๋กœ ์ ‘๊ทผ
for character in "Dog!๐Ÿถ" {
    print(character)
}
// D
// o
// g
// !
// ๐Ÿถ

//๋ฌธ์ž ๋ฐฐ์—ด
let catCharacters: [Character] = ["C", "a", "t", "!", "๐Ÿฑ"]
let catString = String(catCharacters)
print(catString)
// Prints "Cat!๐Ÿฑ"

 

  •  ๋ฌธ์ž์—ด
    • ํฐ ๋”ฐ์˜ดํ‘œ(")๋กœ ํ‘œํ˜„
    • ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ : + ์—ฐ์‚ฌ์ž ์‚ฌ์šฉ
    • ๋นˆ ๋ฌธ์ž์—ด ํ™•์ธ : isEmpty ํ•จ์ˆ˜ ์‚ฌ์šฉ
//String 
var someString: String = "ํ•˜ํ•˜ํ•˜"
someString += "ํ˜ธํ˜ธํ˜ธ"

์—ฌ๋Ÿฌ์ค„ ๋ฌธ์ž์—ด์€ ํฐ๋”ฐ์˜ดํ‘œ ์„ธ ๊ฐœ ์‚ฌ์šฉ.
someString = """
์—ฌ๋Ÿฌ์ค„ ๋ฌธ์ž์—ด์„
์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฒซ ์ค„์— ๊ฒน๋”ฐ์˜ดํ‘œ ์„ธ ๊ฐœ,
๋งˆ์ง€๋ง‰ ์ค„์— ๊ฒน๋”ฐ์˜ดํ‘œ ์„ธ ๊ฐœ๋ฅผ
์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
"""

someString = """
๊ฒน๋”ฐ์˜ดํ‘œ ์„ธ ๊ฐœ์ธ ์ค„(์ฒซ์ค„, ๋์ค„)์—์„œ
์ค„ ๋ฐ”๊ฟˆ์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
"""

/*
someString = """์˜ค๋ฅ˜๋ฐœ์ƒ
์˜ค๋ฅ˜๋ฐœ์ƒ"""
*/

//๋นˆ ๋ฌธ์ž์—ด ์ดˆ๊ธฐํ™”
var emtpryString = ""
var anotherEmptyString = String()

//๋นˆ ๋ฌธ์ž์—ด ์—ฌ๋ถ€ ํ™•์ธ - isEmpty ์‚ฌ์šฉ
if emptyString.isEmpty {
    print("Nothing to see here")
}
// Prints "Nothing to see here"โ€‹

   1 - 2. Any, AnyObject, nil, Typealias


๐ŸŽ Any

  • Swift์˜ ํ•จ์ˆ˜ ํƒ€์ž…์„ ํฌํ•จํ•ด ๋ชจ๋“  ํƒ€์ž…์„ ์ง€์นญํ•˜๋Š” ํ‚ค์›Œ๋“œ(Int, String, ํ•จ์ˆ˜, ํด๋กœ์ €๊นŒ์ง€ ํฌํ•จ)
var someAny: Any = 100
someAny = "์–ด๋–ค ํƒ€์ž…๋„ ์ˆ˜์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค"
someAny = 123.12

//Double ํƒ€์ž…์˜ ๋ณ€์ˆ˜์— Any ํƒ€์ž…์˜ ๊ฐ’์€ ๋„ฃ์„ ์ˆ˜ ์—†์Œ!!
//let someDouble : Double = someAny >> ์˜ค๋ฅ˜!!!

*json ํŒŒ์‹ฑํ• ๋•Œ, ๋”•์…”๋„ˆ๋ฆฌ ์‚ฌ์šฉํ•˜๋Š”๋ฐ key๋Š” ์ฃผ๋กœ string, value๋Š” ํƒ€์ž…์ด ๋‹ค ๋‹ค๋ฅด๋‹ˆ๊นŒ Any๋กœ ๋งŽ์ด ํ•จ.


๐ŸŽ AnyObject

  • class instance๋งŒ ๋‹ค๋ฃธ, ๋ชจ๋“  ํด๋ž˜์Šค ํƒ€์ž…์„ ์ง€์นญํ•˜๋Š” ํ”„๋กœํ† ์ฝœ
class SomeClass {}

var someAnyObject: AnyObject = SomeClass()
//AnyObject๋Š” ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋งŒ ์ˆ˜์šฉ ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹ˆ๋ฉด ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
//someAnyObject = 123.12    // ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๋ฐœ์ƒ

๐ŸŽ nil

  • ์—†์Œ์„ ์˜๋ฏธํ•˜๋Š” ํ‚ค์›Œ๋“œ (๋‹ค๋ฅธ ์–ธ์–ด์˜ NULL, Null, null ๋“ฑ๊ณผ ๊ฐ™์€ ํ‘œํ˜„)
  • ์•„๋ž˜ ์ฝ”๋“œ์—์„œ someAny๋Š” Any ํƒ€์ž…์ด๊ณ , someAnyObject๋Š” AnyObject ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— nil์„ ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
    nil์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์€ ์˜ต์…”๋„ ํŒŒํŠธ์—์„œ ๋‹ค๋ฃฐ ์˜ˆ์ •.
someAny = nil         // ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๋ฐœ์ƒ >> any ํƒ€์ž… >> ์–ด๋–ค ๋ฐ์ดํ„ฐ๊ฐ’๋„ ๋˜์ง€๋งŒ ๋นˆ๊ฐ’์€ ์•ˆ๋จ 
someAnyObject = nil   // ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๋ฐœ์ƒ

๐ŸŽ Typealias

  • ํƒ€์ž… ๋ณ„์นญ : ๊ธฐ์กด์— ์„ ์–ธ๋˜์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ์ž„์˜๋กœ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ๋ถ€์—ฌ
typealias MyInt = Int
typealias MyString = String

let num: MyInt = 10
var title: MyString = "techYeon"

 

728x90
๋ฐ˜์‘ํ˜•