Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert value of type 'X' to expected argument type 'X'

Xcode 8 and Swift 3 made me really sad today :(

Please have a look and tell me if you ever had something like this and if it's possible to fix it. I've been trying different solutions, among them:

  1. Cmd + Shift + K
  2. Cmd + Shift + Option + K
  3. Delete Derived Data
  4. Change used struct (it's a nested struct in my code), flattened it, change to really basic one

Sad Xcode error

Update 1:

Here's the code (although I think it's not necessarily a problem related to my implementation), it's in my tests target:

let viewModelStub: Quiz.NewRoundDetails.ViewModel = Quiz.NewRoundDetails.ViewModel(roundNumber: "", score: "", proposedAnswerParts: [
            Quiz.NewRoundDetails.ViewModel.AnswersForComponent(answers: []),
            Quiz.NewRoundDetails.ViewModel.AnswersForComponent(answers: []),
            Quiz.NewRoundDetails.ViewModel.AnswersForComponent(answers: [])])

_ = viewController.view
viewController.display(roundModel: viewModelStub)

Here are structs:

struct Quiz {
    struct NewRoundDetails {
        struct Response {
            let roundNumber: Int
            let score: Int
        }
        struct ViewModel {
            let roundNumber: String
            let score: String
            let proposedAnswerParts: [Quiz.NewRoundDetails.ViewModel.AnswersForComponent]

            struct AnswersForComponent {
                let answers: [String]
            }
        }
    }
}

And in viewController things look like this:

func display(roundModel: Quiz.NewRoundDetails.ViewModel) {
    ...
}

Nothing so unusual I think. I have just discovered one more thing - the code works fine on the app target side, it doesn't work on tests target.

I don't have more ideas atm... Can you help me? I have created radar as well

like image 654
Maciek Czarnik Avatar asked Sep 09 '16 21:09

Maciek Czarnik


2 Answers

Check this answer: Stack Overflow

It's probably an issue with targeting. If you're using the @testable key word on the top of your test file and your main project files also target your tests then the tests are referring to two different files. So your files should only target either the tests (if they're a test file) or the main project. If they have both checked uncheck one and use the @testable import [your project's name] at the top of your test file. enter image description here

like image 130
davidrynn Avatar answered Nov 17 '22 03:11

davidrynn


Ok, I got it...

I've imported my app's module with @testable and also I've had added my .swift file with model classes to the test target. Probably <MyTestModule>. Quiz.NewRoundDetails.ViewModel have been created instead of <MyAppModule>. Quiz.NewRoundDetails.ViewModel

like image 23
Maciek Czarnik Avatar answered Nov 17 '22 03:11

Maciek Czarnik