I have a Global.h that looks like
#define NUMBERX 21
In AppDelegate.h I include the Global.h file. In the AppDelegate.m I include the AppDelegate.h file. But in the AppDelegate.m I can't access the NUMBERX variable.
ERROR: Use of undeclared indentifier 'NUMBERX'.
If I define NUMBERX in AppDelegate.h than it works, but I want include only the header file (Global.h) in all other header files where I want to use the NUMBERX variable.
How can I solve that?
If you're using objective-c standard #import to include your header file, try replacing it with a "c" #include.
This should be fine, assuming you're not #undefing it before you're using it. Are you using the symbol before you include AppDelegate.h in the AppDelegate.m file? Are you using include guards that might prohibit it's inclusion?
You have to include your Global.h file in AppDelegate.m file.
Could you not use int const NUMBERX then you will get code completion and compiler checking.
Apple has some pretty good guidelines on defining constants and naming them here
Apple Coding Guidelines - Constants
Related
I have a project contains objective C code and I want to implement new features using Swift so I created Bridging header and added it to the build paths, the problem now is when I add #import "xx" to the Bridging header #import is not recognized and when I add it manually by copying and pasting like #import "MBProgressHUD.h", then i try to view the source code I get "?" although when I import the same library in any other .h or .m file it works.
Note: I know that "MBProgressHUD" works with swift, it was just for reference.
Appreciating any support and thanks in advance
I think you should do like this
1、drag the MBProgressHUD into your project
2、you can create an OC class first
, which will remind if you create yourProject-Bridging-Header.h, choose yes
3、import #import "MBProgressHUD.h" in the -Bridging-Header.h,And then compile the project
4、use it
I have an iOS project-ProjectX (not created by me) which is able to access declaration from a .h file without using #include "someHeader.h".
In ProjectX, I could just create an empty File and refer to a declaration in "someHeader.h", which I find perplexing. Example:
#import <Foundation/Foundation.h>
#implementation Empty:NSObject
SOME_TYPE_FROM_SOME_HEADER_H x;
#end
and the compiler automatically knows where the definition is?!
I have since tried to create an identical project, duplicating all the project settings, adding static libraries/files, etc. but to no avail.
Any ideas on what I might have missed out or what do I need to configure in the project to achieve this?
As mentioned by Rishab, I was missing a precompiled header (.pch) file. In the project, a pch imported a static library which contained the header file. Therefore, I was able to call the definitions directly.
I have an old .a library called BULib with a macro NEW_API defined in the GCC_PREPROCESSOR_DEFINITIONS of the library target. This macro is used inside the lib to expose some methods in the headers or not with conditional compiling. For example:
#ifdef NEW_API
+ (nullable NSString *)user;
#endif
The problem appears when using this library inside a project. When I want to use
[BULib user];
I get a compile time error
No known class method for selector 'getUser'
The problem is solved if I add NEW_API in the GCC_PREPROCESSOR_DEFINITIONS of the project, then I can use the symbol and it works as expected.
Maybe I'm misunderstanding the use of the macros between libraries and consumer project but I don't want that behavior.
Why do I have to set the macro in the consumer project if the lib has been compiled correctly with the macro set? If this is not the way to achieve this, what is it?
Thank you so much.
There are several possible solutions:
Add NEW_API in GCC_PREPROCESSOR_DEFINITIONS as you did
Delete #ifdef NEW_API and #end
Use #define NEW_API before importing libs header
Add + (nullable NSString *)user; and other missing methods in BULib's category
Personally I prefer 3rd option.
It will be something like that:
#define NEW_API
#import <bulib.h>
I'm currently trying to add LARSAdController to my iOS project with no success.
As soon as i import the files via #import "LARSAdController.h" in my AppDelegate.h the build process fails and on every occurance of (Class)class in LARSAdController.h i get the cryptic error "Expected identifier". BTW I'm using cocoapods.
Example:
- (void)registerAdClass:(Class)class;
which seems fine to me...
If i create a blank project and import the files they compile, so the problem must be in some relation to my code. Anyone got an idea what may cause this?
Thanks for any help in advance!
class is a reserved word in C++, so I would imagine that some of your project uses Objective-C++.
To solve this, use #import LARSAdController.h in Objective-C implementation files only, and remove its use from header files. You can use #class to forward-declare any occurrences of whatever classes are defined in LARSAdController.h in header files (this is best-practise anyway).
If you need to use LARSAdController from an Objective-C++ class then this is more complicated and you will need to use an Objective-C proxy object or modify their header files (which isn't ideal).
I am thinking about that it would be a great idea for examining that the class type. In example I would like to do the following in my application prefix.pch file.
#if isViewController
#import "DeviceCompatibility.h"
#import "UIViewController+Utilities.h"
#endif
How could I do this.
Thanks for the ideas and your time
This won't work. The point of a pch file is that it is a "Pre Compiled Header" file. It's compiled once and then that is used in every other file.
What you are asking for would require that it not be pre-compiled since it would need to be evaluated for each file. What you want is what regular, non-pch files are for.