比较数组和Realm数据库中的字符串数据?

时间:2017-11-12 20:40:08

标签: ios arrays swift swift3 realm

我需要比较数组和Realm数据库中的字符串数据。代码中有这个数组:

let months_arr = ["January","February","March","April","May","June","July","August","September","October","November","December"]

有一个创建的Realm数据库,其中包含以下元素:

import Foundation
import RealmSwift
import UIKit

class EventsDB: Object {
@objc  dynamic var dataMonth = ""
@objc  dynamic var dataDay = ""
@objc  dynamic var desc = ""
@objc  dynamic var link = ""

}

(在数据库中已写入dataMonth列,值" April")

有必要比较字符串值" April"在months_arr和" April"在dataMonth。

据我所知,要从数据库中提取数据,我必须做这样的事情:

let dat = realm.objects(EventsDB)
let filter = dat.filter("dataMonth")

1 个答案:

答案 0 :(得分:1)

如果要从EventsDB属性设置为特定月份的Realm中获取dataMonth对象,可以使用以下代码:

let month = "April"
let events = realm.objects(EventsDB.self)
let eventsInApril = events.filter("dataMonth == %@",month)