gpt4 book ai didi

ios - Swift - 类没有初始化器

转载 作者:行者123 更新时间:2023-11-28 11:02:43 25 4
gpt4 key购买 nike

我正在尝试通过代码将变量传递给另一个 View 。当用户单击 TableView 数据时,我的应用程序将表数据传递到下一个 View 。但是我在第二个屏幕上看到“类没有初始化程序”:

点击时调用第二个屏幕的表格 View :

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("editFriend") as! EditFriendViewController

self.presentViewController(nextViewController, animated: true, completion: nil)

nextViewController.friend = friendArray[indexPath.row];
}

第二屏代码:

import UIKit
import CoreData

class EditFriendViewController: UIViewController {

var friend: Friends

@IBOutlet weak var fName: UITextField!
@IBOutlet weak var lName: UITextField!
@IBOutlet weak var mobile: UITextField!
@IBOutlet weak var gender: UIPickerView!
@IBOutlet weak var address: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

fName.text = friend.firstName!;
lName.text = friend.lastName!;
mobile.text = friend.mobile!;
address.text = friend.address!;
}

最佳答案

那是因为您定义了一个属性,但没有在您的 init() 方法中对其进行初始化。如果您需要在其他地方初始化属性而不是 init 方法,例如。 viewDidLoad。这在 ViewController 中很常见。所以你需要将属性定义为implicitly unwrapped optional,也就是说,你需要将friend声明为Friends!而不是Friends ,或者如果您需要该属性是可选的,则将其声明为 Friend?

import UIKit
import CoreData

class EditFriendViewController: UIViewController {
var friend: Friends! // declare friend as implicitly unwrapped optional, you are responsible to assign any value to it before using it
....

override func viewDidLoad() {
super.viewDidLoad()
friend = Friend() // Assign a value to friend property
}
}

关于ios - Swift - 类没有初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39928312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com