I'm fairly new to C and Objective-C, having been taught Python beforehand. As such, there are things about the C family that boggle my mind. I've looked around the web, gotten books, and browsed the forums here, but some things are still bugging me...
I understand variables vs. pointers in theory (one returns a value, and the othe开发者_C百科r returns the address of the value in the system.) What I don't understand is when it's appropriate to use one over the other. Any advice?
When declaring a class, what is a property? It seems like properties and class variables are identical, yet I know there must be some critical difference.
@interface testViewController : UIViewController {
IBOutlet UILabel *label;
IBOutlet UIImageView *uiImageView;
}
@property (nonatomic, retain) IBOutlet UILable *label;
@property (nonatomic, retain) IBOutlet UIImageView *uiImageView;
@end
If possible, although unlikely, could you answer with a comparison to Python? I know Objective-C and Python are night and day, but whatever you can think of would be great.
The books I got were from Apress: Learn C on the Mac, Learn Objective-C on the Mac, and iPhone and iPad Apps for Absolute Beginners.
I really do appreciate any help!
- I understand variables vs. pointers in theory (one returns a value, and the other returns the address of the value in the system.) What I don't understand is when it's appropriate to use one over the other. Any advice?
Unlike Java, Python and .NET where all variables are "pointers to objects" that can be passed around, in C things can exist in two places.
In the program code (variable appears when code is hit, disappears when function returns). Like this:
int my_arr[3];
or, on the "heap" which is memory not a part of the program which is requested dynamically like this:
int *my_arr_pointer = malloc(sizeof(int) * 3);
The second example is close to how Java, Python and .NET pass things around. However when you use malloc() to get memory, you need to use free() on it later...or your program sucks up and wastes the computers memory. So use technique to ensure you use a consistent amount of memory. Use the second to write a more flexible application. C doesn't have memory management which is why the first approach makes a faster, easy to debug program... that requires copying things between functions compared to the second approach where it's easier to have a flexible sized array where the program grows and shrinks in size... but it's slightly more complex to write. Use what is appropriate.
3 .When declaring a class, what is a property? It seems like properties and class variables are identical, yet I know there must be some critical difference.
Properties are wrappers to class variables. A "setter" and a "getter". This allows you to:
a) use a breakpoint to find out where in your code that variable is being set.
b) to check security, or permissions, or to validate the value being set.
c) A property doesn't have to tie to a variable. It can create an "illusion" of it, when the data is dynamically generated. e.g a size property might count the letters in the string when you ask for it, rather than storing it.
d) more flexibility to change the class later without having change everything accessing that property.
精彩评论