gpt4 book ai didi

ios - 快速从 Realm 中提取值

转载 作者:行者123 更新时间:2023-11-30 11:02:11 27 4
gpt4 key购买 nike

大家好,我是 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com