How to declare an Array of Realm Results in Swift 4? - ios

Is it possible to make an array of realm results in swift 4? , I've tried this one var RealmArray = [[Results<Object1>?](),[Results<Object2>?](),[Results<Object3>?](),[Results<Object4>?]()] as [Any] but this looks like not good to me , I want to use and display the data inside of every object to the tableView but I don't know how to properly use their indices. Someone knows how to declare an Array of Realm Results?
Or Do you know how to merge the 4 Realm Results?
This is for my SearchBar(For those who want to know for what I will use this for).
my declaration
var main_tcb_filteredArray:Results<TrialCourtBranches>?
var main_ibp_filteredArray:Results<IBPChapters>?
var main_pao_filteredArray:Results<Pao>?
var main_lac_filteredArray:Results<LegalAidClinics>?
var RealmArray = [[Results<TrialCourtBranches>?](),[Results<IBPChapters>?](),[Results<Pao>?](),[Results<LegalAidClinics>?]()] as [Any]
Filtering for my searchbar
let tcb = realm.objects(TrialCourtBranches.self)
let tcb_predicate = NSPredicate(format: "(branch_name CONTAINS[c] %#)",searchText.lowercased())
main_tcb_filteredArray = tcb.filter(tcb_predicate)
let ibp_predicate = NSPredicate(format: "(chapter CONTAINS[c] %#)",searchText.lowercased())
let ibp = realm.objects(IBPChapters.self)
main_ibp_filteredArray = ibp.filter(ibp_predicate)
let pao_predicate = NSPredicate(format: "office_name CONTAINS[c] %#",data.lowercased())
let pao = realm.objects(Pao.self)
main_pao_filteredArray = pao.filter(pao_predicate)
let lac_predicate = NSPredicate(format: "school CONTAINS[c] %#",data.lowercased())
let lac = realm.objects(LegalAidClinics.self)
main_lac_filteredArray = lac.filter(lac_predicate)
main_combined_filteredArray = [main_tcb_filteredArray!,main_ibp_filteredArray!,main_pao_filteredArray!,main_lac_filteredArray!]
self.tableView.reloadData()

Related

Searching string in NSArray of custom object using NSPredicate

I have two classes like below:
class City : NSObject{
var header:String? = nil
var areas:NSMutableArray? = nil //Contain array of Area
//Return array of City objects
class func generate(cityCount:NSInteger) -> NSMutableArray{...}
}
and
class Area : NSObject{
var title:String? = nil
var address:String? = nil
}
//Return array of Area objects
class func generate(areaCount:NSInteger) -> NSMutableArray {...}
Now, I have Array of City like this declared in my viewcontroller:
var cities = City.generate(200)
and when I search the header inside using NSPredicate it work perfectly
let pred = NSPredicate(format: "SELF.header CONTAINS %#",searchString)
let filteredCities = self.cities.filteredArrayUsingPredicate(pred)
But when I search the cities->areas->address (I want to search address). It is not working. It is always return 0 object. Here what I am trying:
let pred = NSPredicate(format: "SELF.areas.address CONTAINS %#",searchString) //name
let filteredCities = (self.cities as NSArray).filteredArrayUsingPredicate(pred)
EDIT
I need only the area object that contain matching address.
I have tried:
let pred = NSPredicate(format: "ANY areas.address CONTAINS %#",searchString)
This is giving the City object with all area object.
Thanks in advance.
Have try like this
let pred = NSPredicate(format: "ANY areas.address CONTAINS %#",searchString)
Try this bro,
let pred = NSPredicate(format: "SELF contains[c] %#",searchString)
let filteredCities = self.area.filteredArrayUsingPredicate(pred)

Filtering dictionary In Swift

hello I am am new in Swift and trying to implement a search bar on table view. so I followed one tutorial on Youtube and implement the tutorial correctly. But I have some problems working with my data. As in tutorial the data was in NSArray and mine is coming from web service and its in NSDictionary
So My data is saved in NSDictionary and If I have to access the variables I do this
cell.countryNameLabel.text = (((dict["\(indexPath.item)"] as?NSDictionary)!["Countries"] as?NSDictionary)!["name"] as?NSString)! as String
okay In tutorial He has done something like this
var filterTableData = [String]()
var tableData = ["one","two","three"]
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterTableData.removeAll(keepCapacity: false)
let searchPredict = NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredict)
filterTableData = array as! [String]
self.tableView.reloadData()
}
and Now I want to know how can I implement this as I have data stored in NSdictionary and variable is dict
You can store the keys of the dictionary to an Array object
and use it in the function.
var filterTableData = [String]()
var tableData = Array(dict.keys)

Filter by array of Objects searchController

I have created an searchController and therefor i'm trying t make it filter content according to the text in the UISearchController. I've created a custom Object looking like below. I've tried using NSPredicate, but keep getting:
cannot convert value of type NSPredicate to expected type #noescape (organization) throws...
class Organization: Object {
var id: Int = 0
var name: String = ""
var shortName: String = ""
var image: NSData = NSData()
var pinImage: NSData = NSData()
let locations = List<Location>()
}
Then I have an array called sortedLocations in my ViewController which contains a number of Organization Objects.
What I've tried so far:
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF.name CONTAINS[c] %#", searchController.searchBar.text!)
let array = sortedLocations.filter(searchPredicate)
filteredTableData = array as! [Organization]
self.tableView.reloadData()
}
The filter() method of SequenceType does not take an NSPredicate
as an argument, but a closure, e.g.
let filteredTableData = sortedLocations.filter {
$0.name.localizedCaseInsensitiveContainsString(searchText)
}
The closure is called for each array element (here using the shorthand
argument $0) and returns true or false to indicate if the element
is to be included in the filtered result or not.
You can use an NSPredicate to filter an NSArray, that would look
like
let filtered = someNSArray.filteredArrayUsingPredicate(predicate)
but there is no reason to use this if you have a Swift array.

UISearchController searching two arrays

How can I use UISearchController to search two different arrays and create two new filtered arrays based on that search? The parameters should only require that one array contain the string, then if one of the arrays have it, both indexes should be added. As of now, I use
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredKeys.removeAll(keepCapacity: false)
filteredValues.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text)
let array = (values as NSArray).filteredArrayUsingPredicate(searchPredicate)
println(array)
let arrayTwo = (keys as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredKeys = arrayTwo as! [String]
filteredValues = array as! [String]
self.tableView.reloadData()
}
However, this is very bad and often crashes the app due to a difference in the count of the filtered arrays. Please help, I have been stuck on this for a while.
The search should look at the content:
arrayOne = ["title1", "title2", "title3]
arrayTwo = ["message1", "message2", "message3]
and should filter the arrays based on the search title1 as
arrayOneFiltered = ["title1"]
arrayTwo = ["message1"]
Could someone help me with the predicate string and the filtering please?
Again, this is using a UISearchController in a TableViewController
Since Dictionary or NSDictionary already provides key-value pair type mechanism, you can wrap your related values in a dictionary like this:
var dictionary = ["title_key": "title something", "message_key": "message text"]
And then you can make an array of dictionaries.
var masterArrayOfDictionaries = [ dictionary , ["title_key": "title something 2", "message_key": "message text 2"]]
You can make dictionary at runtime and append to array:
var dictionary3 = ["title_key": "title something 3", "message_key": "message text 3"]
masterArrayOfDictionaries.append(dictionary3)
Now your predicate should look like:
//You want to search on the 'message_key' so put it in the predicate
let searchPredicate = NSPredicate(format: "message_key CONTAINS[c] %#", searchController.searchBar.text)
Now apply the predicate as follow:
let arrayFiltered = (masterArrayOfDictionaries as NSArray).filteredArrayUsingPredicate(searchPredicate)
This will give you filtered array having the dictionaries that contains searched text against their message_key

Having trouble in filter array of dictionaries coming from server in swift

The dictionary coming from server is
{
data = ({
email = "a123#gmail.com";
phone = 9804504884;
"user_id" = 11;
username = abcd;
});
}
var dataArray:NSArray = dict.objectForKey("data") as! NSArray
println("names = ,\(dataArray)");
var pre:NSPredicate = NSPredicate(format: "username CONTAINS[c] a")
var result:NSArray = dataArray.filteredArrayUsingPredicate(pre)
println("names = ,\(result)");
I am always getting result blank result array from this swift code.
Please help me to resolve this issue.
Thanks
Do it like this,
let json = try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
let predicate = NSPredicate(format: "username CONTAINS[C] 'a'")
if let filteredArray = json["data"]?.filteredArrayUsingPredicate(predicate) {
// do something with array
}
Note you should wrap your string inside single quotes ''.

Resources