ios - 快速从 Realm 中提取值

标签 ios swift realm

大家好,我是 Realm 的新手,在呈现我需要的一些值时遇到一些问题。我在 tableView 中显示“name”值没有问题,但我现在尝试访问要放入收件人文本字段中的“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)    
    }
}

最佳答案

您的 var 订阅者:结果?包含 Realm 结果数组,您只需要创建一个新的电话号码数组

var phones: [String] = []

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

然后您可以将电话号码添加给您的收件人

controller.recipients = phones

关于ios - 快速从 Realm 中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53211337/

相关文章:

ios - 透明背景变白

ios - 删除单击任何其他字段或任何地方时的清除按钮

ios - 快速更新 Realm 数据库中的值

ios - 在里面!不会在Swift中继承

ios - 自动布局约束警告 "Will attempt to recover by breaking constraint"

ios - 保存 iOS 8 自定义键盘的首选项

swift - 有没有办法延迟 Swift 中的 return 语句?

ios - 由于未捕获的异常 'NSInternalInconsistencyException' 终止应用程序,原因 : 'Only run on the main thread!'

swift - queryOrdered 并不总是以正确的顺序返回 tableViewCell 上的数据

swift - 对象已被删除或无效 Realm