Macro constant for something like status bar height - ios

I know how to define constants and macros using C. Is there any way to define a constant in Objective-C like this:
#define STATUSBAR_HEIGHT [UIApplication sharedApplication].statusBarFrame.size.height

yes thats pretty much it. For example you want to open a url then -
#define OPEN_URL(urlString) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]]
Put this in a file like utils.h and import that header file for this macro to be available. But in a large code base it is irritating to be importing in each file, You could declare a global macro in the #ifdef __OBJC__ section of your AppName-Prefix.pch. Now you need not import in each file...

Related

in XCode objective-C game project, how to include a .swift file with constants delcared in it

I have a game in Objective-C and I have constant in it declared like this, in file GameplayConstants.h:
#ifndef GameplayConstants_h
#define GameplayConstants_h
static float REGULAR_TIME_PER_FRAME = 0.1f;
#endif /* GameplayConstants_h */
I am converting game to swift, so I want to convert one by one file to swift. In particular, I want this constant (REGULAR_TIME_PER_FRAME) converted to a swift file, to a constant variable in a swift file GameplayConstants.swift (rather than in the GameplayConstants.h file which it currently sits in now). What should I do? I set the defines module in configuration file to YES, and I included the file (with line include GameplayConstants.swift), but I have various errors. At the moment, error is:
"missing #end"
in HeroAnimationhelper.m file, which has code (partial code displayed):
#import "HeroAnimationHelper.h"
#import "HeroConstants.h"
#import "dealer-Swift.h"
#implementation HeroAnimationHelper
//#include "GameplayConstants.h"
#include "GameplayConstants.swift"
As you see, I'm replacing the original .h file with .swift file.
What else do I need to do to use swift constants in a objective-c class?

#define a global preprocessor directive in a separate file in Objective-C

I want to define a global preprocessor directive in my app. For example:
In MyProgram.h, I define:
#define USE_LOCALHOST
I import this file from beginning in appDelegate.m.
For the later use, in another file, I refer to this global preprocessor directive.
In MyWebService.h, I define:
#ifdef USE_LOCALHOST
static NSString *MY_SERVER = #"http://192.168.1.130:8888";
#else
static NSString *MY_SERVER = #"http://myserver.com";
#endif
However, the value of MY_SERVER is always #"http://myserver.com". How to make it work properly? Thanks.
Define it in .pch. And you will never forget to include .h-file where you have defined USE_LOCALHOST.
Or you can define it in build settings in Preprocessor Macros.
For example only for Debug.
#define works only in the file where it's defined in. But you can #import "MyProgram.h" in MyWebService.h and problem solved. Every time you need to access USE_LOCALHOST, just import the header file.

Xcode6 does not export NSLocalizedStrings() defined in header when exporting to xliff

Well title says pretty much all of it.
I am using Xcode6.1 (6A1052d).
I have a header file (LocalString.h) in which I have defined some common NSLocalizedStrings like this
#define LocalStringYESButtonTitle NSLocalizedStringWithDefaultValue(#"YES", nil, [NSBundle mainBundle], #"YES", #"General YES button label")
Now when I "Export For Localization..." in resulting .xliff file I don't have strings defined in header.
Any ideas?
Well, it appears that Xcode doesn't parse header files (.h) for NSLocalizedString macros.
I have ended up with putting all definitions into .m file and include it in my .h file. This is just in order not to change includes in other files.
In LocalString.h
#import "LocalString.m"
In LocalString.m
#define LocalStringYESButtonTitle NSLocalizedStringWithDefaultValue(#"YES", nil, [NSBundle mainBundle], #"YES", #"General YES button label")
Try using an extern NSString rather than a #define. There might be another error causing problems but XCode might purposely not mess with macros.
in your .h
extern NSString *LocalStringYESButtonTitle;
in your .m
NSString *LocalStringYESButtonTitle = NSLocalizedStringWithDefaultValue(#"YES", nil, etc...);
Most likely the "Export for Localization…" action has the same flaw as the old genstrings tool: it parses the source non-pre-processed and thus will ignore your own macros. It's likely looking for the official macros like NSLocalizedString and NSLocalizedStringWithDefaultValue. You can tell genstrings about your macros but it doesn't look like you can do the same with the integrated "Export for Localization…"
The best solution therefor is the one #KirkSpaziani proposed: instead of using macros, use extern variables (you need to make sure they are correctly initialized before you use them, of course).

Configure device specific global instance variables

I'm currently writing a universal app, and would like to set certain parameters, such as standard button size, globally, depending on device. Currently I am using:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
iVar = 88;
}
else {
iVar = 44;
}
in various places. However, I'd like to run this only once, and set the ivar somewhere globally. I know I could do this in the app delegate, once the app initialises, and declare the variable in the header, but I'm wondering if there is a more elegant solution that is standard practice.
(I am aware that I could use the native image size using xcassets, but I'd like more control than this).
Create a class Global.h and declare all global variables in it. Keep only the header file and import the Global.h file in YourProject-Prefix.pch like this:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Global.h"
#endif
In order to define your global variables, simply open the Global file and put the following line of code, i.e:
#define __metricsTableRowHeight 45
#define __metricsTableSectionHeight 45
#define __metricsTableSectionButtonHeight 57

'Undeclared Identifier' error with defined constants

My defined constants are giving me 'undeclared identifier' issues. I have them in a Constants.h file that I am including in my .pch file. I thought it might be something with my .pch file, however, if I delete it from there and try #import it in one of the classes where I need one of the constants, then I still get an 'undeclared identifier' error.
If I take each #define line and put them at the top of the .m class file directly, they work. So my syntax is correct.
So it's something with the .h file itself, but I have no idea what.
//
// Constants.h
// Sliding Drawer
#define kOffscreenX 320 // X coord when Sliding Drawer is offscreen
#define kVisibleX 40 // X coord when Sliding Drawer is visible
// WordlistButton
#define kNumScores 3
// Fonts
#define kMessageFontSize 14
#define kScoreFontSize 10
It's impossible to see the error only from this piece of code. Preprocessor tends to create very messy things, especially when there are circular imports involved.
You can try to delete the current compiled version of the header, note it's not in the derived data folder, it's in XCode's cache (see Project -> Build Setttings -> Precompiled Headers Cache Path).
However, if you have tried to import Constants.h directly and it didn't work, the problem may be somewhere else.
Are you sure there is only 1 file called Constants.h? Note you should use a prefix for your files (e.g. SAConstants.h if Smooth Almonds is your name) to avoid collision with Apple's headers or headers of the libraries you are using.
If you import the header directly, go to the .m file and tap on Product -> Generate Output -> Preprocessed File and find Constants.h import in it. Is it your header?
By the way, there is a nice article about avoiding this kind of things in precompiled headers http://qualitycoding.org/precompiled-headers/
I found this thread due to an other error upper case parameter in my define statement. I solved it for my issue with lower casing:
#define MSB(BTvalue) ((uint8_t) (BTvalue >> 8)) //threw this error
changing BTvalue to just value with lowercase parameter made me happy
#define MSB(value) ((uint8_t) (value >> 8))

Resources