【问题标题】:Retrieve values from an array - get "cannot call value of non-function type String"从数组中检索值 - 获取“无法调用非函数类型字符串的值”
【发布时间】:2016-12-21 17:24:55
【问题描述】:

我正在尝试根据从一串数字中解析的索引从数组中检索一个值。我被这个错误困住了,这个论坛中类似问题的其他答案似乎是针对更高级的开发人员(这是我的第一个 iOS 应用)。

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

操场代码如下,感谢任何关于我哪里出错的帮助(寻找***)

//: 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)")

}

【问题讨论】:

  • forecastPeriodArray[periodInt]替换forecastPeriodArray(periodInt)
  • 您的periodIntCharacter,而不是Int——您要转换它吗? (如果有,见stackoverflow.com/q/30771119/2976878

标签: swift3


【解决方案1】:

@shallowThought 很接近。

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

您可能想要实现的是将字符转换为整数并使用它来访问数组:

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

当字符实际上不代表整数时,您可能需要添加错误处理。

【讨论】:

    猜你喜欢
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2020-02-01
    • 2021-01-15
    • 2021-03-30
    • 2016-03-10
    • 2016-09-26
    相关资源
    最近更新 更多