“未解析标识符的使用"与斯威夫特 [英] "Use of unresolved identifier" with Swift

查看:30
本文介绍了“未解析标识符的使用"与斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Swift 重写我的一个应用,该应用显示南柯哈拉的实时天气数据.到目前为止喜欢斯威夫特!

I am rewriting one of my apps in Swift, that displays live weather data for South Kohala. Love Swift so far!

我遇到了一个小问题,无法解决问题.我有一个基于标签栏的应用程序,仅限 iPad.

I'm having one small problem that is holding things up. I have a tab bar based app, iPad only.

我的一个选项卡是一个 UIViewController,在故事板中添加了一个 TableView 以显示 NOAA 预测.我有一个数据类 Data,它从我们的服务器检索列表并在 viewDidLoad 中创建一个常量 List.它是一个数组数组,每个子数组中有四个字符串.

One of my tabs is a UIViewController with a TableView added to it in the storyboard to display NOAA forecasts. I have a data class Data that retrieves the list from our server and creates a constant List in viewDidLoad. It is an array of arrays with four strings in each subarray.

我已经验证它在那里,因为我可以创建一个常量:List[0]println 所有的字符串.

I have verified that it's there, as I can create a constant: List[0] and println all the strings.

但是,当我使用它来填充表视图时,出现使用未解析的标识符"错误.如果相反,我尝试将单元格标题设置为测试",这样可以正常工作.

However, when I go to use it to populate the table view, I get a "Use of unresolved identifier" error. If instead, I try setting the cell Title to "Test" that works OK.

这似乎是一个范围问题,但由于常量是在类中创建的,我只是不明白为什么它在 tableView 函数中不可见.我已经看了几十篇关于这个的帖子,但我发现没有任何帮助.代码如下:

This seems like a scope issue, but as the constant is created within the class, I just don't understand why it's not visible in the tableView function. I've looked at dozens of posts on this, but nothing I have found seems to help. Here is the code:

import UIKit

class ForecastsViewController: UIViewController,   UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var forecastsTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let instanceofData: Data = Data()
        let list = instanceofData.forecastsList() as NSArray
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
      var forecastList:NSArray = list[indexPath.row] //selects one of the arrays from List
      cell.textLabel?.text = forecastList[0] as string
           return cell
    }

推荐答案

这是因为 List 纯粹是局部于 viewDidLoad 内部的;在你声明的地方,其他方法看不到它.

It is because List is purely local to the inside of viewDidLoad; where you've declared it, other methods cannot see it.

对比,例如forecastsTable,它声明在outside任何方法,表示任何方法都可以看到.

Contrast, for example, forecastsTable, which is declared outside any method, which means that any method can see it.

这种行为称为作用域",对任何编程语言都至关重要.方法内部声明的变量在方法外部不可见,也不会在该方法运行完毕后持续存在.因此,在您的示例中,当 cellForRowAtIndex 运行时,不仅 Listvisible,它甚至不存在cellForRowAtIndex 运行时 - 它早就被销毁了.

This behavior is called "scope" and is crucial to any programming language. Variables declared inside a method are neither visible outside it nor do they persist when that method has finished running. Thus, in your example, not only is List not visible when cellForRowAtIndex runs, it doesn't even exist when cellForRowAtIndex runs - it has been destroyed long before.

这篇关于“未解析标识符的使用"与斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆