Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance member cannot be used on instance of nested type?

I have the following code below and Xcode keeps giving me an error I do not understand how to solve.

class ViewController: UIViewController {
    private var manager: Manager?

    enum Link {
        case faq
        case tos

        var url: String {
            switch self {
            case .faq:
                return "www.google.com"
            case .tos:
                return manager!.isFreeUser ? "www.google.com" : "www.duckduckgo.com"
            }
        }
    }
}

The error is:

Instance member ‘manager’ of type 'ViewController' cannot be used on instance of nested type 'ViewController.Link'

like image 821
user7097242 Avatar asked Dec 14 '19 21:12

user7097242


1 Answers

You are trying to access a property of the ViewController class inside a seperate code environment, being the enum Link.

An easy solution would be to pass your value, in this case manager as a parameter to the enum like so:

enum Link {
    case faq
    case tos(Manager)

    var url: String {
        switch self {
        case .faq:
            return "www.google.com"
        case .tos(let manager):
            return manager.isFreeUser ? "www.google.com" : "www.duckduckgo.com"
        }
    }
}

And whenever you access your enum value, pass in your manager property.

print(Link.tos(self.manager!).url
like image 55
Eilon Avatar answered Sep 22 '22 09:09

Eilon