Extracting Values from Realm in swift

Wurl Link

Hey everyone I am a newbie to Realm and I am having some issues to present some of the values I need. I had no problem getting the "name" values to present in the tableView but I am now trying to get access to the "phoneNumber" values to be placed into a recipient textField but after numerous efforts I just can't figure out how to do get access to the "phoneNumber" values. I added pictures and samples of my project hopefully I explained it clearly, any help would be greatly appreciated.

Issue is at "Mark Send SMS Pressed"

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)    
    }
}
Raul Mantilla

your var subscribers: Results? contains an Array of realm results, you'll need to create a new array of phoneNumbers only

var phones: [String] = []

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

then you can add the phone number to your recipients

controller.recipients = phones

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related