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
Where to place your typedef struct declaration file ?
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="nabl" data-source="post: 922398" data-attributes="member: 43512"><p>1. NSRange is just a C struct. To set its value, you have to set the components individually. For your example, you’d have to do</p><p>[code]Range.location = 17;</p><p>Range.length = 4;[/code]</p><p>NSMakeRange does this for you and returns the resulting NSRange.</p><p></p><p>2. This is largely your choice. I would personally put it either in MyClass.h (outside the interface block) if you’ll only be using it in MyClass.m, or if you plan on using it in various other files, perhaps create an entirely separate header (for example, Definitions.h) and then import that into your other classes as necessary.</p><p></p><p>—</p><p></p><p>I would also like to point out some stylistic conventions; it’s not that your code is wrong, but most Objective-C code that you see will follow these. Typically, only class names begin with capital letters while instances of those classes, their instance variables, and method names begin with lowercase letters. In addition, rather than using underscores, most Objective-C code uses “camelCase,” where you simply place a capital letter where a new word begins.</p><p></p><p>As an example, I would change your main.m code to this:</p><p>[code]#import "MyClass.m"</p><p></p><p>int main(int argc, char * argv[])</p><p>{</p><p> id classInstance = [ [MyClass alloc] init];</p><p> [classInstance genericMessage];</p><p> [classInstance release];</p><p> return 0;</p><p>}[/code]</p><p>Of course, you’d also have to make the appropriate changes to the header and class files (for example, changing the Range instance variable to range).</p><p></p><p>One more recommendation: don’t get into the habit of using id too much. There are times when it is a lifesaver, but using it everywhere makes the code harder to read and removes some of the benefits of compile-time checking (explained below), leaving errors to show up during runtime.</p><p></p><p>For example, when you’re initializing an instance of MyClass in your code, try this:</p><p>[code]MyClass *classInstance = [ [MyClass alloc] init];[/code]</p><p>When you know that a variable will only hold one type of data for its entire life, you’ll typically want to initialize it like this, which is called <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-TPXREF113" target="_blank">static typing</a>.</p><p></p><p>As for what I mentioned about compile-time checking, say you were to accidentally send the <span style="font-family: 'Lucida Console'">generalMessage</span> message to your instance of MyClass. As you can see, <span style="font-family: 'Lucida Console'">generalMessage</span> is not defined in MyClass.h. If you initialize the instance with <span style="font-family: 'Lucida Console'">MyClass *classInstance</span>, the compiler will see that you are sending a message that MyClass doesn’t know, and the compiler will warn you. However, if you initialize the instance with <span style="font-family: 'Lucida Console'">id classInstance</span>, the compiler will not recognize the error, and the program will run fine until the program reaches that line, at which point it will throw an exception and crash—something like this:</p><p>[code]2009-10-10 17:14:02.083 Untitled[610:a0f] -[MyClass generalMessage]: unrecognized selector sent to instance 0x10010c650</p><p>2009-10-10 17:14:02.086 Untitled[610:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyClass generalMessage]: unrecognized selector sent to instance 0x10010c650'[/code]</p><p></p><p>For more information, check out <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163-CH1-SW2" target="_blank">The Objective-C 2.0 Programming Language</a>.</p><p></p><p>Hope that helps!</p></blockquote><p></p>
[QUOTE="nabl, post: 922398, member: 43512"] 1. NSRange is just a C struct. To set its value, you have to set the components individually. For your example, you’d have to do [code]Range.location = 17; Range.length = 4;[/code] NSMakeRange does this for you and returns the resulting NSRange. 2. This is largely your choice. I would personally put it either in MyClass.h (outside the interface block) if you’ll only be using it in MyClass.m, or if you plan on using it in various other files, perhaps create an entirely separate header (for example, Definitions.h) and then import that into your other classes as necessary. — I would also like to point out some stylistic conventions; it’s not that your code is wrong, but most Objective-C code that you see will follow these. Typically, only class names begin with capital letters while instances of those classes, their instance variables, and method names begin with lowercase letters. In addition, rather than using underscores, most Objective-C code uses “camelCase,” where you simply place a capital letter where a new word begins. As an example, I would change your main.m code to this: [code]#import "MyClass.m" int main(int argc, char * argv[]) { id classInstance = [ [MyClass alloc] init]; [classInstance genericMessage]; [classInstance release]; return 0; }[/code] Of course, you’d also have to make the appropriate changes to the header and class files (for example, changing the Range instance variable to range). One more recommendation: don’t get into the habit of using id too much. There are times when it is a lifesaver, but using it everywhere makes the code harder to read and removes some of the benefits of compile-time checking (explained below), leaving errors to show up during runtime. For example, when you’re initializing an instance of MyClass in your code, try this: [code]MyClass *classInstance = [ [MyClass alloc] init];[/code] When you know that a variable will only hold one type of data for its entire life, you’ll typically want to initialize it like this, which is called [URL="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-TPXREF113"]static typing[/URL]. As for what I mentioned about compile-time checking, say you were to accidentally send the [FONT="Lucida Console"]generalMessage[/FONT] message to your instance of MyClass. As you can see, [FONT="Lucida Console"]generalMessage[/FONT] is not defined in MyClass.h. If you initialize the instance with [FONT="Lucida Console"]MyClass *classInstance[/FONT], the compiler will see that you are sending a message that MyClass doesn’t know, and the compiler will warn you. However, if you initialize the instance with [FONT="Lucida Console"]id classInstance[/FONT], the compiler will not recognize the error, and the program will run fine until the program reaches that line, at which point it will throw an exception and crash—something like this: [code]2009-10-10 17:14:02.083 Untitled[610:a0f] -[MyClass generalMessage]: unrecognized selector sent to instance 0x10010c650 2009-10-10 17:14:02.086 Untitled[610:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyClass generalMessage]: unrecognized selector sent to instance 0x10010c650'[/code] For more information, check out [URL="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163-CH1-SW2"]The Objective-C 2.0 Programming Language[/URL]. Hope that helps! [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Where to place your typedef struct declaration file ?
Top