Reswift: Ambiguous reference to member 'subscribe(_:transform:)'

Created on 26 Mar 2018  路  6Comments  路  Source: ReSwift/ReSwift

I cannot explain why the compiler can't compile this minimal example.
For the line store.subscribe(self) I always get Ambiguous reference to member 'subscribe(_:transform:)
Did I miss something or is that a bug?

Version: 4.0.1
Swift: 4.0

I would appreciate any help. Thanks

struct AppState: StateType {
    var studentState: StudentState
}
struct StudentState: StateType {
    var students: [String: [Student]] = [:]
}

let store = Store<AppState>(
     reducer: appReducer,
     state: AppState(studentState: StudentState())
)

class StudentVC: UIViewController, StoreSubscriber {
    override func viewWillAppear(_ animated: Bool) {
        store.subscribe(self) { <--------- Ambiguous reference to member 'subscribe(_:transform:)
            $0.select {
                $0.studentState
            }
        }
    }

    func newState(state: StudentState) {
    }
}

Most helpful comment

Perhaps clean & build, or restart xcode/your computer? I just tested the following:

import ReSwift

struct Student {}

struct AppState: StateType {
    var studentState: StudentState
}
struct StudentState: StateType {
    var students: [String: [Student]] = [:]
}
func appReducer(_ action: Action, state: AppState?) -> AppState {
    return state ?? AppState(studentState: StudentState())
}

let store = Store<AppState>(
    reducer: appReducer,
    state: AppState(studentState: StudentState())
)

class StudentVC: NSObject, StoreSubscriber {
    func start() {
        store.subscribe(self) {
            $0.select {
                $0.studentState
            }
        }
    }

    func newState(state: StudentState) {
    }
}

And it compiles successfully.

All 6 comments

@krema The error is occuring because you're returning ($0.studentState.students) which is of type (StudentState) (note the () making it a tuple), but then you're referencing state: StudentState below.
You can resolve this by removing the parenthesis from the select return value.

I think the issue is that you can only subscribe to sub-states defined in AppState. Which in this case should be $0.studentState and not $0.studentState.students

Ah, yes, thats also causing an issue, since $0.studentState.students is [String: [Student]]
You could select that, but then you'd need func newState(state: [String: [Student]]) instead of state: StudentState

@mjarvis & @amreshkumar
Thanks for the quick response. I updated the code above with your suggestions

  • Removing the parenthesis
  • $0.studentState instead of $0.studentState.students

but still get the same problem.

Perhaps clean & build, or restart xcode/your computer? I just tested the following:

import ReSwift

struct Student {}

struct AppState: StateType {
    var studentState: StudentState
}
struct StudentState: StateType {
    var students: [String: [Student]] = [:]
}
func appReducer(_ action: Action, state: AppState?) -> AppState {
    return state ?? AppState(studentState: StudentState())
}

let store = Store<AppState>(
    reducer: appReducer,
    state: AppState(studentState: StudentState())
)

class StudentVC: NSObject, StoreSubscriber {
    func start() {
        store.subscribe(self) {
            $0.select {
                $0.studentState
            }
        }
    }

    func newState(state: StudentState) {
    }
}

And it compiles successfully.

Thank you for your help. I found the problem. :sweat_smile:

This line caused the issue:

typealias StoreSubscriberStateType = AppState

Didn't recognize it because I also had an additional newState function with AppState in it.

Wow, what a mess馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dolanmiu picture dolanmiu  路  6Comments

coderMemberam picture coderMemberam  路  8Comments

dolanmiu picture dolanmiu  路  7Comments

dolanmiu picture dolanmiu  路  3Comments

eschan picture eschan  路  3Comments