Playground Xcode never stops running, any ideas? - swift

I have been learning Swift and had a recurring problem throughout. The playground, when run, doesn't finish running the code, even the default MyPlayground file. I get no output whatsoever.
I have searched online and others have the same problem as me but no answer. This happens for the default and built up files I have created previously.
I spoke to Apple on 3 separate occasions and got nothing and referred to the Developer forums and they haven't got an answer either.
Any ideas guys?
For example,
//: Playground - noun: a place where people can play
import Cocoa
var str = "Hello, playground"
print(str)
This is the default and when run, I don't get the output of str or anything in the Utilities view, it just says running MyPlayground at the top.
Thanks

What are you building for? iOS, macOS, or tvOS?
The default file for macOS is as you say:
import Cocoa
var str = "Hello, playground"
Which runs perfectly, with no errors.
But when I run your code built for iOS, Xcode throws an error:
Swift Compiler Warning: No such module `Cocoa`.
Either way, you cannot import Cocoa in playgrounds built for iOS, so don't import Cocoa, import UIKit instead. Besides, import UIKit is the default file when building for iOS. So I suspect you're running the default macOS file in an iOS build of playgrounds.
There is another question here which addresses the issue of importing Cocoa in Playgrounds.
Since you're having what looks like a null pointer exception, based on your comment, likely from the project trying to load a non-existent object, here are some troubleshooting steps:
Erase import Cocoa.
Type in import (Notice the space at the end.)
Type in C
If C doesn't come up with an autocomplete list with Cocoa in it, then it's not part of the build.
And this would explain the null pointer exception (EXC_BAD_ACCESS at 0x0.)
Next, in the same playground:
Erase the import Cocoa line
Type in import (space at the end.)
Type in UI and wait for an autocomplete list
If UI has the autocomplete option for UIKit, then Cocoa isn't part of the playground.
Which is why there is a null pointer error.

Related

SwiftUI modifier .resizable() not available for Image

I found a lot of tutorials for SwiftUI where the modifier .resizable() is used on an Image.
It doesn't seem to be available anymore on Xcode 13.3.1 with Swift 5.
SwiftUI is imported. If you type the whole modifier myself is says
Local module defines have a preference over system. Taking into account context menu proposals it is your case.
The fix is to use module explicitly, like
SwiftUI.Image("myImage").resizable()
It definitely is...
Have you definitely imported SwiftUI in that file?
If you type the whole thing out does it build or does it cause an error?
From the auto completion drop down it looks like Xcode thinks you’re referring to an imageURL perhaps there is a change that Xcode hasn’t caught up with yet.
It looks like the Image you are getting is from some other module (not SwiftUI).
If you are 100% sure you don't have another import -- then I recommend exiting Xcode and deleting derived data.
rm -rf ~/Library/Developer/Xcode/DerivedData/
You are using a different Image, it's not from SwiftUI. See the color of Image is lite green(ies). You might be using an Image extension or something. Make sure while typing Image..., you select Image from SwiftUI.

What is the differnce between #main and #uiapplicationmain

I need help understanding when to use #main vs #uiapplicationmain.
From what I know, #uiapplicationmain is an easier way of calling the uiapplicationmain(:::) where the class that is made after #uiapplicationmain is used as the App Delegate. I also know that it also creates an UIApplication.
I also know that app delegate becomes the entry point for the project.
From what I have read I was told that #main is also a entry point but it requires an main function.
What I want to know is what do they mean by the main entry point. Like what is Xcode doing to make it the "entry" point. And how does the whole #main thing work, as in how does it differ from #uiapplicationmain and what is it doing to the way Xcode runs the code.
All code has an entry point: the place where whoever calls that code actually calls. How does the whole program, comprising many Swift files, actually get started? We need an entry point for the whole program, which the runtime will call to launch us.
In Swift, this is the main.swift file. Its job is to call UIApplicationMain, which creates some instances including the app and the app delegate and gets the event loop running (and stays running for the rest of the time the app runs). A minimal main.swift file would have to look like this:
import UIKit
UIApplicationMain(
CommandLine.argc, CommandLine.unsafeArgv, nil,
NSStringFromClass(AppDelegate.self)
)
However, no one ever uses a main.swift file! It's boilerplate, so why bother? Instead, you say #main, and a main.swift file is generated for you behind the scenes. In particular, you put the attribute #main on your AppDelegate class, so the main.swift generator knows which class to instantiate as your application delegate.
Two more things to know:
Before Swift 5.3, #main was called #UIApplicationMain instead. From that point of view, they are identical, two names for the same thing.
New in Swift 5.3 and Xcode 12, you can designate one of your own types as #main and give it a static main function, where you do whatever you would have done in the main.swift file. That is something #UIApplicationMain cannot do:
#main
struct MyMain {
static func main() -> Void {
UIApplicationMain(
CommandLine.argc, CommandLine.unsafeArgv, nil, NSStringFromClass(AppDelegate.self)
)
}
}
#main is part of the new SwiftUI lifecycle, introduced in iOS 14.
#uiapplicationmain is the older version that's part of UISceneDelegate and UIApplicationDelegate, but is still in use. Both represent your app's lifecycle.

How to inspect swift function implementations

How can I look behind the scenes? For example I would like to know more about the isEqual function ... Command+Click -> Jump to Definition does not show how this function work.
let pi : Double = 3.14159265359
pi.isEqual(3.14159265359)
If the code is in a compiled framework, like Foundation or UIKit for example, Xcode will take you to the declaration of the function, where you'll be able to see the function signature and maybe some comments about it, but not the full code becase since the code has already been compiled, Xcode doesn't have access to it.
Swift is open source, you can go to the repository and see how it's implemented. I think this includes most of Foundation, but it won't include Apple's frameworks like UIKit, SwiftUI, etc.

Xcode Ignoring import

I have just installed Xcode 11 and when I try to create new fresh project with the SwiftUI check mark selected it returns an error.
Not able to build and run successfully.
File 'ContentView.swift' is part of module 'SwiftUI'; ignoring import
ContentView.swift
Use of undeclared type 'View'
SceneDelegate.swift
Use of unresolved identifier 'UIHostingController'
I have tried removing all derived data and also set command-line tools to 11
Your project is named SwiftUI - please try using a different name.
Detailed Answer
Each project you create has a module with the same name as the project. So there are two SwifUI modules here:
The actual SwiftUI
The project itself
Xcode always takes the nearest definition as the default. So your SwiftUI is closer than the system's SwiftUI. But you are in the project's module already! So Xcode ignores the import.
A very common mistake is to name the project same as one of the using frameworks! (e.g. CoreData, SwiftUI, SceneKit, Metal)
Solution
As Matteo mentioned in his answer, Don't name your project same with another module. Change it to anything else.
Note that It could appear as an error too. For example, if you name your project CoreData and using SwiftUI, the error appears as Circular dependency error:
Circular dependency between modules 'CoreData' and 'SwiftUI'
Because Xcode gets confused about modules and can not detect what the real issue is.
How can we access our module's classes instead of the system's module?
Imagine you have a class named Section in a custom framework called MyProject and you imported it alongside the SwiftUI.
import SwiftUI
import MyProject
Section // <- This could be either SwiftUI's section or MyProject's Section
To make it clear for the compiler (and anyone else), you should call the module before the class name:
SwiftUI.Section // <- This returns the SwiftUI's Section
MyProject.Section // <- This returns the MyProject's Section
Try with different project name. With SwiftUI, it will always show compilation error. Just change the name and enjoy coding with SwiftUI

Why am I able to use MessageUI without a framework reference?

In my Swift 2 project, targeting iOS 9.2 and above, in Xcode 8.2.1, I have code that shows the mail-compose screen like so:
if MFMailComposeViewController.canSendMail() {
let composeMailVC = MFMailComposeViewController()
composeMailVC.mailComposeDelegate = self
composeMailVC.setSubject("Test")
// etc
}
Originally I had a reference to the MessageUI.framework in my project properties, but after removing the framework reference and cleaning the project, it still builds fine and when I run the code on my device the mail compose window still appears and seems fully functional.
I cannot find any explicit references to MessageUI.framework in the raw text of my .xcodeproj file, nor is there anything in my Objective-C bridging header.
I know that Swift does make some implicit framework references, but I couldn't find anything that suggests MessageUI.framework is one of them.
Curiously when I jump to the definition of MFMailComposeViewController XCode shows it in the MessageUI module.
The compiler automatically added the frame work in given its previous direction - IE. Import.