[GH-ISSUE #123] Error: Type of expression is ambiguous without more context #76

Open
opened 2026-03-03 19:11:34 +03:00 by kerem · 6 comments
Owner

Originally created by @rustemmanafov on GitHub (Feb 2, 2023).
Original GitHub issue: https://github.com/ProxymanApp/atlantis/issues/123

func swizzleConnectionDidReceiveResponse(anyClass: AnyClass) {
//
// Have to explicitly tell the compiler which func
// because there are two different objc methods, but different argments
// It causes the bug: Ambiguous use of 'connection(
:didReceive:)'
//
let selector: Selector = #selector((NSURLConnectionDataDelegate.connection(_:didReceive:)!)
as (NSURLConnectionDataDelegate) -> (NSURLConnection, URLResponse) -> Void)

    guard let method = class_getInstanceMethod(anyClass, selector),
        anyClass.instancesRespond(to: selector) else {
        return
    }

    typealias NewClosureType =  @convention(c) (AnyObject, Selector, AnyObject, AnyObject) -> Void
    let originalImp: IMP = method_getImplementation(method)
    let block: @convention(block) (AnyObject, AnyObject, AnyObject) -> Void = {[weak self] (me, connection, response) in

        // call the original
        let original: NewClosureType = unsafeBitCast(originalImp, to: NewClosureType.self)
        original(me, selector, connection, response)

        // Safe-check
        if let connection = connection as? NSURLConnection, let response = response as? URLResponse {
            self?.delegate?.injectorConnectionDidReceive(connection: connection, response: response)
        } else {
            assertionFailure("Could not get data from _swizzleConnectionDidReceiveResponse. It might causes due to the latest iOS changes. Please contact the author!")
        }
    }

    // Start method swizzling
    method_setImplementation(method, imp_implementationWithBlock(block))
}

func swizzleConnectionDidReceiveData(anyClass: AnyClass) {
//
// Have to explicitly tell the compiler which func
// because there are two different objc methods, but different argments
// It causes the bug: Ambiguous use of 'connection(
:didReceive:)'
//
let selector : Selector = #selector((NSURLConnectionDataDelegate.connection(_:didReceive:)!)
as (NSURLConnectionDataDelegate) -> (NSURLConnection, Data) -> Void)

    guard let method = class_getInstanceMethod(anyClass, selector),
        anyClass.instancesRespond(to: selector) else {
        return
    }

    typealias NewClosureType =  @convention(c) (AnyObject, Selector, AnyObject, AnyObject) -> Void
    let originalImp: IMP = method_getImplementation(method)
    let block: @convention(block) (AnyObject, AnyObject, AnyObject) -> Void = {[weak self] (me, connection, data) in

        // call the original
        let original: NewClosureType = unsafeBitCast(originalImp, to: NewClosureType.self)
        original(me, selector, connection, data)

        // Safe-check
        if let connection = connection as? NSURLConnection, let data = data as? Data {
            self?.delegate?.injectorConnectionDidReceive(connection: connection, data: data)
        } else {
            assertionFailure("Could not get data from _swizzleConnectionDidReceiveResponse. It might causes due to the latest iOS changes. Please contact the author!")
        }
    }

    // Start method swizzling
    method_setImplementation(method, imp_implementationWithBlock(block))
}
Originally created by @rustemmanafov on GitHub (Feb 2, 2023). Original GitHub issue: https://github.com/ProxymanApp/atlantis/issues/123 func _swizzleConnectionDidReceiveResponse(anyClass: AnyClass) { // // Have to explicitly tell the compiler which func // because there are two different objc methods, but different argments // It causes the bug: Ambiguous use of 'connection(_:didReceive:)' // let selector: Selector = #selector((NSURLConnectionDataDelegate.connection(_:didReceive:)!) as (NSURLConnectionDataDelegate) -> (NSURLConnection, URLResponse) -> Void) guard let method = class_getInstanceMethod(anyClass, selector), anyClass.instancesRespond(to: selector) else { return } typealias NewClosureType = @convention(c) (AnyObject, Selector, AnyObject, AnyObject) -> Void let originalImp: IMP = method_getImplementation(method) let block: @convention(block) (AnyObject, AnyObject, AnyObject) -> Void = {[weak self] (me, connection, response) in // call the original let original: NewClosureType = unsafeBitCast(originalImp, to: NewClosureType.self) original(me, selector, connection, response) // Safe-check if let connection = connection as? NSURLConnection, let response = response as? URLResponse { self?.delegate?.injectorConnectionDidReceive(connection: connection, response: response) } else { assertionFailure("Could not get data from _swizzleConnectionDidReceiveResponse. It might causes due to the latest iOS changes. Please contact the author!") } } // Start method swizzling method_setImplementation(method, imp_implementationWithBlock(block)) } ------------------------------------------ func _swizzleConnectionDidReceiveData(anyClass: AnyClass) { // // Have to explicitly tell the compiler which func // because there are two different objc methods, but different argments // It causes the bug: Ambiguous use of 'connection(_:didReceive:)' // let selector : Selector = #selector((NSURLConnectionDataDelegate.connection(_:didReceive:)!) as (NSURLConnectionDataDelegate) -> (NSURLConnection, Data) -> Void) guard let method = class_getInstanceMethod(anyClass, selector), anyClass.instancesRespond(to: selector) else { return } typealias NewClosureType = @convention(c) (AnyObject, Selector, AnyObject, AnyObject) -> Void let originalImp: IMP = method_getImplementation(method) let block: @convention(block) (AnyObject, AnyObject, AnyObject) -> Void = {[weak self] (me, connection, data) in // call the original let original: NewClosureType = unsafeBitCast(originalImp, to: NewClosureType.self) original(me, selector, connection, data) // Safe-check if let connection = connection as? NSURLConnection, let data = data as? Data { self?.delegate?.injectorConnectionDidReceive(connection: connection, data: data) } else { assertionFailure("Could not get data from _swizzleConnectionDidReceiveResponse. It might causes due to the latest iOS changes. Please contact the author!") } } // Start method swizzling method_setImplementation(method, imp_implementationWithBlock(block)) }
Author
Owner

@rustemmanafov commented on GitHub (Feb 2, 2023):

Hi, Nghia please help me about this.

<!-- gh-comment-id:1413195029 --> @rustemmanafov commented on GitHub (Feb 2, 2023): Hi, Nghia please help me about this.
Author
Owner

@NghiaTranUIT commented on GitHub (Feb 2, 2023):

Can you share with me some context:

  • What Atlantis version, Xcode, and iOS you're using?
<!-- gh-comment-id:1413195958 --> @NghiaTranUIT commented on GitHub (Feb 2, 2023): Can you share with me some context: - What Atlantis version, Xcode, and iOS you're using?
Author
Owner

@rustemmanafov commented on GitHub (Feb 2, 2023):

Xcode version 14.2
Min Deployment 13.0
Atlantis version pod 'atlantis-proxyman', '~> 1.4.3'

<!-- gh-comment-id:1413204476 --> @rustemmanafov commented on GitHub (Feb 2, 2023): Xcode version 14.2 Min Deployment 13.0 Atlantis version pod 'atlantis-proxyman', '~> 1.4.3'
Author
Owner

@rustemmanafov commented on GitHub (Feb 2, 2023):

post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['DEBUG_INFORMATION_FORMAT'] = 'dwarf-with-dsym'
config.build_settings['LD_NO_PIE'] = 'NO'
config.build_settings['CLANG_WARN_DOCUMENTATION_COMMENTS'] = 'NO'
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
config.build_settings['ONLY_ACTIVE_ARCH'] = 'NO'
config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
end
end
end
end

<!-- gh-comment-id:1413205448 --> @rustemmanafov commented on GitHub (Feb 2, 2023): post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['DEBUG_INFORMATION_FORMAT'] = 'dwarf-with-dsym' config.build_settings['LD_NO_PIE'] = 'NO' config.build_settings['CLANG_WARN_DOCUMENTATION_COMMENTS'] = 'NO' config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0' config.build_settings['ONLY_ACTIVE_ARCH'] = 'NO' config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES' end end end end
Author
Owner

@NghiaTranUIT commented on GitHub (Feb 2, 2023):

Please update the Atlantis to the latest version 1.20.0. You're using an 1 years ago version 👍

<!-- gh-comment-id:1413206955 --> @NghiaTranUIT commented on GitHub (Feb 2, 2023): Please update the Atlantis to the latest version [1.20.0](https://github.com/ProxymanApp/atlantis/releases/tag/1.20.0). You're using an 1 years ago version 👍
Author
Owner

@rustemmanafov commented on GitHub (Feb 2, 2023):

Thanks a lot.

<!-- gh-comment-id:1413210771 --> @rustemmanafov commented on GitHub (Feb 2, 2023): Thanks a lot.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/atlantis#76
No description provided.