类型 'AnyObject' 的值没有成员 ''。

5
对于学校项目,我正在制作一个有关大屠杀的应用程序。我需要在detailViewController中显示有关集中营的信息,但它无法正常工作。我能够访问变量的“名称”值(如果我的术语不正确,请谅解),但没有其他信息。其他帖子不是使用Swift编写的,或者与我的特定问题无关,因此我找不到解决方案。
这是创建变量类型的类中的代码:
class DeathCamp: NSObject {

var name:String = ""
var locationCountry:String = ""
var jewishDeathCount:Int = 0
init(name:String, locationCountry:String, jewishDeathCount:Int){
    self.name = name
    self.locationCountry = locationCountry
    self.jewishDeathCount = jewishDeathCount
}



}

这里是设置细节项到数组中的代码:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)     {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let deathCampNewObject = deathCampArray[indexPath.row]
            let countryDeathNewObject = countryArray[indexPath.row]
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.detailItemDeath = deathCampNewObject
            controller.detailItemCountry = countryDeathNewObject
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}

针对评论中的建议,这里是我的完整代码,用于DetailViewController:

import UIKit

class DetailViewController: UIViewController {

    @IBOutlet weak var detailDescriptionLabel: UILabel!

@IBOutlet var percentageJewsKilled: UILabel!
@IBOutlet var jewsKilled: UILabel!
let masterViewController = MasterViewController()

var detailItemDeath: AnyObject? {
    didSet {
        // Update the view.
        self.configureView()
    }
}
var detailItemCountry: AnyObject? {
    didSet {
        // Update the view.
        self.configureView()

    }
}

func configureView() {
    // Update the user interface for the detail item.
    if let detail = self.detailItemDeath {
        if let label = self.detailDescriptionLabel {
            label.text = detailItemDeath?.name
        }
        if let label = self.jewsKilled {
            label.text = "\(detailItemDeath?.jewishDeathCount)"
        }
    }
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.configureView()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

1
没有所有的代码,很难理解是什么在调用什么。configureView 属于哪个类?谁调用它?发布 DetailViewController 的代码。 - ryantxr
1个回答

3

更改

if let detail = self.detailItemDeath

变成:

if let detail = self.detailItemDeath as? DeathCamp

上面的代码尝试将detailItemDeath强制转换为DeathCamp类型的对象。

同时,将

detailItemDeath?.name

变成:

detail.name

您已经使用if let语法对detailItemDeath进行了解包,可选链接是多余和错误的。

还有,我简直不敢相信我错过了这个,将detailItemDeath.name中的detailItemDeath重命名为detail


我刚刚修改了那行代码,但它仍然给我相同的错误。 - Ben A.
@BenA:我更新了我的回答。 - Vatsal Manot
感谢您的快速回复。当我按照您的建议去做时,我得到了"Value of optional type 'AnyObject?' not unwrapped; did you mean to use '!' or '?'?" 的信息。 - Ben A.
@BenA:又更新了。 - Vatsal Manot
我按照你建议的重新命名了 .name 和 .jewishDeathCount,现在可以正常工作了!谢谢。 - Ben A.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接