Property observers in Lua - oop

Is there way to add a property observer in Lua just like Swift does for WillSet and DidSet with properties?
For example, I have a object of display.newText and whenever I change it's text property. I need to do something other as well? Like
object.text = "Changed"
I know I can add a method like setText() or something but I'm looking for property observers in Lua language.
Thanks in advance.

Use a proxy table. This is explained in Pil, Chapter 13.

Related

How do I hookup for each setter

I am programming vb.net. I would like to register everytime a property has been "set". But rather than create a Sub and call it from every property setter, I'd like it to be triggered automatically any time the setter is called. Is there any kind of "hookup" system I can use to achieve this?
There is an existing thread that explains how you can achieve this using the INotifyPropertyChanged Interface:
Implementing INotifyPropertyChangedEvent

Easy way to tell if a method is overriding another method?

I'm just beginning with ObjC. I'm wondering how to find out when looking at code, written by me or from a template that comes when you use the wizard to create a new class, how you can tell if a method is overriding something.
In Java, you can mark a method with #Override, and then it's very easy to see if it's overriding something. That's not foolproof, because #Override is optional, but if I'm still unsure I can just type that in and see if it generates an error.
Is the only way to look up the source of the superclass, or in the case of a framework to read the documentation?
I don't know a way to see this immediately, but you could check if super responds
to the same selector. Example:
- (void)myMethod
{
// Temporarily add this line. If the compiler does NOT complain,
// "myMethod" overrides a method from some superclass.
[super myMethod];
// ...
}
You can use instancesRespondToSelector to see if your instance has an implementation of the method in its object hierarchy.
[MyClass instancesRespondToSelector:#selector(myMethod)];
or depending on what type of checking you need to do
[MyClassSuperClass instancesRespondToSelector:#selector(myMethod)];

Does every action method need a sender object?

Can I just omit the parameter completely? I cannot seem to find a use for it within my IBAction method.
Yes, you can omit it if you don't want it:
-(IBAction)action{
// some stuff
}
Although it can come in handy in a lot of situations
Source : Apple Doc

Calling a class method from another class in Objective-C (Cocoa)

I'm new to programming in Cocoa, so I'm still struggling to grasp some basic concepts.
What I want to do (as an example) is write an application with multiple NSTextFields. However, these NSTextFields need to be linked to separate classes. Additionally, each separate class needs to be able to get and set data from each other.
I tried to add methods to tackle this problem, to no avail. Let's say this is a method in the textbox's original class, and I want to call it from another class.
-(void)settextfield:(NSString*)stringy;
{
[TextField setStringValue:stringy];
}
Here's the calling code (we're calling this from another class, TestClass)...
-(IBAction)test:sender;
{
[BundleBrowseTextBox settextfield: #"Testy"];
}
Nothing happens. There's probably some obvious way to do this, but I haven't been able to unearth this via Google searches.
My mistake was that I was calling the class method instead of the instance... you can call the instance via IBOutlets and defining those outlets properly in Interface Builder.
You need to make sure the pointers you are using are not nil.
One odd/convenient thing about objC is that you can pass messages to nil and it won't crash.
If I'm right in assuming you're trying to set the text in an instance of BundleBrowseTextBox, you should call the settextfield: message on the instance name, rather than on the class name (if BundleBrowseTextBox IS the instance -- rather than the class -- you should really avoid capitalized instance names for clarity). i.e.:
-(IBAction)test:(id)sender;
{
// Assuming bbtBox is defined as an instance of BundleBrowseTextBox
[bbtBox settextfield: #"Testy"];
}
I believe you forgot your parameter type in your original post
this...
-(IBAction)test:sender;
{
[BundleBrowseTextBox settextfield: #"Testy"];
}
should be
-(IBAction)test:(id)sender;
{
[BundleBrowseTextBox settextfield: #"Testy"];
}
That aside if you understand the difference between class and instance as you say you do.
Then it would be nice if you would show us the rest of your implementation and interface.
The problem is probably not in the code snippets you showed us.

How can I iterate over a class keys in objective-C?

How can I iterate over an instance properties in objective-C ?
I need to get all the properties values without having to specify them.
This is the only solution I've found so far, but I was wondering if it could be done with less code: Get an object properties list in Objective-C
thanks
EDIT : #EmptyStack has the right idea - follow his link in the comments on the question :)
Take a look at these methods : http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/c/func/class_copyPropertyList
They let you (amongst other things) find all the properties that a class implements. You can then run through that array of properties and call performSelector: for each property you are interested in.