【发布时间】:2019-11-24 07:18:41
【问题描述】:
我有一个对象数组来将联系人数据存储在模型中。还有一个带有两个任务的视图控制器。在此视图 Controller 中修改和创建联系人。在修改模式下,我将模型数据传入 表视图 didSelectRow ,现在那里有问题。 但在创建模式下,当在第一个 VC 应用程序中触摸添加按钮时会导致此崩溃:致命错误:在隐式展开可选值时意外发现 nil。 主要问题是如何在不崩溃的情况下将空模型传递给 VC2。
型号:
class ContactModel : NSObject {
var identifier : String!
var thumbnailImageData : UIImage?
var givenName : String!
var familyName : String!
var phoneNumbers : [String]!
var emailAddresses : [String]!
override init()
{
super.init();
}
init(identi:String,img:UIImage?,name:String,family:String,phone:[String],email:[String]) {
self.identifier = identi
self.thumbnailImageData = img
self.givenName = name
self.familyName = family
self.phoneNumbers = phone
self.emailAddresses = email
}
class func generateModelArray() -> [ContactModel]{
let contactStore = CNContactStore()
var contactsData = [ContactModel]()
let key = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactImageDataKey,CNContactThumbnailImageDataKey,CNContactPhoneNumbersKey,CNContactEmailAddressesKey,CNLabelPhoneNumberMobile] as [CNKeyDescriptor]
let request = CNContactFetchRequest(keysToFetch: key)
try? contactStore.enumerateContacts(with: request, usingBlock: { (contact, stoppingPointer) in
let givenName = contact.givenName
let familyName = contact.familyName
// let emailAddress = contact.emailAddresses.first?.value ?? ""
let email : [String] = contact.emailAddresses.map { ($0.value as String) }
// let phoneNumber = contact.phoneNumbers.first?.value.stringValue ?? ""
let phone : [String] = contact.phoneNumbers.map { $0.value.stringValue }
let identifier = contact.identifier
var image : UIImage?
if contact.thumbnailImageData != nil{
image = UIImage(data: contact.thumbnailImageData!)!
}
contactsData.append(ContactModel(identi: identifier, img: image, name: givenName, family: familyName, phone: phone, email: email))
})
return contactsData
}
}
第一个表视图确实选择了行:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let storyboard = self.storyboard
{
let vc = storyboard.instantiateViewController(withIdentifier: "InsertContactViewController") as! InsertContactViewController
vc.strTitle = "edit contact"
vc.contactModel = dataArray[indexPath.row]
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
}
}
在第一个 VC 中添加按钮:
@IBAction func btnAdd_DidTouch(_ sender: Any) {
let story = UIStoryboard(name: "Main", bundle: nil)
let vc = story.instantiateViewController(withIdentifier: "InsertContactViewController") as! InsertContactViewController
vc.isCreateContact = true
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
}
VC 2:
var contactModel : ContactModel!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
}else if section == 1{
return contactModel.phoneNumbers.count
}else if section == 2 {
return contactModel.emailAddresses.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell0 = tableView.dequeueReusableCell(withIdentifier: "InsertTableCell0") as! InsertTableCell0
//////crash happend here
cell0.txtFirstName.text = self.contactModel.givenName
//////crash happend here
cell0.txtLastName.text = self.contactModel.familyName
return cell0
}else if indexPath.section == 1 {
let cell1 = tableView.dequeueReusableCell(withIdentifier: "InsertTableCell1") as! InsertTableCell1
cell1.btnDelete.addTarget(self, action: #selector(deleteRowDate(_:)), for: .touchUpInside)
cell1.txtPhoneNumber.placeholder = "Phone Number"
cell1.txtPhoneNumber.text = contactModel.phoneNumbers[indexPath.row]
return cell1
}else {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "InsertTableCell2") as! InsertTableCell2
cell2.btnEmail.addTarget(self, action: #selector(deleteRowDate(_:)), for: .touchUpInside)
cell2.txtEmail.placeholder = "Email"
cell2.txtEmail.text = contactModel.emailAddresses[indexPath.row]
return cell2
}
}
【问题讨论】:
-
将
var contactModel : ContactModel!更改为var contactModel : ContactModel?。应用不会崩溃。