2016-12-21 1 views
2

数字の文字列から解析されたインデックスに基づいて配列から値を取得しようとしています。私はこのエラーに立ち往生しています。このフォーラムの同様の質問に対する他の回答は、より高度な開発者のためのものです(これは私の最初のiOSアプリケーションです)。配列から値を取得する - "関数型ではない文字列の値を呼び出すことはできません"

ウェブサイトから天気予報(それぞれ5桁の "MAFOR"グループ)を検索し、各グループを解析し、風向、速度、予測期間などの各文字を使用して配列から値を検索します。

遊び場コードは以下の通りです、私は間違っているつもりどこに任意の助けに感謝(***を探してください)近かった@shallowThought

//: Playground - noun: a place where people can play 

import UIKit 

var str = "Hello, playground" 

// create array for Forecast Period 
let forecastPeriodArray = ["Existing conditions at beginning","3 hours","6 hours","9 hours","12 hours","18 hours","24 hours","48 hours","72 hours","Occasionally"] 

// create array for Wind Direction 
let windDirectionArray = ["Calm","Northeast","East","Southeast","South","Southwest","West","Northwest","North","Variable"] 

// create array for Wind Velocity 
let windVelocityArray = ["0-10 knots","11-16 knots","17-21 knots","22-27 knots","28-33 knots","34-40 knots","41-47 knots","48-55 knots","56-63 knots","64-71 knots"] 

// create array for Forecast Weather 
let forecastWeatherArray = ["Moderate or good visibility (> 3 nm.","Risk of ice accumulation (temp 0C to -5C","Strong risk of ice accumulkation (air temp < -5C)","Mist (visibility 1/2 to 3 nm.)","Fog (visibility less than 1/2 nm.)","Drizzle","Rain","Snow, or rain and snow","Squally weather with or without showers","Thunderstorms"] 


// retrieve full MAFOR line of several information groups (this will be pulled from a web site) 
var myMaforLineString = "11747 19741 13757 19751 11730 19731 11730 13900 11630 13637" 

// split into array components wherever " " is encountered 
var myMaforArray = myMaforLineString.components(separatedBy: " ") 

let count = myMaforArray.count 
print("There are \(count) items in the array") 

// Go through each group and parse out the needed digits 
for maforGroup in myMaforArray { 
    print("MAFOR group \(maforGroup)") 

    // get Forecast Period 
    var idx = maforGroup.index(maforGroup.startIndex, offsetBy: 1) 
    var periodInt = maforGroup[idx] 
    print("periodInt is \(periodInt)") 

    // *** here is where I am stuck... trying to use the periodInt index value to retrieve the description from the ForecastPeriodArray 
    var periodDescription = forecastPeriodArray(periodInt) 
    print("Forecast period = (forecastPeriodArray(periodInt)") 


    // get Wind Direction 
    idx = maforGroup.index(maforGroup.startIndex, offsetBy: 2) 
    var directionInt = maforGroup[idx] 
    print("directionInt is \(directionInt)") 

    // get Wind Velocity 
    idx = maforGroup.index(maforGroup.startIndex, offsetBy: 3) 
    var velocityInt = maforGroup[idx] 
    print("velocityInt is \(velocityInt)") 

    // get Weather Forecast 
    idx = maforGroup.index(maforGroup.startIndex, offsetBy: 4) 
    var weatherInt = maforGroup[idx] 
    print("weatherInt is \(weatherInt)") 

} 
+0

'forecastPeriodArray(periodInt)'を 'forecastPeriodArray [periodInt]'に置き換えます。 – shallowThought

+0

あなたの 'periodInt'は'文字 'であり、' Int'ではありません - それを変換しようとしていますか? (もしそうなら、http://stackoverflow.com/q/30771119/2976878を参照) – Hamish

答えて

1

インデックスで配列にアクセスしようとしているため、array[index]という表記を使用してください。しかし、あなたのインデックスは正しいタイプでなければなりません。したがって、periodIntIntではないので、forecastPeriodArray[periodInt]は機能しません。現在はそれほど意味をなさないタイプCharacterです。

あなたはおそらく、アレイにアクセスするために整数に文字を変換している達成し、それを使用しようとしている。

var periodInt = Int(String(maforGroup[idx]))! 

あなたは文字が実際に表していないとき場合のエラー処理を追加したい場合があります整数。

関連する問題