I'm getting this really weird error when trying to import UIKit in my swift file.
My Code is simply:
import UIKit
class Test: NSObject {
}
The error I get at 'import UIKit' is:
Unknown type name 'import'
Expected ';' after top level declarator
I have added UIKit to my Frameworks folder, the class doesn't contain any code (so therefore there shouldn't be anything wrong with it) and I tried to restart both xCode and my Mac, but the error is still there.
I appreciate any help.
Thanks.
EDIT:
Solved:
I tried to import 'Test.swift' in AppDelegate.
This problem usually happens when you try to import ".swift" file in your Objective-C code, like this: #import "HomeViewController.swift". This is wrong and you should import special, automatically generated Swift header, instead:
#import "ProductModuleName-Swift.h"
where ProductModuleName is the name of your module (or project) containing the Swift code.
Found good Trouble shooting guide - there is great Troubleshooting Tips and Reminders section!
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_87
The import stuff in swift is case sensitive. Example :
import uikit
will not work. You'll have to type it as
import UIKit
In my case, it was because somehow the selected type in the file inspector was objective-c source instead of Default - Swift Source even though i was using a .swift file.
Changing it to Default - Swift Source solved the issue for me.
Related
Anyone faced problems in {Module_name}-Swift.h file for Swift 4 projects? I've noticed -Swift.h autogenerated file not working well with Swift 4 syntax unlike Swift 3.2!.
For example, -Swift.h file doesn't contain all variables and methods which implemented in the custom Swift classes which inherited from NSObject class!
I've used #objc and #classkeywords but no way.
I don't get any errors! the problem is if I've created a class like this:
import Foundation
class Utils: NSObject {
let abc: String?
func xyz() {
print("")
}
}
and navigate to {Module_name}-Swift.h I see something like that:
SWIFT_CLASS("_TtC3{Module_name}5Utils")
#interface Utils : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
#end
Problem
Both let abc: String? and func xyz() have been never included in {Modue_name}-Swift.hfile!
I think in Swift 4 you have to mark a lot more things #objc (nothing implicit anymore) but other than that it should just be in there.
You can all check it to confirm class name in .h file like:
#class filename;
The generated file {Module}-Swift.h does not contain your variables and methods, the file is generated to give you access to the Module namespace.
The actual interface for the generated module lives in Module.swiftmodule/arm64.swiftmodule (depending on built architecture).
More information on its contents:
https://bugs.swift.org/browse/SR-2502
https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160111/000827.html
https://stackoverflow.com/a/24396175/1755720
however... the format is not documented anywhere and is subject to change. A good starting point would be to look in include/swift/Serialization/ModuleFormat.h
As to why it's not working - Swift 4 has a migration process, please ensure you have followed it: https://swift.org/migration-guide-swift4/
Xcode will pick up most things ... but it won't get everything!
And why do you need header files for Swift classes? You just can mark swift class as #objc and you will be able to reach all its properties.
I am attempting to use the autogenerated Swift bridging header in an Objective-C class, but when I try to include it, I get many errors in the bridging header. Since the header is automatically generated, its not at all clear what I might have wrong.
In an Objective-C class, if I declare the following line I will get build errors - without this line, no errors and it builds fine:
#import "Oilist-Swift.h"
EDIT: I just moved the #import "Oilist-Swift.h" statement to after all the other headers have been imported, and now I only get errors relating to MFMailComposeViewControllerDelegate and possibly one error relating to PopupStoreControllerDelegate. Here are all the errors now:
Angle brackets contain both a protocol ('PopupStoreControllerDelegate') and a type ('MFMailComposeViewControllerDelegate')
Unknown class name 'MFMailComposeViewControllerDelegate'; did you mean 'MFMailComposeViewController'?
Replace 'MFMailComposeViewControllerDelegate' with 'MFMailComposeViewController'
- (void)mailComposeController:(MFMailComposeViewController * _Nonnull)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError * _Nullable)error;
Expected a type (MFMailComposeResult)
I've looked for circular references and can't find any so far.
Any insights would be greatly appreciated!
Seems really close now - it seems its just not happy with MFMail for some unknown reason.
If I import MessageUI.h it works now. (Despite MessageUI not being used at all in the class importing the Swift header).
I guess I have to import MessageUI because it is referenced in the bridging header and doesn't include a definition for the MessageUI stuff...
Solution:
#import <MessageUI/MessageUI.h>
#import "Oilist-Swift.h"
I'm working on a project that contains Swift and Objective_C code, it works without any problem until i was trying to import a ".h" class in my "Bridging-Header" file:
Expected a type : in the class that i was trying to import it
Failed to import bridging header '/Users/sysadmin/Desktop/Application /Classes/UI/Application-Bridging-Header.h'
however when i have imported an other class in Bridging Header that works without problem!
There is 3 ways to import objective c file in Swift project.
#import "FileName.h"
#import <FolderName/FileName.h>
#import PackageName; like #import Alamofire;
Try importing your class like
#import "classname.h" if this fails try
#import `<classname/classname.h>`
Remember to clean and then run after you make a change.
Again if it fails check whether you have added the class to your target
I am getting this error while running this project.
failed to import bridging header '/Users/OdysseyApps/Desktop/ashraf/onedrive-sample-sync-ios-master/OneAPI Sync/OneAPI Sync-Bridging-Header.h'
I think the problem is the space in project name OneAPI Sync.
please give me some suggestion.
Probably You have imported some class in bridge file.
That is not a part of your bundle.(or didn't accidentally imported .m file)
Try to find that class and remove that class import statement.
I want to use my protocol method of swift class into objective-c class for it I have to import 'ProductModuleName-Swift.h' and declare its protocol in interface but it shows error: 'ProductModuleName-Swift.h' file not found.
But when I import it into test.m file then its fine but when I import it into test.h then it shows the error.
example:
test.h
#import "myProject-Swift.h"
#interface test : UIViewController<mySwiftDelegate>
or There is any way to use protocol method of swift in obj-c class
thanks in advance.
Go to Targets -> Build Settings -> Swift Compiler - Code Generation and make sure the "Objective-C Bridging Header" points to the correct place and the "Objective-C Generated Interface Header Name" is the correct file. My header name for the unit tests defaulted to Module-NameTest-swift.h I needed to remove "Test" from the title.