Xcode Ignoring import - swift

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

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.

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.

Playground Xcode never stops running, any ideas?

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.

No such module purelayout in swift with manual installation?

I have been at this for over an hour. I am desperate.
I am trying to implement this here https://github.com/enisgayretli/EGFloatingTextField and this is my previous question Best way to achieve UILabel animation effect in Swift?
For the text field to work I need to import Purelayout. I'm working in swift and this is where things don't work- I've tried all the methods on the purelayout github but can't for the life of me import purelayout.
No matter what I do I get no such module purelayout. I am trying to do the manual installation and I have 1. Dragged and copied the purelayout source files into my project 2. Written the import Purelayout.h line in my bridging header
Where am I going wrong here? Am I configuring my header wrong? When I drag the files in it wants to configure a new Bridger and creates a blank file.. Could this be the problem?
My project will not compile. I can't understand this.

How to properly import subclass frameworks into projects

I have a question regarding importing subclasses into projects. We often find useful subclasses on GitHub that we can use. In my case it's just a Swift Xcode project which is a Swift port of an Objective-C subclass.
Contains what you see below.
There's no storyboard or anything so I'm wondering why the product folder at the bottom has a SiriWaveformView.framework suitcase icon but there is no framework with this name in the Finder.
Is this technically a framework/API? Do I simply copy SiriWaveformView.swift and SiriWaveformView.h into my own project? How do I connect an interface object to this class?