google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.
Apache License 2.0
3.79k stars 292 forks source link

PreconditionFailure Cannot cast AnyObject to AnyObject #135

Open VitaliyZaharenko opened 4 years ago

VitaliyZaharenko commented 4 years ago

I successfully used Promises in one project until the moment I decided to break it into modules and get "Fatal error: Cannot cast AnyObject to AnyObject" when trying to "then" on promise. It promise returns some class, which implement some public interface.

I created a simplified version of what was happened in the repository on the link

This workspace have a main project and an module as .framework. For clarity of what was at first in my project, i create SomeMainClass and SomeMainStruct. Also I have protocol, which describe provider, thats return promise parameterized with SomeMainClass, SomeMainClass and it's have MainModuleProvider thats implement this provider interface. On other parts of project I use this provider and call "then" on Promise, that's return this provider. All is work as expected.

On the other part in the module I have SomeModuleClass and SomeModuleStruct, ModuleProviderProtocol thats return Promise and Promise. Main project defines ModuleProvider, which implements ModuleProviderProtocol.

I also have some consumer of this provider in Module. When I'm trying to consume provider, i have precondition failure: Cannot cast AnyObject to AnyObject.

//module
public class SomeModuleClass {
    var blabla: String = "blaCls"
    public init() {}
}
public protocol ModuleProviderProtocol {
    func getObjectAsClass() -> Promise<SomeModuleClass>
}
public class ModuleProviderConsumer {
    var provider: ModuleProviderProtocol
    public init(provider: ModuleProviderProtocol) {
        self.provider = provider
    }
    public func consume() {
        provider.getObjectAsClass().then { objectAsClass in
            print(objectAsClass)
        }
    }
}

// main project 
import PromiseModule
final class ModuleProvider: ModuleProviderProtocol {
    func getObjectAsClass() -> Promise<SomeModuleClass> {
        return Promise { resolve, reject in
            resolve(SomeModuleClass())
        }
    }
}

// usage of consumer,  also in main project
import PromiseModule
let consumer = ModuleProviderConsumer(provider: moduleProvider)
consumer.consume()

Additional Info Promises: 1.2.8 Xcode: Version 11.3 (11C29) Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15) Mac Os 10.14.6

brettcallaghan commented 3 years ago

@VitaliyZaharenko, I ran into the same issue as I was migrating from CocoaPods to Swift Package Manager. It turned out that I was also still importing Promises as a pod. Removing the pod seems to have fixed the issue.