質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

981閲覧

'cannot subscript a value of ~'というエラー

退会済みユーザー

退会済みユーザー

総合スコア0

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2016/01/12 13:03

編集2016/01/12 13:04

Swift

1{ 2 let colors: Array = [ 3 [1, 255, 0, 0, 3, 5, 1, 0, 0, 7], 4 [1, 255, 255, 0, 3, 5, 1, 0, 1, 7], 5 [1, 0, 255, 0, 3, 5, 1, 0, 2, 7], 6 [1, 0, 255, 255, 3, 5, 0, 1, 3, 7], 7 [1, 0, 0, 255, 3, 5, 5, 0, 4, 7], 8 [1, 255, 0, 255, 3, 5, 1, 0, 5, 7], 9 [1, 255, 255, 255, 3, 5, 1, 1, 6, 7] 10 ] 11 12 // fadeIn/Out 13 let buf: [UInt8] = [ 14 UInt8(colors[sequencePostCount][0]), 15 UInt8(colors[sequencePostCount][1]), 16 UInt8(colors[sequencePostCount][2]), 17 UInt8(colors[sequencePostCount][3]), 18 UInt8(colors[sequencePostCount][4]), 19 UInt8(colors[sequencePostCount][5]), 20 UInt8(colors[sequencePostCount][6]), 21 UInt8(colors[sequencePostCount][7]), 22 UInt8(colors[sequencePostCount][8]), 23 UInt8(colors[sequencePostCount][9]) 24 ] 25}

いまこんなコードを書いてまして、colorsbufが同じスコープ内にいるときは問題ないのですが、

Swift

1let colors: Array = [ 2 [1, 255, 0, 0, 3, 5, 1, 0, 0, 7], 3 [1, 255, 255, 0, 3, 5, 1, 0, 1, 7], 4 [1, 0, 255, 0, 3, 5, 1, 0, 2, 7], 5 [1, 0, 255, 255, 3, 5, 0, 1, 3, 7], 6 [1, 0, 0, 255, 3, 5, 5, 0, 4, 7], 7 [1, 255, 0, 255, 3, 5, 1, 0, 5, 7], 8 [1, 255, 255, 255, 3, 5, 1, 1, 6, 7] 9 ] 10 11{ 12 // fadeIn/Out 13 let buf: [UInt8] = [ 14 UInt8(colors[sequencePostCount][0]), 15 UInt8(colors[sequencePostCount][1]), 16 UInt8(colors[sequencePostCount][2]), 17 UInt8(colors[sequencePostCount][3]), 18 UInt8(colors[sequencePostCount][4]), 19 UInt8(colors[sequencePostCount][5]), 20 UInt8(colors[sequencePostCount][6]), 21 UInt8(colors[sequencePostCount][7]), 22 UInt8(colors[sequencePostCount][8]), 23 UInt8(colors[sequencePostCount][9]) 24 ] 25}

こんな感じで、colorsをスコープの外に出すと、UInt8(colors[sequencePostCount][0])のところで、cannot subscript a value of arrayと言って怒られてしまいます。

原因と解決方をご存知の方がいたらご教授ねがいます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Stripe

2016/01/12 13:44

sequencePostCountの内容は?
guest

回答1

0

ベストアンサー

swiftの中括弧はクロージャーなのでスコープは作れないですよ。

swift

1_ = { 2 // fadeIn/Out 3 let buf: [UInt8] = [ 4 UInt8(colors[sequencePostCount][0]), 5 : 6 UInt8(colors[sequencePostCount][9]) 7 ] 8}()

こんなのでどうですか?(動作未確認)

■追記
調べてたらこんなのを見つけました。ちょっとシンプル。

swift

1do { 2 // fadeIn/Out 3 let buf: [UInt8] = [ 4 UInt8(colors[sequencePostCount][0]), 5 : 6 UInt8(colors[sequencePostCount][9]) 7 ] 8}

投稿2016/01/12 14:06

編集2016/01/12 14:12
fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問