Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check specific macro is there or not with identifier stored inside NSString

I have macros named

CONSTANT_1
CONSTANT_2
CONSTANT_3
CONSTANT_4 etc..

I have an NSString variable sending from another class consist of macro name only, how can I access the contents of macro variable matching with this NSString.

My NSString variable is defined as:

NSString *str=@"CONSTANT_3"
like image 568
Saranjith Avatar asked Jan 03 '17 11:01

Saranjith


People also ask

How to find the name of a macro in a button?

You can always right-click the button, left click on Assign Macro, and see the name of the macro that way. I assume you mean it is a Forms button, which makes sense because it's a macro as you said, not a procedure written into a command button click. 64, "Information about macro being run..."

How to check if a macro is defined or not?

Ideally, I would like to be able to both check that a macro is defined, and check that it equals a certain value, in one line. If it is not defined, the preprocessor throws an error (or warning). It's like a combination of #ifdef and #if, and if it doesn't exist, uses #error.

How to view macros content without running them?

The most secure way to see macros content without running them is to open Visual Basic Editor from you office document which will contain the content of the macros. To open Visual Basic Editor (Office 2010, probably 2013 and 2016): In the right section, select "Developer", it will add a tab "Developer" on the Ribbon.

How to check if the Excel file has macros?

is an unreliable way to check if the excel file has macros. To test it, create a workbook, say Book1.xlsm and insert a module but don't type any code. Close the file and in a new Excel file run this code from a module.


2 Answers

1 Here is one solution to do it, but it needs you to know all macros:

#define CONSTANT_1 1
#define CONSTANT_2 2
#define CONSTANT_3 3

#define STRINGIZE(x) #x

+ (int)getValueForContant:(NSString *)constantStr {
    const char *charStr = STRINGIZE(CONSTANT_1);
    NSString *str = [NSString stringWithUTF8String:charStr];
    if ([constantStr isEqualToString:str]) {
        return CONSTANT_1;
    }

    charStr = STRINGIZE(CONSTANT_2);
    str = [NSString stringWithUTF8String:charStr];
    if ([constantStr isEqualToString:str]) {
        return CONSTANT_2;
    }

    return -1;
}

And use it as:

int constVal = [ClassName getValueForContant:@"CONSTANT_1"];

It'll give you int value in return.

2 Use .plist to define constants and loop through to get appropriate value as provided string parameter to this method;

+ (int)getValueForContant:(NSString *)constantStr
like image 96
D4ttatraya Avatar answered Oct 23 '22 04:10

D4ttatraya


I would recommend to better use constants

NSString *const CONSTANT_1 = @"1";
NSString *const CONSTANT_2 = @"2";
NSString *const CONSTANT_3 = @"3";

The you could use CFBundleGetDataPointerForName

-(NSString *)valueOfConstantWithName:(NSString *)constantName {
    void ** pointer = CFBundleGetDataPointerForName(CFBundleGetMainBundle(),   (__bridge CFStringRef)constantName);
    return (__bridge NSString *)(pointer ? *pointer : nil);
}

and use that method like

NSString *costant1 = [self valueOfConstantWithName:@"CONSTANT_1"];
like image 26
Black Sheep Avatar answered Oct 23 '22 03:10

Black Sheep