All Questions
Tagged with macros objective-c
308
questions
117
votes
10
answers
78k
views
How can I convert from degrees to radians?
I am trying to convert this Obj-C code to Swift code but I don't know what the equivalent of this code should be ?
#define DEGREES_TO_RADIANS(degrees)((M_PI * degrees)/180)
I googled and found this
...
77
votes
5
answers
108k
views
How to use a Objective-C #define from Swift
I am migrating a UIViewController class to train a bit with Swift. I am successfully using Objective-C code via the bridging header but I have the need of importing a constants file that contains #...
44
votes
2
answers
57k
views
What's the difference between #if and #ifdef Objective-C preprocessor macro?
How to define preprocessor macros in build settings, like IPAD_BUILD, and IPHONE_BUILD (and how to use them in my factory methods)?
I'm using these by heart now, would be cool to know what is going ...
36
votes
5
answers
54k
views
Implicit declaration of function - C99
I am currently using Xcode 4, and in my .pch file I have this macro:
#define localize(s) NSLocalizedString((s), nil).
When I try to use this macro in some .m file, I receive this warning: Implicit ...
31
votes
3
answers
31k
views
How to use Objective-C code with #define macros in Swift
I'm trying to use a third-party Objective-C library in a Swift project of mine. I have the library successfully imported into Xcode, and I've made a <Project>-Bridging-Header.h file that's ...
20
votes
4
answers
7k
views
@"" string type literals for NSNumber
I love the shorthand handling of string literals in Objective C with the @"string" notation. Is there any way to get similar behavior with NSNumbers? I deal with numbers more and it's so tedious ...
20
votes
8
answers
16k
views
How to make a macro that can take a string?
I'm trying to do something really trivial: A macro that takes an string and prints that to NSLog.
Like this:
#define PRINTTHIS(text) \
NSLog(@"text");
However, when I try to pass a string to ...
18
votes
1
answer
5k
views
Swift alternative for #pragma clang diagnostic
Problem
I recently encountered a warning in a third party utility (WEPopover) in this piece of code:
_effectivePopoverContentSize = _contentViewController.contentSizeForViewInPopover;
This was ...
16
votes
2
answers
1k
views
Meaning of Objective-C macros prefixed with an at (@) symbol
The ReactiveCocoa framework makes use of weakify and strongify macros, both of which are preceded by an '@' symbol.
Here's an example (From this file).
- (RACSignal *)rac_textSignal {
@...
15
votes
3
answers
17k
views
iOS Writing Macro detect 3.5 inch or 4 inch display [duplicate]
I am trying to write a macro to determine the device is 3.5 inch or 4 inch. Some thing similar below.
#define IOS_OLDER_THAN_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6....
15
votes
4
answers
20k
views
What does "macro" mean in Objective-C?
I am new to iOS development and I just want to know the meaning of macro in Objective-C?
I have found that "macro" is used with #define but still do not get its meaning.
http://www.saturngod.net/ios-...
14
votes
2
answers
7k
views
Accessing the value of a Preprocessor Macro definition
If I add a macro "FOO=bar" under GCC_PREPROCESSOR_DEFINITIONS (or Preprocessor Macros if you use XCode"), what would be the best way to access the value of "FOO"?
Currently, I use the clumsy:
#...
11
votes
1
answer
9k
views
Objective C - Defining macro to call a method?
I want to define a macro to call the following, Is this possible?
I also want it to accept format string.
- (void)logString:(NSString *)string withLogLogLevel:(LogLevel)logLevel
{
// Sav log to ...
10
votes
1
answer
4k
views
Where is the DEBUG macro defined?
When I run code such as the following:
- (void)viewDidLoad
{
#ifdef DEBUG
NSLog(@"debug");
#else
NSLog(@"here");
#endif
[super viewDidLoad];
}
I see "debug" printed in the log, but I ...
10
votes
1
answer
6k
views
How to suppress "macro redefined" warning in Objective-C
I need to redefine a macro in my project and have a compile warning. I've tried
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wall"
#define SomeMacroToRedefine MyMacro
#pragma ...
10
votes
1
answer
387
views
ELEVENWORDINLINE when to use it?
I was always wondering what can I do with things like that:
ONEWORDINLINE(w1)
TWOWORDINLINE(w1, w2)
THREEWORDINLINE(w1, w2, w3)
up to
TENWORDINLINE(w1, w2, w3, w4, w5, w6, w7, w8, w9, w10)
...
9
votes
5
answers
9k
views
How can I write a 'clamp' / 'clip' / 'bound' macro for returning a value in a given range?
I often find myself writing something like
int computedValue = ...;
return MAX(0, MIN(5, computedValue));
I would like to be able to write this as a single one-line macro. It must be free of side ...
9
votes
1
answer
5k
views
Objective C MAX macro bug
I'm seeing strange behavior with the MAX macro in Objective C. Specifically, I have this code in my main function:
NSArray* array = [NSArray array];
NSLog(@"[array count] - 1 = %d", [array count] - ...
9
votes
2
answers
1k
views
How to require ARC in a class?
I have an app with both ARC code and non-ARC code. The compiler will catch when I try to compile non-ARC code as ARC. How do I cause a compile time error/notice when my ARC code is erroneously ...
9
votes
1
answer
3k
views
Preprocessor macro to target Interface Builder not available to OS X?
I have an IB_DESIGNABLE custom NSView subclass. When drawing, I want my view to perform a method only when it's being run in Interface Builder. I looked this up, and Apple's documentation states:
...
8
votes
2
answers
8k
views
How to define array in Macros in Objective-c?
Is there any possibility to define arrays in macro in objective-c.
8
votes
4
answers
8k
views
iOS create macro
I have a piece of code I'm using fairly often and would like to make a macro out of it. I'm not exactly sure how to do that though. Here's the code I want to use
UIImage *titleImage = [UIImage ...
7
votes
2
answers
6k
views
Objective-C Macro Redefinition
I am currently using three servers (deploy, live_testing, and local).
I am using macros to define a serie of domain locations:
#define __LIVE_TESTING // Here I chose what domain to use
#ifdef ...
7
votes
1
answer
5k
views
XCode Preprocessor Macro for Configuration?
I want my XCode Objective-C project to be able to detect which configuration it is being built with. How can I achieve this?
7
votes
1
answer
6k
views
Is there a way to check how a macro "expands" in C or Objective-C
A macro is a preprocessor right. Sometimes we set things right sometimes we don't.
Wouldn't it be nice to once in a while expand the macro and see how it works?
7
votes
3
answers
2k
views
Unused parameter warning
I am getting "unused parameter 'testString'" warning from following code. But I am using testString to log. So how come it is unused ?
- (void)getString:(NSString *)testString {
ICELogInfo(...
7
votes
3
answers
6k
views
Conditional macro expansion
Heads up: This is a weird question.
I've got some really useful macros that I like to use to simplify some logging. For example I can do Log(@"My message with arguments: %@, %@, %@", @"arg1", @"arg2"...
7
votes
3
answers
365
views
Using C macros to create C-style code which maps to Objective-C message calls?
I'm sure I'll get 20 people saying "why would you want to do that anyways"... but, I'm going to ask my question none-the-less because it's somewhat academic in nature.
I'd like to use C macros to ...
7
votes
1
answer
429
views
How can I use commercial at sign in Objective-C macro?
How can I use commercial at sign in Objective-C macro?
I know it is not recommended, but seems it is possible somehow? Since libextobjc provides @weakify, @strongify, etc...
7
votes
1
answer
657
views
How to use prepeocessor macros in storyboard elements?
I want to use pre-defined macros in StoryBoard elements
For example:
I have a 10 textfield in my app and if I want to change the size of all textfield through preprocessor macros then it will be ...
7
votes
2
answers
3k
views
How to expand a MACRO into NSString without using any string concatenation at runtime?
I defined a macro MYMACRO. Note: the value is not a valid NSString.
#define MYMACRO is
The macro is used inside the declaration of a NSString
@"This MYMACRO fun"
However, the preprocessor does not ...
6
votes
4
answers
9k
views
#define: why uppercase?
When defining a macro (e.g. #define BALL). Why do people use uppercase letters? I can write something like #define ball. It is not in uppercase, but it works.
6
votes
3
answers
3k
views
How can I replace a #define macro I used to have in Objective-C in SWIFT?
I've been using this macro in Objective-C:
#define RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
I am trying to figure out how I can get the closest ...
6
votes
3
answers
5k
views
How to detect C++ compiler with macro in Xcode?
I'm mixing Objective-C (*.m) and Objective-C++ (*.mm) source files in an iOS project.
When I import a C++ header file in a *.m file how can I exclude the C++-specific code in the header file? I want ...
6
votes
2
answers
6k
views
Objective-C: Where to define macros to be available everywhere?
I have an iOS application which uses in many view controllers, the same strings, numbers, etc. So I think it would be much better to define these constants in one file and use it in all the view ...
6
votes
3
answers
2k
views
Objective-C Safe Casting Macro
I've written a macro in Objective-C to perform a safe cast. Here's what it looks like so far:
#define SAFE_CAST(OBJECT, TYPE) ([OBJECT isKindOfClass:[TYPE class]] ? (TYPE *) OBJECT: nil)
This works ...
6
votes
2
answers
2k
views
Global Variables for Class Methods
Background
In Cocoa, Apple frequently makes use of the following paradigm:
[NSApplication sharedApplication]
[NSNotificationCenter defaultNotificationCenter]
[NSGraphicsContext currentContext]
[...
6
votes
4
answers
2k
views
Objective-C: Function-Like Macro Vs. Method
In Objective-C, when do you recommend using function-like macros over class or instance methods?
6
votes
2
answers
3k
views
NSImageView rounded corners + stroke
I have subclasses NSImageView and i want to draw a border around with rounded corners. It works but i need to clip off the image corners as well.
Please see my screenshot:
I have created this code ...
6
votes
1
answer
6k
views
XCode 5.1 preprocessor macro not working
I cannot get this macro to compile the correct code.
Here is the code:
Here are the build settings (I'm doing a Release build):
Note that the GCC documentation says -Dname will define as 1, so I ...
6
votes
3
answers
1k
views
Asserting object class in Objective C
I often find myself asserting that an object "isKindOfClass" of some class in Objective-C. I do it like this:
NSAssert([obj isKindOfClass:[AClass class]], @"%@ should be kind of class %@", obj, [[...
6
votes
2
answers
1k
views
Xcode 8 and Preprocessor Macros
Since the latest XCode update 8.1 (Build 8B62) I'm having strange problems with syntax highlighting/code completion in connection with preprocessor macros.
They worked just fine with the XCode 8 GM ...
5
votes
7
answers
20k
views
Is Macro Better Than UIColor for Setting RGB Color?
I have this macro in my header file:
#define UIColorFromRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & ...
5
votes
5
answers
9k
views
ABS(A) and abs(int)
I am baffled in the difference of this two in xcode, ABS(A) and abs(int). Cant seem to find ay explanation online as well. Which should I use? I am actually working on an accelerometer. Using ABS(A) ...
5
votes
2
answers
3k
views
Macro for NSLocalizedString
All my calls of NSLocalizedString have nil as second param, because i don't use any comments. But i hate to repeat myself. Now i'm asking myself if it's ok to define a macro like LSSTRING(str) that ...
5
votes
2
answers
10k
views
Objective-C: "format string is not a string literal (potentially insecure)" warning with macro
I'm using a macro to simplify returning localised strings, like so:
#define GetLocalStr(key, ...) \
[NSString stringWithFormat:[[NSBundle mainBundle] localizedStringForKey:key value:@"" table:nil]...
5
votes
1
answer
3k
views
Macro condition for iOS and OSX code
I need to support iOS and OSX in my shared library, whats the best Macro / Practise for conditioning iOS and OSX code
the bellow code doesn't work for some reason :/
- (NSString *)hostName {
#...
5
votes
2
answers
3k
views
How to make NSString macro?
How to make a macro that represents a constant NSString value? I'm getting
"Multi-character character constant" and "Character constant too long for its type" warnings when defining in Xcode 4:
#...
5
votes
0
answers
866
views
Disable NSLog for CocoaPods
I am using several CocoaPods in my project. Some of them are outputting strings to the console via NSLog().
How can I disable all logging from CocoaPods?
Also, is there a way to do so while still ...
4
votes
2
answers
3k
views
How to define a macro globally in Objective-C?
I want to define a macro globally. I want to override NSLocalizedString() throughout my whole project. Is that possible and how do I do it?