typedef struct
{
int first;
int second;
} IntPair;
IntPair directions[8] = {
{ -1, -1 }, { 0, -1 }, { 1, -1 },
{ -1, 0 }, { 1, 0 },
{ -1, 1 }, { 0, 1 }, { 1, 1 }
};
上のコード(c言語)をswiftに書き換えたいのですが、上手くいきません。
どなたか教えてください。。。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
いろいろな方法があるとおもいますが、3 つの方法で書いてみました。
-
strunct とその配列にする。 (質問文の C でのコードにもっとも近い)
-
Dictionary にする。
-
Enum にする。
-
strunct とその配列にする。
2. Dictionary にする。
3. Enum にする。
コード全体:
swift
1//: Playground - noun: a place where people can play 2import UIKit 3// ******* struct の配列として実装 ********** 4struct StDirection { 5 var x:Int = 0 6 var y:Int = 0 7} 8let Ds: Array<StDirection> = [ 9 StDirection(x:-1, y:1), StDirection(x:0, y:1), StDirection(x:1, y:1), 10 StDirection(x:-1, y:0), StDirection(x:1, y:0), 11 StDirection(x:-1, y:-1), StDirection(x:0, y:-1), StDirection(x:1, y:-1), 12] 13print(Ds[0]) 14print("x = \(Ds[0].x), y = \(Ds[0].y)") 15 16// ******* 辞書として実装 *********** 17let direction2: Dictionary<String, (x:Int, y:Int)> = [ 18 "LeftTop": (-1, 1), "CenterTop": (0, 1), "RightTop": (1, 1), 19 "LeftMiddle": (-1, 0), "RightMiddle": (1, 0), 20 "LeftBottom": (-1, -1), "CenterBottom": (0, -1), "RightBottom": (1, -1) 21] 22print(direction2["LeftTop"]) 23let lt = "LeftTop" 24print("x =\(direction2[lt]!.x), y =\(direction2[lt]!.y)") 25 26// ******* enum として実装 ********** 27enum EnumDirections:Int { 28 case LeftTop, CenterTop, RightTop, 29 LeftMiddle, RightMiddle, 30 LeftBottom, CenterBottom, RightBottom 31 32 func vec() -> (x:Int, y:Int) { 33 switch self { 34 case .LeftTop: return (x:-1, y: 1) 35 case .CenterTop: return (x: 0, y: 1) 36 case .RightTop: return (x: 1, y: 1) 37 case .LeftMiddle: return (x:-1, y: 0) 38 case .RightMiddle: return (x: 1, y: 0) 39 case .LeftBottom: return (x:-1, y:-1) 40 case .CenterBottom:return (x: 0, y:-1) 41 case .RightBottom: return (x: 1, y:-1) 42 } 43 } 44} 45print(EnumDirections.LeftTop.vec()) 46print("\(EnumDirections.LeftTop.vec().x) , \(EnumDirections.LeftTop.vec().y)")
投稿2015/09/26 02:02
総合スコア22324
0
ベストアンサー
swiftの勉強がてら調べながら書いてみます。
最初は連想配列にしようかと思ったのですが、タプルというのがあったのでそれを使ってみました。
(連想配列については興味があればご自分で調べてみてください)
swift
1let directions = [ 2 ( -1, -1 ), ( 0, -1 ), ( 1, -1 ), 3 ( -1, 0 ), ( 1, 0 ), 4 ( -1, 1 ), ( 0, 1 ), ( 1, 1 ), 5] 6 7print( directions[2].0 )//directions[2].first 8print( directions[2].1 )//directions[2].second 9 10let (first, second) = directions[2] 11print( first, second ) 12
出力
1 -1 1 -1
動作確認はXcode7のPlaygroundで行いました。
【追記】
少し冗長になってしまいますが、連想配列のような感じで名前を付けられるようなので、それを使って書いてみます。
swift
1let directions = [ 2 ( first:-1, second:-1 ), ( first: 0, second:-1 ), ( first: 1, second:-1 ), 3 ( first:-1, second: 0 ), ( first: 1, second: 0 ), 4 ( first:-1, second: 1 ), ( first: 0, second: 1 ), ( first: 1, second: 1 ), 5] 6 7print( directions[2].first, directions[2].second )
出力
1 -1
投稿2015/09/25 00:28
編集2015/09/25 00:40総合スコア16731
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。