Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding Frustrations Please Help
Quicksilver
Unregistered

 
Post: #1
Coding Frustrations Please Help
For the last few months I have been working on learning Objective-C/Cocoa and it been fine up until now. Now it's turning out to be a big pain in the ass and I desperatley need help. I am going to post a few questions and I sincerely hope that some people can help. Thanks.

First of all I dont fully understand pointers. What kind of thing are they (eg. variable, object instance)? Why would you ever need to have something that points to an object? Why not just send messages directly to it???? And how would you use them? Also, I know that the "*" has something to do with them but what?

Regarding the main.m file..
"int main(int argc, const char *argv[])
{
return NSApplicationMain(argc, argv);
}"

"int main(int argc, const char *argv[])" How can you have a funtion with a type of int? Functions dont have data they just do stuff. Right?

And what the hell is all this stuff "int argc, const char *argv[]" and what does each do or mean?

What is the "ProjectName_Prefix.h" file for?

Right now it contains this:
"#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif"

What's the "#" for?
What's this "#ifdef __OBJC__"??
and this "#endif"

What is this "enum" thing? How is it used?

And why is this such a damn pain in my ass? How did you guys learn all this crap?

Sorry for my frustrations and thanks for any responses.
2003.07.22 10:36 PM
Quote this message in a reply
Jake Offline
Member
***

Posts: 184
Joined: Mar 2005
Post: #2
Coding Frustrations Please Help
I think you have pointers confused a little bit, I think of a pointer as the actual object itself. For instance here is some code

NSObject *myObject;

[myObject doSomething];

The myObject code is just telling the comp to go to a specific point in memory for the functions code, therefore "pointing" the program there.



All the main function does is tell your NSApplication method to run, and that basically loads the nib, waits for crap to happen, handles clicks, ect.

I don't know what those parameters are.

That projectname_prefix.h file is pissing me off to, I have to delete it just to get a new project to compile!

I can't remember what enum is, but I know I used it before...
2003.07.22 10:57 PM
Find all posts by this user Quote this message in a reply
jSTIN
Unregistered

 
Post: #3
Coding Frustrations Please Help
Pointers they are *very* useful. A pointer just points to something else. Umm, I really can't explain this one. They point to memory, for easy accees. You can use them with any data type.

Quote:int main(int argc, const char *argv[])" How can you have a funtion with a type of int? Functions dont have data they just do stuff. Right?


A function can have a any data type, int, float, or myStruct. That detemines what the function can return. So if you had,

short MyFunction();

you could do this.

myShort = MyFunction();

The argc etc can be left out in a lot of instances of main. It basically just is stuff the computer puts in at the begining of your program.



Quote:What's the "#" for?
What's this "#ifdef __OBJC__"??
and this "#endif"



The # is for preproccessor stuff. The #indef _OBJC_ is to checkto see if that file is already loaded.
2003.07.22 11:00 PM
Quote this message in a reply
jSTIN
Unregistered

 
Post: #4
Coding Frustrations Please Help
Oh, and an enum is just to define constants, like this, it will go in order.

enum
{
kMyNUm = 0,

kAnotherNum,

kSomeOtherNum,

kmyLastNum,
};
2003.07.22 11:04 PM
Quote this message in a reply
Mazilurik
Unregistered

 
Post: #5
Coding Frustrations Please Help
Quote:Originally posted by Quicksilver
First of all I dont fully understand pointers. What kind of thing are they (eg. variable, object instance)? Why would you ever need to have something that points to an object? Why not just send messages directly to it???? And how would you use them? Also, I know that the "*" has something to do with them but what?

Pointers are variables that store the position in memory of another item in memory. Basically, they reduce the amount of data that needs to be sent to a function by just passing it a number (memory address) rather than an entire object/structure/whatever, and also allow you to create structures such as linked lists. (They probably have a few other purposes as well; I'm hardly an expert in this area.) The asterisk simply means that the variable is a pointer, e.g." int* ptr = &a" (or "int *ptr = &a", if you like; they both mean the same thing) is a pointer that stores the location of an int. (The ampersand before "a" means that you're taking the address of "a" rather than its value.) Asterisks are also used to dereference pointers (retrieve the data at the location stored in the pointer), e.g. "int b = *ptr" will assign "b" the value of the int that "ptr" refers to.

Quote:"int main(int argc, const char *argv[])" How can you have a funtion with a type of int? Functions dont have data they just do stuff. Right?

Functions can also return values; the "int" before "main" tells what type of value is returned by the function. If the function doesn't return any value, its type is "void". The return value is often just an error code, but functions might also return a pointer to data created inside the function; for instance, a CreateWindow function might return a WindowRef (a type of pointer) to the newly-created window.

Quote:What is the "ProjectName_Prefix.h" file for?

Right now it contains this:
"#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif"

What's the "#" for?
What's this "#ifdef __OBJC__"??
and this "#endif"


The prefix header is added to the beginning of your header files when you compile. The "#" sign is used before compiler preprocessing macros (#include, #define, etc.); in this case, the compiler will check to see if "__OBJC__" has been defined, and if not will import the <Cocoa/Cocoa.h> header; the "#endif" just closes the "#ifdef" statement.
2003.07.22 11:10 PM
Quote this message in a reply
OneSadCookie Offline
Genius Bar
*****

Posts: 2,154
Joined: Feb 2005
Post: #6
Coding Frustrations Please Help
Think of a variable like a box with a name, that you can put stuff in. If you have an int, it'll go in the box. If you have a float, it'll go in the box. If you have an object, it's too big to go in the box, so at lives in a big heap of other objects at number 17 Kehuia Road. Its address goes in the box, so you know where to find the object. The address is called a pointer to the object, because it points out where the object is.

This means that if you have two different variables (boxes) and each has the number 3 in it, you have two different 3s. If you have two different variables each of which has the address 17 Kehuia Road in it, you still only have one object.

Objects in Objective C are always referred to via pointers.

If you have a declaration like NSString* name; that means that name is a box which can hold the address of an NSString object.

The type before the function's name is the type of value that function returns. sin returns a double, for example, and lots of functions return nothing at all (void). main returns an int, which is the exit status of your program. Typically you'll return zero (EXIT_SUCCESS) or one (EXIT_FAILURE).

If your program is run from the command-line, the user can type more things on the command-line after your program's name. The number of things they type comes into main as argc "argument count", the actual things they type come in as zero-terminated C strings into the array argv "argument values". Cocoa's NSApplicationMain handles the arguments for you, so you typically don't have to worry about them in Cocoa.

ProjectName_Prefix.h is a header file which is automatically included at the top of every file in your project. It gets precompiled, which means that files that include it can compile quicker. You'll typically just #import framework headers into your prefix header. It's not crucial, if it's causing problems you can delete it and remove it from the target settings.

# signifies a preprocessor command. C-family languages get compiled in two stages. First the preprocessor runs over the source file doing essentially find-and-replace and copy-and-paste, then that transformed source file is sent to the compiler.

#ifdef means "if defined". __OBJC__ is a preprocessor symbol defined by the compiler whenever it's compiling an Objective C source file.

#import means "if you haven't already, find this file and copy and paste the contents here", so #import <Cocoa/Cocoa.h> will get replaced by the entire contents of /System/Library/Frameworks/Cocoa.framework/Headers/Cocoa.h, which in turn #imports a bunch more files...

enum declares a bunch of integer constants. So if I write

Code:
enum
{
    MOVE_LEFT,
    MOVE_RIGHT,
    MOVE_UP,
    MOVE_DOWN
};

It basically says that I can type MOVE_LEFT instead of zero, MOVE_RIGHT instead of one, &c. It's really just a way to give integer constants meaningful names.

I started by learning Java. It doesn't have a preprocessor or enums, but it does have pointers (though no *s), function return types, and command-line arguments passed to main...
2003.07.22 11:17 PM
Visit this user's website Find all posts by this user Quote this message in a reply
OneSadCookie Offline
Genius Bar
*****

Posts: 2,154
Joined: Feb 2005
Post: #7
Coding Frustrations Please Help
Heh, guess everyone knew those answers. Hopefully everything you'd ever want to know is in there somewhere Smile
2003.07.22 11:18 PM
Visit this user's website Find all posts by this user Quote this message in a reply
macboy
Unregistered

 
Post: #8
Coding Frustrations Please Help
A pointer references an object in memory. Why do this, you ask? So you can modify the object without making a copy of it for this one specific function.

The data type before the function name indicates what data type it returns, as others have said.

Now I noticed nobody could tell what the 'int argc, const char *argv[]' statement does. Well, this doesn't really apply to a Cocoa/Carbon program, but if you're making a program that can be executed from the Terminal, 'argc' is the amount of arguments the program has taken, while the 'argv' array holds all the arguments. And BTW, the path the user used to run the program is always the first argument.

The '#' before a statement means the command is a pragma, this means it's meant only for the preprocessor to read.

#ifdef checks to see if the variable name after it is defined, and if it is, it executes code up to #endif. So, in this case, it's checking whether or not you're using Objective-C.

And welcome back QS Smile

EDIT: Bah, I spent so long thinking about this and typing it, OSC posted right before Tongue
2003.07.22 11:22 PM
Quote this message in a reply
OneSadCookie Offline
Genius Bar
*****

Posts: 2,154
Joined: Feb 2005
Post: #9
Coding Frustrations Please Help
Well when I started typing, nobody had replied yet...
2003.07.22 11:29 PM
Visit this user's website Find all posts by this user Quote this message in a reply
AnotherJake Offline
Member
***

Posts: 159
Joined: Mar 2005
Post: #10
Coding Frustrations Please Help
Hah! Great questions! I wrote out this huge explanation and went to preview it and realized everyone else did the same thing so I'll spare you. Get "The C Programming Language" by Brian Kernighan and Dennis Ritchie ( often referred to as K&R). You don't need to understand it but it has lot of answers to questions like yours. How do we learn this stuff? It's massochism, that's all.
2003.07.22 11:33 PM
Find all posts by this user Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)