for var i = 0; i < 10; i+=1 
{ // for i in 0..<10 로 수정해야 함
print(i)
}
-------------------------------------
for var i in 0..<10
{
    print(i)
}
----------------------------------
for _ in 0..<5  // _  <- 사용 가능
{
    print("Han")
}
-------------------------------------
let name = ["a","b","c","d"]
for i in name[2...]
{
    print(i)
}
----------------------------------------------------------
let numberOfLegs = ["Spider": 8, "Ant": 6, "Dog": 4]
//dictionary는 key:value형식의 배열
for (animalName, legCount) in numberOfLegs 
{
    print("\(animalName)s have \(legCount) legs")
}
-------------------------------------
// 감소하는 경우
for i in (5..<0).reversed()
{
    print(i)
}
-------------------------------------
// 2씩 증가하는 경우
for i in stride(from: 0, to: 10, by: 2) 
{
    print(i)
}
-------------------------------------

다른 언어들과 다른 Swift의 for문 사용법...

 

for i in 1..<10 
{
if i > 5 break
//error: expected '{' after 'if' condition
print(i)
}

Swift에선 조건식 다음 한 줄만 있어도  { } 중괄호를 사용해야 한다.

 

var a = 1
var b = 2
var c = 3
var d = 4
if a < b && d > c {
print("yes")
}
if a < b, d > c { 
print("yes")
}

Swift if문에서  ,(콤마)를 사용할 경우 AND와 동일하다

 

let someCharacter: Character = "z"
switch someCharacter 
{
case "a":
	print("The first letter of the alphabet")
case "z":
	print("The last letter of the alphabet")
default:
	print("Some other character")
}
-------------------------------------------------
let anotherCharacter: Character = "a"
switch anotherCharacter 
{
case "a", "A":
	print("A 글자")
default:
	print("A글자 아님")
}
----------------------------------------------------
let weight = 60.0
let height = 170.0
let bmi = weight / (height*height*0.0001) // kg/m*m
var body = ""
switch bmi {
case 40...:
    body = "3단계 비만"
case  30..<40:
    body = "2단계 비만"
case 25..<30: 
    body = "1단계 비만"
case 18.5..<25:
    body = "정상"
default:
    body = "저체중"
}
print("BMI:\(bmi), 판정:\(body)")

Switch~Case 문, break문이 포함되어 있다.

Switch~Case 문에서 ,(콤마)는 OR와 동일하다

Switch~Case 문에서도 범위지정 매칭을 사용할 수 있다.

 

var temperature = 60
switch (temperature)
{
case 0...49 where temperature % 2 == 0:
	print("Cold and even")
case 50...79 where temperature % 2 == 0:
	print("Warm and even")
case 80...110 where temperature % 2 == 0:
	print("Hot and even")
default:
	print("Temperature out of range or odd")
}

where절은 다양한 식에 부가적인 조건을 추가하기 위하여 사용한다

값이 속하는 범위뿐만 아니라 그 숫자가 홀수인지 짝수인지도 검사

 

var value = 4
switch (value)
{
case 4:
	print("4")
	fallthrough
case 3:
	print("3")
	fallthrough
case 2:
	print("2")
	fallthrough
default:
	print("1")
}

내장되어 있는 break문에 걸리지않고 순차적으로 실행되기 위해선 fallthrough 이라는 키워드를 사용하면 된다

 

func sayHello() 
{ //리턴값 없으면( -> Void ) 지정하지 않아도 됨
	print("Hello")
}
func message(name: String, age: Int) -> String 
{
	return("\(name) \(age)")
}

func add(first x: Int, second y: Int) -> Int {
//외부 내부:자료형,외부 내부:자료형 -> 리턴형
	return(x+y) 
}
add(first:10, second:20)
-----------------------------------------------------------------
func add(x:Int, y:Int) -> Int{
    return (x+y)
}

print(add(x:10,y:10))

리턴값은 -> 이후에 작성한다

함수 선언 시 외부 내부:자료형, 외부 내부:자료형 -> 리턴형

함수 정의할 때는 내부 매개변수명을 사용한다

함수 호출할 때는 외부 매개변수명을 사용한다

------------------------------------------------------------------------------------------------------------

외부 매개변수명 생략하면 내부 매개변수명이 외부 매개변수명까지 겸한다

 

func add(_ x: Int, _ y: Int) -> Int {
	return(x+y) 
}
print(add(1,2))
print(type(of:add))

( _ ) 사용시 외부 매개 변수명을 생략할 수 있다.

 

func add(_ x: Int, with y: Int) -> Int {
	return(x+y) 
}
print(add(1,with:2))
print(type(of:add))

첫번째 외부 매개 변수만 생략하는 경우이다. Object-c 의 방식이며 가장 많이 사용되는 방식이다.

'iOS' 카테고리의 다른 글

iOS (?)  (0) 2023.11.14
iOS (10)  (0) 2023.11.07
iOS (5)  (1) 2023.10.17
iOS (2)  (0) 2023.09.19
2023-09-12  (0) 2023.09.12

+ Recent posts