<commit_before>---
layout: post
title: Objective-c
date: 2015-10-20 13:40:00 +0000
summary: Listening for property changes on all properties in a class
---
If you are going to want to use KVO on a class in Objective-c for all properties, your are going to have to use an interesting work around. What I mean is that you are going to have to retrieve all the properties as an array then loop through them and individually assign a KVO listener to each property.

```objective-c
unsigned int count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (size_t i = 0; i < count; ++i) {
  NSString *key = [NSString stringWithCString:property_getName(properties[i])];
  [self addObserver:self forKeyPath:key, options:options, context:context];
}
```

Then you can react to changes by simply adding the following method to your class and react to them as you would normally do in the KVO pattern.
```objective-c
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *);
```<commit_msg>Change title of new post<commit_after>---
layout: post
title: Objective-c wildcard property listener
date: 2015-10-20 13:40:00 +0000
summary: Listening for property changes on all properties in a class
---
If you are going to want to use KVO on a class in Objective-c for all properties, your are going to have to use an interesting work around. What I mean is that you are going to have to retrieve all the properties as an array then loop through them and individually assign a KVO listener to each property.

```objective-c
unsigned int count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (size_t i = 0; i < count; ++i) {
  NSString *key = [NSString stringWithCString:property_getName(properties[i])];
  [self addObserver:self forKeyPath:key, options:options, context:context];
}
```

Then you can react to changes by simply adding the following method to your class and react to them as you would normally do in the KVO pattern.
```objective-c
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *);
```
{'fraction_non_alphanumeric': 0.09082652134423251, 'fraction_numerical': 0.01725703905540418, 'mean_word_length': 5.2585227272727275, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4253043', 'n_tokens_mistral': 657, 'n_tokens_neox': 598, 'n_words': 287}