Why is Foundation.h not imported in the standard Xcode templates? - iphone

I just noticed that when I create iPhone application using standard templates in Xcode (View Based Application, Window based application etc) the header files only import UIKit.h (and not Foundation.h). So if the Foundation.h file is not imported, how can one use Foundation classes like NSString, NSArray etc in the program ? Shouldn't it be throwing an error?

It is imported in the precompiled header file (extension .pch) in your project.
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
As to why UIKit.h appears to have two import lines per file since it also shows up above, though, I'm not too sure. It shouldn't matter anyway, as the second #import line won't cause UIKit to be included again if it's already included in the precompiled header file.

If you take a look at some of the individual UIKit headers, they import Foundation themselves. As BoltClock noted, it's also in the standard .pch
The reason that it can be put in all these places is mentioned in "The Objective-C Programming Language":
This directive is identical to #include, except that it makes sure that the same file is never included more than once. It’s therefore preferred and is used in place of #include in code examples throughout Objective-C–based documentation.
You want to make sure that every file that needs access to the symbols defined in Foundation has them. The reason it's repeated is that it helps with reading and understanding the code; it says "This code relies on the stuff that's in this header".

It looks like the template files are designed to work with or without precompiled headers, so they import the header files they use. If you create a subclass of NSObject the template file will import Foundation. If you create a subclass of a UIKit object the UIKit header will be imported.
OK, so why can you use Foundation classes like NSString when only UIKit is imported. Well nearly all of the UIKit headers import Foundation.h!
Hope this make sense.

Related

UIGestureRecognizer setState: in swift 3 [duplicate]

You can't write to self.state in the subclass unless you import UIGestureRecognizerSubclass.h as indicated here.
In a Swift environment, I'm confused how I'd go about importing this. I tried import UIGestureRecognizerSubclass.h, and without the .h, but I still can't write to self.state.
How would I accomplish this?
The Swift equivalent is simply:
import UIKit.UIGestureRecognizerSubclass
That imports the appropriate header.
You need to have or create a -Bridging-Header.h file to import objc headers such as the one you want. The import line looks like this:
#import <UIKit/UIGestureRecognizerSubclass.h>
If you don't already have a bridge header file in your app, the easiest way to get one is to add an objc class to your project, and xcode will ask if you want one, then creates the file and ties it into the settings for you. You can then delete the objc class.
Everything in that header file is automatically made available to your Swift code, no need to add any import lines in your swift files.

use swift file into Objective-C project

Can anyone tell me how can I use swift file into my objective-C project?
I have a swift file which inherits from UIViewController instead of NSObject. I am trying to use the swift file but I am not able to figure out how.
When I add my swift file into objective-C project, it usually asks to create a bridging header.
In that header file I am importing my swift file as -
#import "SlideController.swift"
and my bridging file name is objcToSwift-Bridging-Header.h
I read many answers which asks to import file
#import "MyProductModuleName-Swift.h"
into the .m file, but I can't find any such file in my project.
I also changed the defines module = YES as shown in another answer.
But still I am not able to access swift file.
What am I missing? Please tell me the right steps to figure it out.
After you created Bridging header.
Go to Build Setting =>Search for "Objective-C Bridging Header"
Just below you will find ""Objective-C Generated Interface Header Name" file
and import That file in your view controller.
ex.In my case: "Dauble-Swift.h"

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).

File not found when trying to import a header file into the bridging header?

I'm trying to import my speech recognition framework into my project. Sadly my project is in Swift and speech rec is in objc. I don't see a problem with this. I have created a bridging header file before importing the framework and compiled to make sure it has no errors. Then I dragged the framework into my project. Got a successful compile again. Now I type this in my header file:
#ifndef Bridging_Header_h
#define Bridging_Header_h
#import "SpeechKit/SpeechKit.h"
#endif
And then I get the error: "SpeechKit/SpeechKit.h" file not found
I have tried reimporting the framework. Deleting and making a new header file etc. Nothing seems to work. I don't get why it's not picking up the header files from the framework.
Does anyone have any ideas? All help would be greatly appreciated.
Thank you so much.
Try #import "SpeechKit.h" or #import <SpeechKit/SpeechKit.h> instead #import "SpeechKit/SpeechKit.h"
use #import <SpeechKit/SpeechKit.h> in bridging header and don't forget to place import SpeechKit in your swift file

AVFoundation/AVCaptureDevice.h not found

i want to use this header #import < AVFoundation/AVCaptureDevice.h > in theos tweak, but i get this error:
AVFoundation/AVCaptureDevice.h: not such file or directory
i also add the AVFoundation framework to the makefile.
if i do #import < AVFoundation/AVFoundation.h> work fine, but i need this header AVCaptureDevice.h
help!
EDIT: My apologies, I completely missed the lower part of your post for some reason.
Just import the whole AVFoundation framework. It will include the AVCaptureDevice header for you.
For Theos projects, you need to add the frameworks to your makefile.
Look for this line (if it doesn't exist, add it manually):
*YOUR_PROJECT_NAME*_FRAMEWORKS
There's where you add any public framework you want to use. Suppose your app is called "myVideo" and you want to add the AVFoundation framework, you would do this:
myVideo_FRAMEWORKS = AVFoundation
As an additional piece of info, if you wanted to add more frameworks you do this:
myVideo_FRAMEWORKS = AVFoundation UIKit