-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
defer란 무엇인지 설명하시오 #36
Labels
Comments
defer Statement
func sayNumber() {
defer {
print("print check : 1")
}
print("print check : 2")
}
// print check : 2
// print check : 1
func sayCheckNumber() {
defer {
for i in 0..<5 {
print("for loop check : \(i)")
}
}
print("print check : 5")
}
// print check : 5
// for loop check : 0
// for loop check : 1
// for loop check : 2
// for loop check : 3
// for loop check : 4
func sayNestedNumber() {
defer {
print("print check : 1")
}
defer {
print("print check : 2")
}
defer {
print("print check : 3")
}
print("print check : 4")
}
// print check : 4
// print check : 3
// print check : 2
// print check : 1 defer가 호출되는 순서
func test() {
print("First print")
defer { print("!!defer!!")}
print("Second print")
}
test()
/* 결과
First print
Second print
!!defer!
*/
func test2() {
defer { print("1st defer - first") }
defer { print("2nd defer") }
defer { print("3rd defer - last") }
}
test2()
/* 결과
3rd defer - last
2nd defer
1st defer - first
*/
func test3() {
defer {
defer {
defer {
print("1st defer - inner")
}
print("2nd defer")
}
print("3rd defer - outer")
}
}
test3()
/* 결과
3rd defer - outer
2nd defer
1st defer - inner
*/ defer가 호출되지 않는 경우
func test4() {
print("1st print")
return
defer { print("defer~~") }
print("2nd print")
}
test4()
/* 결과
1st print
*/
func example() throws -> Int {
defer { print("1") }
enum ExampleError {
case error
}
throw ExampleError.error
defer { print("2") }
print("3")
}
example()
/*
Print
1
*/
func example() {
defer { print("1") }
guard false else { return }
defer { print("2") }
}
example()
/*
Print
1
*/
defer를 사용하는 경우
class MyResource {
private var data: Int = 0
private let lock = NSLock()
func updateData() {
lock.lock() // 데이터를 잠금
defer {
lock.unlock() // 함수가 끝나기 전에 반드시 데이터 잠금을 해제
}
// 데이터를 안전하게 업데이트
data += 1
print("Data updated to \(data)")
}
}
let resource = MyResource()
// 병렬로 데이터를 업데이트하는 스레드를 실행
DispatchQueue.global().async {
resource.updateData()
}
DispatchQueue.global().async {
resource.updateData()
}
func handleFileTransfer() {
defer {
print("임시파일을 제거합니다.")
}
do {
try simulateFileTransfer(connectionExists: true)
print("File transfer successful")
} catch let error as FileTransferError {
switch error {
case .fileNotFound:
print("File not found: \(error.localizedDescription)")
case .fileCorrupted:
print("File corrupted: \(error.localizedDescription)")
}
} catch {
print("An unexpected error occurred: \(error.localizedDescription)")
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: