Monday, November 28, 2022
HomeiOS Developmentios - Swift Mix: Couldn't extract a String from KeyPath Swift.KeyPath

ios – Swift Mix: Couldn’t extract a String from KeyPath Swift.KeyPath


I'm trying to observer any changes in object in the core data instance. Here is my code:

class MyClass {
    @objc dynamic var str: String?
}

final class Article: NSObject {
    @objc dynamic var title: String?
    @objc dynamic var summary: String?
    var myVar: MyClass?
}

Here is were I'm implementing the observers:

func update(article: Article) {
    titleSubscription = article.publisher(for: .title).sink { value in
        print(value)
    } receiveValue: { _ in
        print("I got something")
    }
    summarySubscription = article.publisher(for: .myVar?.str).sink{ _ in
        
    } receiveValue: { _ in
        
    }
}

But I'm getting this error:

Thread 1: Fatal error: Could not extract a String from KeyPath Swift.KeyPath<Examples.Article, Swift.Optional<Swift.String>>

enter image description here

Any of you knows why I'm getting this error or if there is any work around ?

I'll really appreciate your help.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments