Reputation: 155
I know that I can use @private keyword to declare my instance variable private. Which I believe it will only get enforced at compile time.
Do you know of any way to prevent other users from accessing my private variable at run time?
I'm asking this because I'm building a static library which will get distributed to other developers and I don't want them to have access to my private variables.
Upvotes: 0
Views: 196
Reputation: 993
I do not think you can prevent other access to private variable at run time.Because others can use KVC and KVO to get and change your private variable. Maybe you can make lots of relationship between your private variable , if other change it , they will get wrong answer , maybe it can prevent other to change your private variable
Upvotes: 0
Reputation: 539725
Instead of using @private in your (public) .h file you can declare the instance variables in a class extension in your .m file. This will also not prevent a determined person from accessing it, as @KevinGrant correctly said, but at least other people will not see the declaration (and the debugger will not show it, I assume).
Added: See e.g. Objective C - Make iVars hidden for a discussion on that topic.
Upvotes: 1
Reputation: 5431
You can't prevent a determined person from messing with data at runtime (once they know the address of an object in memory and it's in an allocated space that is freely writable by the current process, there is always a way to change it). The best you could probably do is to obfuscate it, e.g. show only a void*
, allocate memory somewhere else and cast.
Upvotes: 2