在变量上创建通知

Kev*_*vin 1 cocoa notifications objective-c

我如何设置我的代码,以便我在整数变量上设置一个监听器,这样当值改变时(该值绑定到一个对象),通知被调用?谢谢!

Nik*_*uhe 5

您无法检测到普通C变量的更改.

您可能希望观察对象内部状态的变化.如果是这样,您应该将整数值包装到属性中并使用访问器方法来修改该值.

@interface Foo : NSObject
@property int bar; // declares a property of type int
@end

@implementation Foo
@synthesize bar; // creates accessor methods for the property
@end
Run Code Online (Sandbox Code Playgroud)

可以使用键值观察来检测属性的更改.