Marti Markov
Marti Markov

Reputation: 766

Rewriting C Struct to ObjC Class (C Struct vs ObjC Class)

I read this article: http://www.koboldtouch.com/display/IDCAR/Use+Classes+Instead+of+Structs

and I was already thinking that I would want to use classes instead of structs in my ObjC application but the article says that it requires a little bit more memory.

I also read this question's selected answer: Class vs Struct for data only? and it says that the variables in the class defaults to private but it is for C++ do they default to private in ObjC? I think not because if I have a @property in ObjC then I can access it from another object.

Finally if I'm just going to create an object that implements only a C struct what is the difference of using @public {} and @property?

EDIT

To future readers both three answers need to be read all of them answer the question but I had to select and answer so this is why I chose one.

Upvotes: 1

Views: 290

Answers (3)

newacct
newacct

Reputation: 122449

and it says that the variables in the class defaults to private but it is for C++ do they default to private in ObjC?

Instance variables declared in an @interface in Objective-C default to @protected. Instance variables declared in an @implementation in Objective-C default to @private. You can manually use @public, @protected, @private just like in C++.

I think not because if I have a @property in ObjC then I can access it from another object.

Property access is calling a method. It is completely different from instance variable access.

Upvotes: 2

Grady Player
Grady Player

Reputation: 14549

Do it the easy way first, and then if you reveal that profiling the actual running code the memory footprint is too great, then reconsider...

basically a Obj-C Class is a struct. It is a struct that has a pointer to the class object, and all of the ivars declared by the super class, and that class.

you gain so much by using Classes and objects in ease of memory management, and collection classes you would be foolish not to (except in very special circumstances)

For the second question... you typically always want to use properties over obj->iVar

this keeps encapsulation, and also allows validation on setters... and also fires KVO notifications.

So basically:

Use Objects/Classes unless you have a reason not to.
Use Properties, unless you have a reason not to.

Upvotes: 2

trojanfoe
trojanfoe

Reputation: 122391

I wouldn't worry about the additional memory requirements of an Objective-C class compared to a struct, unless you are storing a great many objects in memory. Then it might be worth considering. You get other benefits by using an Objective-C class.

WRT to public vs properties; you don't want code outside of the object messing with your instance variables "generally". In some cases it doesn't matter, and you can use @property/@synthesize without worrying, however there are times you might want to either validate the value being assigned or do something when a particular value is assigned. Using properties from the outset allows you to change this later without affecting any code that uses your classes.

Upvotes: 3

Related Questions