Hello i'm trying to implement NSUserDefaults strea...
# kotlin-native
a
Hello i'm trying to implement NSUserDefaults stream that listens to changes to on a key The code i found is in objc
Copy code
- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSUserDefaults standardUserDefaults] addObserver:self
                                            forKeyPath:@"SomeKey"
                                               options:NSKeyValueObservingOptionNew
                                               context:NULL];
    // Testing...
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:@"test" forKey:@"SomeKey"];
    [defaults synchronize];
}
- (void)viewDidUnload
{
    [super viewDidUnload];

    [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:@"SomeKey"];
}

- (void)observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
    if([keyPath isEqual:@"SomeKey"])
    {
       NSLog(@"SomeKey change: %@", change);
    }
}
the interesting part is the
self
in the
addObserver
but how to implement that in kotlin native
I'm assuming i will create a kotlin class that inherits from NSObject() and have
observeValueForKeyPath()
method in it but i'm not sure what is this function signature will be
j
a
thank you for response Well i've added the observer.def file in src/nativeInterops and added this part in kotlin block in gradle.build.kts
Copy code
ios {
        compilations.all {
            cinterops {
                val observer by creating {
                    defFile(project.file("src/nativeInterop/cinterop/observer.def"))
                    packageName("kmp.observer")
                }
            }
        }
    }
but i'm always getting unresolved reference at
kmp
in
import kmp.observer
statment
Well never mind i was using it i a common sourceset that includes iosSimulator target as well as ios() target, without adding the introp to the iosSimulator() target, doing so fixed the problem
thanks alot
👍 1