欠如

来世は貝になりたい

久しぶりのツアーですね Enumerations and Structures

久しぶりのツアーですね テーマはEnumerations and Structures 列挙と構造体となります。

構造体(こうぞうたい、英: structure)はプログラミング言語におけるデータ型の一つで、1つもしくは複数の値をまとめて格納できる型。(wiki) 列挙型(れっきょがた、enumerated type)とは、コンピュータプログラミングにおいて、プログラマが選んだ各々の識別子をそのまま有限集合として持つ抽象データ型である。列挙型は一般に、カードのスートのように番号順を持たないカテゴリ変数として使われるが、実際のコンパイル時あるいは実行時には、列挙型は整数で実装されることが多い。各々の識別子は通例異なる整数値を持つが、複数の識別子に対して意図的に同じ整数値を割り当てる(つまり別名を定義する)ことも可能である。(うぃき)

Use enum to create an enumeration. Like classes and all other named types, enumerations can have methods associated with them.

enumを使用して列挙を作成します。 クラスや他のすべての名前付き型と同様に、列挙型はそれらに関連付けられたメソッドを持つことができます。

enum Rank: Int { case ace = 1 case two, three, four, five, six, seven, eight, nine, ten case jack, queen, king func simpleDescription() -> String { switch self { case .ace: return “ace” case .jack: return “jack” case .queen: return “queen” case .king: return “king” default: return String(self.rawValue) } } } let ace = Rank.ace let aceRawValue = ace.rawValue

上記の結果において 例えば let aces = Rank.jack print(aces) と追加して追記すると func simpleDescription()のswitch文の対応するcaseが返ってきます この場合だとjackなのでprintにはjackという文字列が入ります

By default, Swift assigns the raw values starting at zero and incrementing by one each time, but you can change this behavior by explicitly specifying values. In the example above, Ace is explicitly given a raw value of 1, and the rest of the raw values are assigned in order. You can also use strings or floating-point numbers as the raw type of an enumeration. Use the rawValue property to access the raw value of an enumeration case.

デフォルトでは、Swiftは0から始まり1ずつ増分する生の値を割り当てますが、値を明示的に指定することでこの動作を変更できます。 上記の例では、Aceに明示的に1の生の値が与えられ、残りの生の値は順番に割り当てられます。 列挙型の生の型として文字列または浮動小数点数を使用することもできます。 列挙ケースの生の値にアクセスするには、rawValueプロパティを使用します。

Use the init?(rawValue:) initializer to make an instance of an enumeration from a raw value. It returns either the enumeration case matching the raw value or nil if there is no matching Rank.

init?(rawValue :)イニシャライザを使用して、生の値から列挙のインスタンスを作成します。 生の値と一致する列挙ケースを返します。一致するRankがない場合はnilを返します。

enum Suit { case spades, hearts, diamonds, clubs func simpleDescription() -> String { switch self { case .spades: return “spades” case .hearts: return “hearts” case .diamonds: return “diamonds” case .clubs: return “clubs” } } } let hearts = Suit.hearts let heartsDescription = hearts.simpleDescription()

こちらの例だと先程の例と異なりCaseのheartsを指定しているので case内の"hearts"が文字列として返されます

Notice the two ways that the hearts case of the enumeration is referred to above: When assigning a value to the hearts constant, the enumeration case Suit. hearts is referred to by its full name because the constant doesn’t have an explicit type specified. Inside the switch, the enumeration case is referred to by the abbreviated form . hearts because the value of self is already known to be a suit. You can use the abbreviated form anytime the value’s type is already known.

上記の列挙の心臓ケースが上で言及される2つの方法に注目してください。心臓に値を割り当てるときは、列挙ケースSuit。 心臓は、定数に明示的な型が指定されていないため、フルネームで参照されます。 スイッチの内部では、列挙ケースは省略形で参照されます。 心の価値はすでにスーツであることが知られているからです。 値の型がすでにわかっているときはいつでも省略形を使用できます。

If an enumeration has raw values, those values are determined as part of the declaration, which means every instance of a particular enumeration case always has the same raw value. Another choice for enumeration cases is to have values associated with the case—these values are determined when you make the instance, and they can be different for each instance of an enumeration case. You can think of the associated values as behaving like stored properties of the enumeration case instance. For example, consider the case of requesting the sunrise and sunset times from a server. The server either responds with the requested information, or it responds with a description of what went wrong.

列挙型に生の値がある場合、それらの値は宣言の一部として決定されます。つまり、特定の列挙型のすべてのインスタンスが常に同じ生の値を持つことを意味します。 列挙ケースの別の選択肢は、ケースに関連付けられた値を持つことです。これらの値は、インスタンスの作成時に決定され、列挙ケースの各インスタンスごとに異なる場合があります。 関連付けられた値は、列挙型のインスタンスの格納されたプロパティのように動作すると考えることができます。 たとえば、サーバーから日の出と日没の時刻を要求する場合を考えてみましょう。 サーバーは要求された情報で応答するか、何が問題になったのかの説明で応答します。

enum ServerResponse { case result(String, String) case failure(String) }

let success = ServerResponse.result(“6:00 am”, “8:09 pm”) let failure = ServerResponse.failure(“Out of cheese.”)

switch success { case let .result(sunrise, sunset): print(“Sunrise is at (sunrise) and sunset is at (sunset).”) case let .failure(message): print(“Failure… (message)”) }

上記はenumで作ったcaseに対応してsucces,failureの2つでデータを挿入しswitchで指定したデータを回す感じになっております 現在switch文はsuccessを対象としているので結果はprint(“Sunrise is at (sunrise) and sunset is at (sunset).”)こちらに上記のいれたsuccessのデータが入り表示されます。

Notice how the sunrise and sunset times are extracted from the ServerResponse value as part of matching the value against the switch cases.

スイッチのケースと値を照合する際に、ServerResponseの値から日の出と日没の時刻がどのように抽出されているかに注目してください。

Use struct to create a structure. Structures support many of the same behaviors as classes, including methods and initializers. One of the most important differences between structures and classes is that structures are always copied when they are passed around in your code, but classes are passed by reference.

structを使用して構造体を作成します。 構造体は、メソッドや初期化子を含むクラスと同じ動作の多くをサポートしています。 構造とクラスの最も重要な違いの1つは、構造がコード内を渡されたときに常にコピーされますが、クラスは参照によって渡されるということです。

struct Card { var rank: Rank var suit: Suit func simpleDescription() -> String { return “The (rank.simpleDescription()) of (suit.simpleDescription())” } } let threeOfSpades = Card(rank: .three, suit: .spades) let threeOfSpadesDescription = threeOfSpades.simpleDescription()

こちらは以前作ったenumのデータを参照しデータを表示をしております 今回の例で言うとrankにあるthreeという数値、suitにあるspadesという文字列を参照しreturnで返しています

まぁ久しぶりの更新だったので勉強しつつやっていきたいと思います