Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Iterating on class variables.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Iterating on class variables.




On 30/08/2008, at 12:32, MARTIN Pierre wrote:

Thanks Ofri,

My wondering is about using ivars in such a way. Firstly, ivars are designed for the object's internal usage. Relaying on them from other locations is generally wrong, and some people will argue that even inside the object's implementation direct ivar access is worng.
Well, only my base class would use ivars descriptors, to inspect subclassing object's variables.
So, for each found variable, if this variable is of a certain type, my root class knows it has to be used for SQL query generation.

That's what accessors are for.
This means "@property" then "@synthesize", right? i'm using this already for my stuff. The problem is that if from my base class i loop for each property, get it's accessor, and use it, it will not suit my needs: i don't need every single property of my subclassed object, but only the ones of a certain type ("DBFieldDescriptor").

Properties aren't accessors. These are different concepts that accomplish different things.


Secondly, abusing ivars this way forces all instances of your class to have these ivars, so unless instances themselves need them, it's just a waste of memory.
Hmm...

Finally, this technique is *really* non-standard even if appropriate in your specific case, which will make your code harder to read by other people.
Okay you've made your point. Since i'm totally newbee to Objective-C, i'm going to listen to you and find another way to achieve what i want.

Just another question: i have coded this:
NSUInteger count;
Ivar *vars = class_copyIvarList([self class], &count);
for (NSUInteger i=0; i<count; i++) {
Ivar var = vars[i];
NSLog(@"%s %s", var->ivar_name, var->ivar_type);
}
And i'm getting warnings about "ivar_name" and "ivar_type" getting deprecated. Should i bother this kind of warning in my future experience of Objective C?

Thanks,
Pierre.

That's because the Ivar's fields are private in the new runtime. Use the ivar_getName() and ivar_getTypeEncoding() functions instead. Also note that class_copyIvarList() uses unsigned int as the count, which is not the same as NSUInteger.
As a general rule, when you get deprecated warnings it means you're using a deprecated API and you should use some other API :)

Ofri
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Iterating on class variables. (From: MARTIN Pierre <email@hidden>)
 >Re: Iterating on class variables. (From: Ofri <email@hidden>)
 >Re: Iterating on class variables. (From: MARTIN Pierre <email@hidden>)
 >Re: Iterating on class variables. (From: Ofri <email@hidden>)
 >Re: Iterating on class variables. (From: MARTIN Pierre <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.