Ios 在Swift中使用领域

Ios 在Swift中使用领域,ios,swift,xcode,realm,Ios,Swift,Xcode,Realm,我想创建一个使用Realm存储联系人的电话簿。当我尝试检索对象时,由于对象为nil,会出现一些错误。我不确定我应该做什么: 我的代码如下所示: // MainVC.swift: import UIKit import RealmSwift class MainVC: UIViewController,UITableViewDelegate,UITableViewDataSource{ @IBOutlet weak var contact_tableview: UITableVie

我想创建一个使用Realm存储联系人的电话簿。当我尝试检索对象时,由于对象为
nil
,会出现一些错误。我不确定我应该做什么:

我的代码如下所示:

// MainVC.swift:
import UIKit
import RealmSwift

class MainVC: UIViewController,UITableViewDelegate,UITableViewDataSource{

    @IBOutlet weak var contact_tableview: UITableView!
    var realm : Realm!

    var ContactList: Results<Contact> {
        get {
            return realm.objects(Contact.self)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.titleTextAttributes =
            [NSFontAttributeName: UIFont(name: "IRANSansWeb-Medium", size: 17)!]
        contact_tableview.delegate = self
        contact_tableview.dataSource = self

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ContactList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "contactcell") as! ContactCell

        let item = ContactList[indexPath.row]

        cell.lblName!.text = item.first_name
        return cell
    }
}

// Contact.swift:
import Foundation
import RealmSwift

class Contact: Object {
    dynamic var first_name = ""
    dynamic var last_name = ""
    dynamic var work_email = ""
    dynamic var personal_email = ""
    dynamic var contact_picture = ""
    dynamic var mobile_number = ""
    dynamic var home_number = ""
    dynamic var isFavorite = false
}
//MainVC.swift:
导入UIKit
导入RealmSwift
类MainVC:UIViewController、UITableViewDelegate、UITableViewDataSource{
@IBOUTLE弱var触点\u表视图:UITableView!
var领域:领域!
var联系人列表:结果{
得到{
返回realm.objects(Contact.self)
}
}
重写func viewDidLoad(){
super.viewDidLoad()
self.navigationController?.navigationBar.titleTextAttributes=
[NSFontAttributeName:UIFont(名称:“IRANSansWeb媒体”,大小:17)!]
联系_tableview.delegate=self
联系_tableview.dataSource=self
}
func numberOfSections(在tableView:UITableView中)->Int{
返回1
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回ContactList.count
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.devqueueReusableCell(带标识符:“contactcell”)作为!contactcell
let item=ContactList[indexPath.row]
cell.lblName!.text=item.first\u name
返回单元
}
}
//Contact.swift:
进口基金会
导入RealmSwift
类联系人:对象{
动态变量first_name=“”
动态变量last_name=“”
动态var工作\u电子邮件=“”
动态var personal_email=“”
动态变量触点_picture=“”
动态var mobile_number=“”
动态var home_number=“”
动态变量isFavorite=false
}

这是因为您没有实例化一个领域,所以当您尝试访问它时,它是零

改变

var realm : Realm!


完全有效!谢谢你^ ^ ^@b完成了兄弟;-)
let realm = try! Realm()