LoginSignup
1
3

More than 5 years have passed since last update.

enumへ変数を渡す

Posted at

内部の変数を渡す場合


import UIKit

enum Fruit: CustomStringConvertible {

    case apple
    case banana

    static var appleValue: String = "りんご"
    static var bananaValue: String = "バナナ"

    var description: String {
        switch self {
        case .apple:
            return Fruit.appleValue
        case .banana:
            return Fruit.bananaValue
        }
    }

}

print(Fruit.apple)
print(Fruit.banana)

りんご
バナナ

外部から変数を書き換えたい場合


import UIKit

var aapple = "りんご"
var bbanana = "バナナ"

enum Fruit: CustomStringConvertible {

    case apple
    case banana

    static var appleValue: String = aapple
    static var bananaValue: String = bbanana

    var description: String {
        switch self {
        case .apple:
            return Fruit.appleValue
        case .banana:
            return Fruit.bananaValue
        }
    }

}

print(Fruit.apple)
print(Fruit.banana)

りんご
バナナ
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3