Mercurial
Mercurial

Reputation: 2165

Objective C - Add property in runtime

I'd like to add an ivar to an existing objective-c class in runtime, but documentation states that an ivar cannot be an existing class, so I think property could still solve my issue.

As stated here class_addProperty(...) returns true, but when I try to access the ivar by it's name (or the property name) it always returns nil. What could be the issue causing this to happen?

Upvotes: 0

Views: 2251

Answers (2)

Elto
Elto

Reputation: 429

Are you looking for something similar with some other programming language?

it looks like adding properties in AS3, but objc think the best would you use to store NSDictionary objects by keys.

Upvotes: 0

adpalumbo
adpalumbo

Reputation: 3031

You won't be able to add an ivar to the class at runtime. You can think of the class, and its ivars, as something like a C struct. It's layout is defined at compile time.

You can add properties at runtime (since these are just methods), and you can implement their getters and setters, but you'll need to come up with a different way to store any data that they represent.

Upvotes: 1

Related Questions