No such module Error "NotificationCenter" WatchOS, Swift - swift

I'm getting an module error in two of my controllers who tries to import the NotificationCenter framework. I have added the framework in "linked frameworks and libraries" but I'm still getting the error.
I even tried cmd + shift + k, but it did not do any good for me.
I also tried to set the framework search path to $(SRCROOT), but still same error.
I have two controllers with the import.
Linked frameworks and libraries
Added notificationCenter
TimeController
import WatchKit
import Foundation
import NotificationCenter error: "No such module 'NotificationCenter'"
SwipeController
import WatchKit
import Foundation
import NotificationCenter error: "No such module 'NotificationCenter'"
Any help would be much appreciated!

I agree with #David's comment, There are three targets for a single watchOS project.
You need to try these steps for each Target:
Select each target and go to -> Build Phases
Click on the right arrow. and click on the +
Finally, search for NotificationCenter and add the framework.
You need to try these steps for each target.

Related

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

Not able to import main app module in xctest

When using XCTest I am not able to import the main module. Instead of like importing #testable import App it is import #testable import Pods_App, also not able to import the main app structs and data. How to resolve this issue?
Click on your test file, then press CMD + OPTION + 0. This will bring up the inspector panel on the right side of the screen. Look for the target membership section inside the panel and ensure that the your test module is checked.

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.

Use of undeclared type 'MFMailComposeViewControllerDelegate'

I am trying to send an update to an application built in Xcode 7, Swift 2.3. The application builds and runs fine with no issues, however, when I attempt to Archive I receive "Use of undeclared type 'MFMailComposeViewControllerDelegate'".
I figured it was just my old project giving me grief so I created a new project which had one class:
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {}
Again, it works when building and gives me the same error when archiving. I am currently using Xcode 8.0, any ideas??

Why my import MessageUI in AppDelegate is not visible within my UITableViewController?

Since Swift files are visible for each others, why my import within Appelegate is not visible for one of my controllers? I get an error there.
This is called Access level for Swift Modules. Default access level for Swift modules is internal, that is to that file itself.
Have a look here in Apple documentation Access level in Swift module
You need to import frameworks/modules in which ever class you are using.
Put import MessageUI at the top of the file where you declare your PBOUserViewController class (which I hope is contained in a separate .swift file than your AppDelegate).