Simple understanding of #define and enum code

What does the code below mean?

#define kSelectedTabDefaultsKey @"Selected Tab" enum { kByName, kBySecretIdentity, }; 

Indicates #define kSelectedTabDefaultsKey @ "Selected tab" means that we define a constant called "kSelectedTableTabKey" whose value is "SelectedTab"? Like NSDictionary one key / value pair?

I think the enumeration simply translates the value of kByName = 0 and the value of kBySecretIdentity = 1. Is #define kSelectedTabDefaultsKey and the enumeration linked together?

I don’t think I understand correctly, because later in the code:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSInteger selectedTab = [defaults integerForKey:kSelectedTabDefaultsKey]; // How does "selectedTab" get back an integer? I thought "kSelectedTabDefaultsKey" // was a key with a String value of @"Selected Tab ? UITabBarItem *item = [self.tabBar.items objectAtIndex:selectedTab]; 
+4
source share
4 answers
 #define kSelectedTabDefaultsKey @"Selected Tab" 

It just tells the compiler that everywhere he sees kSelectedTabDefaultsKey , he should expand it to @"Selected Tab" .

So this is:

 NSInteger selectedTab = [defaults integerForKey:kSelectedTabDefaultsKey]; 

Becomes as follows:

 NSInteger selectedTab = [defaults integerForKey:@"Selected Tab"]; 

selectedTab will end up returning an integerForKey: after the default manager looks at the @"Selected Tab" key in the default database. Presumably, it will be 0 or 1, corresponding to two values ​​in your enumeration.


Enumeration is not associated with the default key, but is convenience.

  enum { kByName, kBySecretIdentity, }; 

This tells the compiler to replace 0 whenever kByName is encountered, and 1 for kBySecretIdentity. No more no less.

Thus, presumably, the value written to the database will be one of 0 or 1. The enumeration exists in such a way that in the code for clarity you can say "kByName" instead of 0. In the code you sent, no enumeration is mentioned, because the value from the default database is simply passed in to select the tab.

It would be wise to check the result from the default database. List the listing as:

  enum { kByName, kBySecretIdentity, kByUnusedSentinal }; 

Then:

 NSInteger selectedTab = [defaults integerForKey:kSelectedTabDefaultsKey]; if ((selectedTab < 0) || (selectedTab >= kByUnusedSentinal)) selectedTab = kByName; 

That way, if God ever writes to the default database, your application will use the kByName tab by default.

+5
source

#define kSelectedTabDefaultsKey @"Selected Tab" means that you define CHANGE TEXT @"Selected Tab" (not "Selected Tab" or Selected Tab ) for kSelectedTabDefaultsKey.

Instead, you can define it as if (x== , and it will replace that text wherever you use a specific name (whether it makes sense or not). This β€œvalue” is (with a few confusing exceptions) ) it is the text that starts with the first character without spaces and ends with the end of the line (missing space).

(In languages ​​that use begin / end instead of {/} , you can create some real malware #defining "begin".)

+2
source

Think of #define as text substitution performed in the source code by the preprocessor before running the compiler. The example above from @bbum illustrates this.

Enumeration is a kind of new type that the compiler understands. It essentially maps the character to an integer (i.e., a constant), but with a stronger type check than if you were using #define.

+1
source

define is similar to const (but processed by the precompiler, not the compiler).

kSelectedTabDefaultsKey is the default (string) name in which the selected tab is saved. after reading the preference, the previously selected tab is again selected in the tab bar. this is probably done when you restart the application.

the listing does not seem to be related.

0
source

All Articles