Using #import ... as default instead of #import <...> - objective-c

When creating a new Objective-C class, XCode inserts this line into the .h file:
#import <Foundation/Foundation.h>
Instead, I want it to be the following automatic:
#import Foundation;
Where can I set it?

Xcode inserts the imports because this is how it is defined in the "new file templates". Instead of changing those built-in templates you should clone them and make your own, with adjusted import statement.
Look at this answer how to do that: https://stackoverflow.com/a/33743/396578

Related

different import statement in objective c

I was trying to debug a problem we were having in our React-Native on SDK
In our existing code, I see they have done
#import "CleverTap.h"
#import "CleverTapReactManager.h"
and docs says to do
#import <CleverTapSDK/CleverTap.h>
#import <CleverTapReact/CleverTapReactManager.h>
What is the difference between both the imports in swift? or they are the same?
#import "" first check the header in project folder then goes to system library, and the #import<> checks for system headers.
Read: https://swift007blog.wordpress.com/2017/01/13/include-vs-import/

Importing a Swift class of a framework in Objective-C (test) code

I'm trying to import a Swift class from my framework MyFramework in my unit test code:
In my Objective-C test file, I do:
#import MyFramework;
This line causes a compilation error:
Include of non-modular header inside framework module 'MyFramework': 'src/core-ios/MyFramework/Classes/MyFramework-Bridging-Header.h'
inside the "MyFramework-Swift.h" file. In that file, I can see a plain import from
#import "src/core-ios/MyFramework/Classes/MyFramework-Bridging-Header.h"
I'm not sure what to do. My framework "defines" modular headers, but it sound like the problem is that bridging header file which is included with its entire path rather than just doing #import <MyFramework/MyFramework-Bridging-Header.h>.
Any help appreciated!
While calling swift class in objective c
In Bridging-Header add #import "myClass.h". This is objective c class
where you are using swift class.
In myClass.m add #import < yourProjectName_swift.h >
In myClass.h or myClass.m add "#swiftcalss;"
first or second point may cause to your error.

Importing .mm file to another .mm file on xcode

I am getting error when trying to import .mm file to another one.
Its build like that :
first class FailedMenuLayer is .mm and have in its .h :
#import "gameScene.h"
second class gameScene is .mm, and have in its .h :
#import "FailedMenuLayer.h"
FailedMenuLayer *menuGO; //here i get the error: unknown type FailedMenuLayer .
why is that ?
It looks like an import cycle.
One way to fix it is to move the "gameScene.h" import to the .mm file. It's actually a good practice to keep the imports in the .h file limited only to what you actually need in the header and keep everything else in the .mm file.
If you need the import in the header try using #class instead of #import;
#class gameScene;
Don't forget to import gameScene.h in the .mm file also.
you are not importing ".mm" file, you are importing it's header.
Check your build phases> compile sources for your .mm file to be listed there. That might be your issue

What is #ifdef __OBJC__ doing and why libraries listed below?

I believe the #ifdef __OBJC__ directive is ensuring that I import the following class libraries for Objective-C only. What is the purpose of listing the class libraries after the ifdef statement? Doesn't this code example kinda defeat the purpose?
#ifdef __OBJC__
#import <foundation/foundation.h>
#import <uikit/uikit.h>
#import <coredata/coredata.h>
#endif
</coredata/coredata.h></uikit/uikit.h></foundation/foundation.h>
Objective-C is a superset of C (just like C++ is) and fairly often files from the different languages will be used in the same project and share headers, especially the prefix header. The #ifdef __OBJC__, like #ifdef __cplusplus, lets you include (or #import for Objective-C) headers for only the appropriate language.
The same header included in .c, .cpp, and .m files (with default compiler settings) would only have __OBJ__ defined for the .m files.
Basically in that code if you are using Objective C it will import those 3 libraries
#import <foundation/foundation.h>
#import <uikit/uikit.h>
#import <coredata/coredata.h>
The purpose of that if, is to not import them unless it is necessary.
They are listed after the #endif just as a reminder, so it makes the code easier to read. Otherwise you'd have to look up above to see what the #endif was ending.
The reason this is done is so that this code can still be compatible with regular C code that may want to use the functionality in that C file (at least that's what it looks like to me). By including those libraries only when OBJC is defined it ensures that the libraries are ONLY imported when you are compiling for objective c and not for standard C.

Objective-C #import loop

I have the following code:
#import <Foundation/Foundation.h>
#import "ServerRequest.h" // works even though this line is included
#import "ServerResponseRecord.h"
#protocol ServerRequestDelegate<NSObject>
-(void)request:(id)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(id)request gotError:(NSError*)error;
#end
It compiles and runs fine. However, if I replace the method declarations with:
-(void)request:(ServerRequest*)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(ServerRequest*)request gotError:(NSError*)error;
I get the unexpected syntax error "error: expected ')' before 'ServerRequest'". The only reason I can think this might be a problem is that ServerRequestDelegate.h and ServerRequest.h #import each other. However, I don't understand why the code works with the #import line with (id)request. I also don't understand why it's a syntax error.
Can someone provide a good explanation?
You've already hinted at the explanation: an #import cycle.
The first thing I'd do is remove the #include and add the following line above the #protocol definition:
#class ServerRequest;
This is a forward class declaration, and can help break the import loop. Check out this SO question for more details. Apple also has a brief explanation in this guide.
Basically, #import'ing a file causes the compiler to bring the entire text of that file into the file in question, and although #import is "smarter" than #include, it doesn't mean you're immune from import errors. The #class declaration is a way to tell the compiler that a class exists without importing the header. It's appropriate to use when you only need to know about the class name, but don't care about the methods it provides. Generally, you want to use #class in the .h file and #import in the .m file, where you're actually interacting with the class.
#import "loops" are not a problem. #import is the same as #include except that it tracks files and makes sure the preprocessor only reads them in the first time.
Usually when you get an error like that it is due to a problem in the included file. So the error is probably in ServerResponseRecord.h, thought it is probably being tripped by actually using the object declared by it. Without seeing the complete headers it is not possible to say exactly what is going on.