2016-11-18 12 views
2

bettwen의 관계 : 1 ... (40)는, 실제로는 약 25를 인쇄[놀이터] 내가 DispatchQueue 및 DispatchSemaphore.Like 다음의 예와 혼란 스러워요 DispatchQueue 및 DispatchSemaphore

let semaphore : DispatchSemaphore = DispatchSemaphore(value:1) 
for i in 1...40 { 
    DispatchQueue.global().async{ 
     semaphore.wait() 
     NSLog("......1-%d",i) 
     semaphore.signal() 
    } 
} 

내가 인쇄해야한다고 생각 다음과 같은 결과가 표시됩니다.

2016-11-18 19:05:38.786 MyPlayground[7436:495171] ......1-1 
2016-11-18 19:05:38.787 MyPlayground[7436:495175] ......1-2 
...... 
2016-11-18 19:05:38.797 MyPlayground[7436:495258] ......1-23 
2016-11-18 19:05:38.797 MyPlayground[7436:495244] ......1-24 

이유를 아는 사람은 누구입니까? 40 개의 반복이 완료되기 전에

감사

답변

2

당신이 비동기를 실행하기 때문에, 놀이터가 완료됩니다. 다음 두 줄을 코드의 처음 또는 끝에 추가하십시오.

import PlaygroundSupport 
PlaygroundPage.current.needsIndefiniteExecution = true