Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
XCode enum help
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Nethfel" data-source="post: 1064837" data-attributes="member: 89124"><p>I honestly never typedef something unless I actually plan to assign another variable to be what I've typedef'd.</p><p></p><p>Typedef allows you to create a new type of variable identifier. There is no need to use it unless you want to use a unique name to identify a variable type.</p><p></p><p>In your program there is no need for typedef as you have it written.</p><p></p><p>Here's an example program that uses typedef that I whipped up real quick to demonstrate:</p><p></p><p>[code]</p><p></p><p>#import <Foundation/Foundation.h></p><p></p><p>typedef enum {</p><p> EASY,</p><p> NORMAL,</p><p> HARD</p><p>} Settings;</p><p></p><p>int main (int argc, const char * argv[]) {</p><p> NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];</p><p> </p><p> Settings SecondSetting = HARD;</p><p></p><p> Settings DifficultyLevel = EASY;</p><p> </p><p> switch (DifficultyLevel) {</p><p> case EASY:</p><p> NSLog(@"Easy setting");</p><p> break;</p><p> case NORMAL:</p><p> NSLog(@"Normal Setting");</p><p> break;</p><p> case HARD:</p><p> NSLog(@"Hard Setting");</p><p> break;</p><p> default:</p><p> break;</p><p> }</p><p></p><p> switch (SecondSetting) {</p><p> case EASY:</p><p> NSLog(@"Easy setting");</p><p> break;</p><p> case NORMAL:</p><p> NSLog(@"Normal Setting");</p><p> break;</p><p> case HARD:</p><p> NSLog(@"Hard Setting");</p><p> break;</p><p> </p><p> </p><p> default:</p><p> break;</p><p> }</p><p> </p><p> </p><p> </p><p> [pool drain];</p><p> return 0;</p><p>}</p><p>[/code]</p><p></p><p>And the output it generates is:</p><p></p><p></p><p></p><p>As you can see - I have typedef'd the enumeration to a key word Settings. I then defined two variables (DifficultyLevel and SecondSetting) to be of type Settings (and assigned them a respective value)</p><p></p><p>Using typedef is convenient if you don't want to have to retype something like this enum or a struct over and over again.</p><p></p><p>Here's a good description of the purpose of typedef along with more samples: <a href="http://en.wikipedia.org/wiki/Typedef" target="_blank">Typedef - Wikipedia, the free encyclopedia</a></p><p></p><p>I should note - I am leaving out a variable name in one place - in the enum I could have done:</p><p></p><p>typedef enum Settings {</p><p> EASY,</p><p> NORMAL,</p><p> HARD</p><p>} Difficulties</p><p></p><p>then in the program </p><p></p><p>Difficulties difficultyLevel;</p><p>.</p><p>.</p><p>.</p><p>difficultyLevel = EASY;</p><p></p><p>etc.</p></blockquote><p></p>
[QUOTE="Nethfel, post: 1064837, member: 89124"] I honestly never typedef something unless I actually plan to assign another variable to be what I've typedef'd. Typedef allows you to create a new type of variable identifier. There is no need to use it unless you want to use a unique name to identify a variable type. In your program there is no need for typedef as you have it written. Here's an example program that uses typedef that I whipped up real quick to demonstrate: [code] #import <Foundation/Foundation.h> typedef enum { EASY, NORMAL, HARD } Settings; int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Settings SecondSetting = HARD; Settings DifficultyLevel = EASY; switch (DifficultyLevel) { case EASY: NSLog(@"Easy setting"); break; case NORMAL: NSLog(@"Normal Setting"); break; case HARD: NSLog(@"Hard Setting"); break; default: break; } switch (SecondSetting) { case EASY: NSLog(@"Easy setting"); break; case NORMAL: NSLog(@"Normal Setting"); break; case HARD: NSLog(@"Hard Setting"); break; default: break; } [pool drain]; return 0; } [/code] And the output it generates is: As you can see - I have typedef'd the enumeration to a key word Settings. I then defined two variables (DifficultyLevel and SecondSetting) to be of type Settings (and assigned them a respective value) Using typedef is convenient if you don't want to have to retype something like this enum or a struct over and over again. Here's a good description of the purpose of typedef along with more samples: [url=http://en.wikipedia.org/wiki/Typedef]Typedef - Wikipedia, the free encyclopedia[/url] I should note - I am leaving out a variable name in one place - in the enum I could have done: typedef enum Settings { EASY, NORMAL, HARD } Difficulties then in the program Difficulties difficultyLevel; . . . difficultyLevel = EASY; etc. [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
XCode enum help
Top