XCode 13 - Task not found in scope?

Hello, I'm trying to perform simple async tasks wrapped in a Task struct but the compiler always says "error: cannot find 'Task' in scope" using XCode 13 with macOS 12's last beta

Even the simplier playground like this one, fails:

import Foundation

func foo() async -> String {
    await withUnsafeContinuation { continuation in
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            continuation.resume(returning: "Hello")
        }
    }
}

func bar() {
    Task {
        let text = await foo()
        print(text)
    }
}

Results in:

error: MyPlayground.playground:13:5: error: cannot find 'Task' in scope

    Task {

    ^~~~

So... is this normal or am I missing something basic (and stupid)?

Thanks

  • Thanks for sharing your experience. I could have reproduced the issue on Big Sur 11.6 (iOS Blank playground). (With additional error: cannot find 'withUnsafeContinuation' in scope.) I do not think this is normal and you should better send a bug report to Apple.

  • Thanks to Claude31, I could have explored a little more. Instead of import UIKit, import _Concurrency (in addition to import Foundation) will make it. What is odd is we do not need import _Concurrency in app projects. (You should not use import _Concurrency in actual projects, it may be treated as using a private framework.) And one more odd thing is import _Concurrency can be found when I see the generated header for Foundation by Cmd-clicking the line import Foundation.

Add a Comment

Replies

Do you test this code in playground ?

I tested in playGround with Xcode 13 and it works without problem.

You need to add:

import UIKit

Yes, I've tested in playground and macOS app...

I can confirm that using an iOS playground and importing UIKit, it works. But, why? The symbol is part of the Swift Standard Library, why I need to import UIKit? In this way it's impossible to use inside mac apps.

  • In this way it's impossible to use inside mac apps. Xcode 13 does not include macOS 12 SDK. You may need to wait the next Xcode which contains macOS 12 SDK.

Add a Comment

I have the same issue in Swift Package

try this

import _Concurrency