1

I'm pretty new to Swift, so the thing is that I have an Enum and I have to return text with either Uppercase or Lowercase letters, and my code doesn't work.

enum UpLow {
    case Uppercase
    case Lowercase

    func setCase(text: String) -> String {
        switch self {
        case .Uppercase:
            return text.uppercased()

        case .Lowercase:
            return text.lowercased()
        }
    }
    setCase(text: "example", case: .Uppercase)
}

and the error I get:

Playground execution failed:

error: demo.playground:60:5: error: expected declaration setCase(text: "example", case: .Uppercase) ^

demo.playground:47:6: note: in declaration of 'UpLow' enum UpLow { ^

Edit: I have to get this thing to work only by calling that function setCase(text: "Chosen text", case: .ChosenCase)

Exercise 5a: Create a function and an enum allows you to define this method: setCase (text: "Interstellar", case: .Uppercase) // INTERSTELLAR setCase (text: "Interstellar", case: .Lowercase) // interstellar * /

4

2 回答 2

2

It makes no sense to put the function into the enum and according to Exercise 5a it's not required.

enum UpLow {
    case uppercase
    case lowercase
}

func setCase (text: String, `case`: UpLow) -> String {
    switch `case` {
    case .uppercase: return text.uppercased()
    case .lowercase: return text.lowercased()
    }
}

let uppercaseString = setCase(text: "Interstellar", case: .uppercase)
let lowercaseString = setCase(text: "Interstellar", case: .lowercase)
于 2019-09-01T17:30:19.237 回答
2

Your setCase(text: String) -> String is an instance method of the UpLow enum. That means that in order to call it, you need to call it on a receiver, which is a variable of type UpLow. For example, UpLow.Uppercase.setCase(text: "Chosen Text")

Furthermore, the function you're trying to call is the global function setCase(text:case:), which doesn't exist.

Unlike a global function, an instance method has an "instance" associated with it. This is the self variable, which acts like an implicit parameter that's made available to you within the body of the method.

A further issue that your call to setCase(text: "example", case: .Uppercase) was done within the declaration of UpLow. This is invalid. A declaration is like an abstract set of definitions. It's not "run" top to bottom the way the rest of a playground is.

You have several ways of fixing this.

  1. You can keep setCase as an instance method, in which case you would use like:

    enum UpLow {
        case uppercase
        case lowercase
    
        func setCase(text: String) -> String {
            switch self {
            case .uppercase: return text.uppercased()
            case .lowercase: return text.lowercased()
            }
        }
    }
    
    UpLow.uppercase.setCase(text: "example")
    
  2. You can create a global function instead (this is the worst choice, IMO):

    enum UpLow {
        case uppercase
        case lowercase
    }
    
    func setCase(text: String, case upLow: UpLow) -> String {
        switch upLow { // Note: there is no "self" in a global function
        case .uppercase: return text.uppercased()
        case .lowercase: return text.lowercased()
        }
    }
    
    setCase(text: "Chosen text", case: .ChosenCase)
    
  3. Or my favourite, make your function as an instance method on String itself:

    enum UpLow {
        case uppercase
        case lowercase
    }
    
    extension String
        func setCase(_ upLow: UpLow) -> String {
            // "self" is the String, "upLow" is the UpLow
            switch upLow { 
            case .uppercase: return self.uppercased()
            case .lowercase: return self.lowercased()
            }
        }
    }
    
    "example".setCase(.uppercase)
    
于 2019-09-01T17:55:43.953 回答