Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

How to use in SwiftUI?怎么在SwiftUI中使用? #3

@HarryTMa

Description

@HarryTMa

Hello, I've downloaded the SDK for iOS, and I would like to use it in SwiftUI. It's easy to access the ViewController, but I can hardly conform the protocol UnityAdsDelegate. It requires more methods to overwrite than written. Among them are isEqual, perform, isProxy, isKind, isMember, conforms, and responds. I don't know how to implement them; when I return false to every Bool-resulted function and nil to every Unmanaged-resulted function, the delegate never works when I play an ad. Thank you very much!
您好,我下载了iOS的SDK,我想在SwiftUI中加入它们。我很方便地获得了场景的ViewController,但我很难继承UnityAdsDelegate协议。它需要我重写比你们的示例更多的方法,有:isEqual、perform、isProxy、isKind、isMember、conforms和responds。我不知道该怎么实现它们,当我对所有返回Bool的方法返回false,所有返回Unmanaged类型的方法返回nil,这个delegate在我放广告的时候从来没成功调用过那四个方法。谢谢!

My code:

class UnityListener:UnityAdsDelegate{
    func unityAdsReady(_ placementId: String) {

    }
    
    func unityAdsDidError(_ error: UnityAdsError, withMessage message: String) {

    }
    
    func unityAdsDidStart(_ placementId: String) {

    }
    
    func unityAdsDidFinish(_ placementId: String, with state: UnityAdsFinishState) {

    }
    
    func isEqual(_ object: Any?) -> Bool {
        return false
    }
    
    var hash: Int = 0
    
    var superclass: AnyClass?
    
    func `self`() -> Self {
        return self
    }
    
    func perform(_ aSelector: Selector!) -> Unmanaged<AnyObject>! {
        return nil
    }
    
    func perform(_ aSelector: Selector!, with object: Any!) -> Unmanaged<AnyObject>! {
        return nil
    }
    
    func perform(_ aSelector: Selector!, with object1: Any!, with object2: Any!) -> Unmanaged<AnyObject>! {
        return nil
    }
    
    func isProxy() -> Bool {
        return false
    }
    
    func isKind(of aClass: AnyClass) -> Bool {
        return false
    }
    
    func isMember(of aClass: AnyClass) -> Bool {
        return false
    }
    
    func conforms(to aProtocol: Protocol) -> Bool {
        if aProtocol === self {
            return true
        }
        return false
    }
    
    func responds(to aSelector: Selector!) -> Bool {
        return false
    }
    
    var description: String = ""
    
    
}

Except for those for necessary methods, others are what the compiler asks me to implement.
其中除了那四个必需的方法,其他的都是编译器要求我实现的。

If I don't implement those methods, the compiler says "Cannot declare conformance to 'NSObjectProtocol' in Swift; 'A' should inherit 'NSObject' instead".
如果我不实现这四个方法,编译器报错“Cannot declare conformance to 'NSObjectProtocol' in Swift; 'A' should inherit 'NSObject' instead”。

This problem only occurs when using SwiftUI, and it doesn't appear in traditional Swift.
这个问题在传统Swift上不出现,只在SwiftUI中出现。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions