Swinject / SwinjectAutoregistration

Swinject extension to automatically register your services
MIT License
249 stars 45 forks source link

Showing Ambiguous use of 'init' when opening the project on Xcode version 12.0 + #63

Closed bill0930 closed 3 years ago

bill0930 commented 3 years ago

Showing Ambiguous use of 'init' when opening the project on Xcode version 12.0 +

Background

Currently, a project for my company is using VIPER architeture.

When the Xcode version is 11.7, everything is fine. However, when Xcode version is 12.0+ and opening the same project

, it shows an error Ambiguous use of 'init'

Here's some related example

Sample Code

// RandomServiceAssembly.swift
import Swinject
import SwinjectAutoregistration

func assemble(container: Container) {
  // ....
        container.autoregister(RandomServiceViewProtocol.self, initializer: (RandomServiceView.init) // Ambiguous use of 'init'
  // ...
}
// RandomServiceView.swift

protocol RandomServiceViewProtocol {
    // ...
}
final class RandomServiceView: UIView {
  override init(frame: CGRect) {                        // 1. Found this candidate
        super.init(frame: frame)
        // ....
 }
   required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
   }
}

 extension RandomServiceView: RandomServiceViewProtocol {
  // ....
 } 
// UIView+Utility.swift
extension UIView {
  // ....
    public convenience init(backgroundColor: UIColor) {      // 2. Found this candidate
            self.init()
            self.backgroundColor = backgroundColor
     }
  // ....
}

Things I have done

  1. Checked documentation

When a service has multiple initializers, swift compiler can't be sure which should be used and you will get a ambigious use of init(x: y: z:). This can also happen if the service is extending another class that have initializer with the same number of arguments.

// After implementing the below code  
// Xcode was crashed and shut when building the project.
// After crash, when opening Xcode, it got crashed and closed again and again. Need to change back to Xcode 11.7 to open the    project 
container.autoregister(RandomServiceViewProtocol.self, initializer: (RandomServiceView.init(frame:))  // error gone but crashed on buildtime

container.autoregister(RandomServiceViewProtocol.self, initializer: (RandomServiceView.init as () -> RandomServiceView ) //same
// Crash Thread message
Thread 21 Crashed:: Dispatch queue: XCBuild.XCBBuildOperation.buildOperationHandlerQueue
0   com.apple.dt.IDE.XCBuildSupport 0x000000012fc8a28c specialized IDEXCBuildServiceBuildOperation.buildOperation(_:didEndTask:result:signalled:info:) + 5452
1   com.apple.dt.IDE.XCBuildSupport 0x000000012fc80c29 IDEXCBuildServiceBuildOperation.buildOperationDidEnd(_:metricsData:) + 617
2   com.apple.dt.IDE.XCBuildSupport 0x000000012fc82ef0 protocol witness for XCBBuildOperationDelegate.buildOperationDidEnd(_:metricsData:) in conformance IDEXCBuildServiceBuildOperation + 16
3   com.apple.dt.XCBuild            0x000000012ddbd32a closure #15 in XCBBuildOperation.handleMessage(_:) + 1210
4   com.apple.dt.XCBuild            0x000000012ddff2fe thunk for @escaping @callee_guaranteed () -> () + 14
5   libdispatch.dylib               0x00007fff731a16c4 _dispatch_call_block_and_release + 12
6   libdispatch.dylib               0x00007fff731a2658 _dispatch_client_callout + 8
7   libdispatch.dylib               0x00007fff731a7c44 _dispatch_lane_serial_drain + 597
8   libdispatch.dylib               0x00007fff731a85d6 _dispatch_lane_invoke + 363
9   libdispatch.dylib               0x00007fff731b1c09 _dispatch_workloop_worker_thread + 596
10  libsystem_pthread.dylib         0x00007fff733fca3d _pthread_wqthread + 290
11  libsystem_pthread.dylib         0x00007fff733fbb77 start_wqthread + 15
  1. Update the swinject version

    I updated Swinject and SwinjectAutoregistration version from 2.6 to 2.7 but it seems not help

Right now we could only open and build the project using Xcode 11.7 , there's no way to run and build the project on Xcode 12.0 +.

Does anyone have the issue? If yes, how did you solve it?

Thanks for your help!

mineschan commented 3 years ago

I have encounter similar issue and have no clue how to fix it. Right now we have to stay in Xcode 11

tkohout commented 3 years ago

Hello, can you paste the console output when your app crashes? This issue does not help? https://github.com/Swinject/SwinjectAutoregistration/issues/60

bill0930 commented 3 years ago

I think this crash issue does not related to Swinject. I could finally compile the project in the Xcode Version 12.2 beta 4 (12B5044c) Thanks for your support. I will close the issue now