2016-12-21 26 views
2

我试图从数组中检索一个值,该值基于从一串数字中解析出的索引。我被困在这个错误,其他答案类似的问题在这个论坛似乎是为更高级的开发人员(这是我的第一个iOS应用程序)。从数组中检索值 - 获取“无法调用非函数类型字符串的值”

该应用程序最终将从网站上查找天气报告(每个5位数的“MAFOR”分组),解析每个组,并使用每个字符从阵列中查找阵列中的风向,速度,预测周期等值。

操场代码如下,感激在哪里我错了任何帮助(找***)

//: 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'是一个'Character',而不是'Int' - 你想转换它吗? (如果是这样,请参阅http://stackoverflow.com/q/30771119/2976878) – Hamish

回答

1

@shallowThought接近。

您试图通过索引访问数组,因此请使用array[index]表示法。但是你的索引必须是正确的类型。 forecastPeriodArray[periodInt]因此不起作用,因为periodInt不是名称所示的Int。目前它是Character型,这没有多大意义。

什么你可能想要实现字符转换为整数,并用它来访问数组:

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

您可能要添加错误处理的情况下,当角色实际上并不代表一个整数。

相关问题