How to solve 'cannot find URL in scope' when accessing URL with XCode Playground ?
1. Purpose
In this post, I will demo how to solve the below exception when using xcode playground to test swift program that access a url.
The error in xcode is:
2. Environment
- Mac OS
- Xcode
- Swift
3. The solution
3.1 The code that caused the problem
When compiling, the xcode complains that:
3.2 The solution
Here is the solution, we should add some imports
to the header of the code:
3.2.1 About swift import
An import declaration lets you access symbols that are declared outside the current file. The basic form imports the entire module; it consists of the
import
keyword followed by a module name:Providing more detail limits which symbols are imported—you can specify a specific submodule or a specific declaration within a module or submodule.
But how can I know which module the URL class belongs to ? We can just search this in Google:
Then we should see the link that points to apple developer documents, e.g. https://developer.apple.com/documentation/foundation/url
3.2.2 About the needsIndefiniteExecution
We defined a variable needsIndefiniteExecution
:
By default, when all top-level code is executed, the execution is terminated. When working with asynchronous code, enable indefinite execution to allow execution to continue after the end of the playground’s top-level code is reached. This, in turn, gives threads and callbacks time to execute.
We set this flag because we have an asynchronous process that would wait for the URL to be accessed and process the response, just as follows:
In the above code, we passed a closure function to the dataTask function to process the URL response when the connection succeeded.
3.3 The total lines of code
Here is the whole code of our example:
Now it works!
4. Summary
In this post, I demonstrated how to solve the ‘cannot find class in scope’ error in xcode playground, be sure to check you have imported the classes you are using .