快速从Realm中提取值

时间:2018-11-08 15:49:29

标签: ios swift realm

大家好,我是Realm的新手,但是我在提出一些我需要的价值观时遇到了一些问题。我可以很方便地将“名称”值显示在tableView中,但是现在我试图访问要放置在收件人textField中的“ phoneNumber”值,但是经过无数次努力,我只是想不出办法可以访问“ phoneNumber”值。我添加了项目的图片和示例,希望我能清楚地解释它,任何帮助将不胜感激。

问题位于“标记发送的短信已按”

import Foundation
import RealmSwift

class ContactInfo: Object {

    @objc dynamic var name: String = ""
    @objc dynamic var phoneNumber: String = ""
    @objc dynamic var emailAddress: String = ""

}


import UIKit
import RealmSwift
import MessageUI
import ThirdPartyMailer

class SubscribersVC: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    let realm = try! Realm()
    let clients = ThirdPartyMailClient.clients()
    var subscribers: Results<ContactInfo>?

    var constant = Constants()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self

        loadSubscribers()
    }


    //MARK: TO LOAD FROM REALM
    func loadSubscribers() {    
        subscribers  = realm.objects(ContactInfo.self)
        subscribers = subscribers?.sorted(byKeyPath: "name", ascending: true)

        tableView.reloadData()
    }

    @IBAction func postBtnPressed(_ sender: Any) {
    // MARK: SEND SMS PRESSED

        let smsAction = UIAlertAction(title: constant.sendSMS, style: .default) { (action) in
            if MFMessageComposeViewController.canSendText() {
                let controller = MFMessageComposeViewController()

                controller.recipients = subscribers.
                controller.messageComposeDelegate = self
                self.present(controller, animated: true, completion: nil)
            } else {
                // MARK: Add UIAlert stating that "This device is not compatable for sending SMS"
                print(Error.self, "This device is not compatable for sending SMS")
            }
        }

        let emailAction = UIAlertAction(title: constant.sendEmail, style: .default) { (action) in

            let client = ThirdPartyMailClient.init(name: "Yahoo Mail", URLScheme: "ymail", URLRoot: "//mail/compose", URLRecipientKey: "to", URLSubjectKey: "subject", URLBodyKey: "body")
            let application = UIApplication.shared

            if ThirdPartyMailer.application(application, isMailClientAvailable: client) {
                _ = ThirdPartyMailer.application(application, openMailClient: client, recipient: self.constant.emailAddress.joined(separator: ","), subject: nil, body: nil)
            } else {
                print("\(String(describing: client)) is not available")
            }
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
            return
        }
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alert.addAction(smsAction)
        alert.addAction(emailAction)
        alert.addAction(cancelAction)

        self.present(alert,animated: true) {
        }
    }

    @IBAction func backBtnPressed(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
}



//MARK: TableView Ext
extension SubscribersVC: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        
        return subscribers?.count ?? 1
    }    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "namesCell")
        if let subscriber = subscribers?[indexPath.row] {        
            cell!.textLabel?.text = subscriber.name
            cell!.textLabel?.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        }

        return cell!    
    }
}

extension SubscribersVC: MFMessageComposeViewControllerDelegate {
    //    MARK: Text Message Function
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        controller.dismiss(animated: true, completion: nil)    
    }
}

2 个答案:

答案 0 :(得分:0)

如果要添加多个文本字段,则必须创建一个自定义单元格类。让我们将该类称为“ ContactCell”并创建textField插座。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "namesCell") as! ContactCell
    if let subscriber = subscribers?[indexPath.row] {
        cell!.textLabel?.text = subscriber.name
        cell!.textLabel?.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        cell!.recipientTextField.text = subscriber.phoneNumber
    }

    return cell!
}

答案 1 :(得分:0)

您的var订户:结果?包含领域结果数组,则只需创建一个新的phoneNumbers数组

var phones: [String] = []

for subscriber in self.subscribers {
    phones.append(subscriber.phoneNumber)
}

然后您可以将电话号码添加到收件人中

controller.recipients = phones