I have been searching for hours and no solution in sight... I am filtering an array of custom objects using the text typed into a UISearchBar to change the data in the tableview below.
After a bit of debugging, ive pin pointed the source of my troubles:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
var temp = "c"
FilterResults = SearchResults.filter {
return $0.Username.rangeOfString(temp) != nil
}
}
here is my trouble: If I used a temp that is explicitely defined as above , where temp = "c", it happily matches all the user names that have a c in it! The issue arises when instead of using temp, I used the variable searchText, in that cases it never EVER matches with anything! I checked and searchText is not null, in fact I printed out searchText in tests and it printed out a normal string (Based on what is typed in the search bar), but for some crazy reason, if I use the searchText variable inside the .rangeOfString method, it always returns false! Why is that? Ive also used searchBar.text and it gave me the same troubles... I am completely lost and frustrated. Any help would be much appreciated!
Try this if it works.
var searchList = [String]()
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %#", searchText!)
let array = (searchList as NSArray).filteredArrayUsingPredicate(searchPredicate) as! [String]
Turns out it was because I didn't realize that the rangeOfString search is case sensitive!
Related
I have a 2 column sorted view and try to get a document the following code:
var searchArr = new java.util.Vector();
searchArr.addElement(10000310);
searchArr.addElement(45);
var customerdoc:NotesDocument = viw.getDocumentByKey(searchArr,true);
but the result is null.
If I use only the first element for the key (10000310), then I get (the first) doc with that key. But with the 2-element-vector the lookup returns null.
the same in LotusScript works fine:
Dim searchkey(1) As Double
searchkey(0) = 10000307
searchkey(1) = 45
Set doc = luview.Getdocumentbykey(searchkey, true)
gives me the document I need.
Confusing, for me ....
Uwe
This is a known bug, hopefully to be fixed in 9.0.2. See this question getDocumentByKey with view category separated by "\\" in XPages
Your LS example uses an array, not a Vector. I am not even sure if it is intended to work with a Vector - never did that. So just use an array here, too, as the key.
I have a string var dictAsString:String = '["foo" : 123, "bar" : 456]' that I want to convert to a Dictionary (or NSDictionary, I'm not particular.) I've tried
var dictAsObj:AnyObject = dictAsString as AnyObject
var dictAsDict:NSDictionary = dictAsObj as NSDictionary
but that doesn't work. I've also tried
var dictAsDict:NSDictionary = NSDictionary(objectsAndKeys: dictAsString)
and
var dictAsObj:AnyObject = dictAsString as AnyObject
var dictAsDict:NSDictionary = NSDictionary(objectsAndKeys: dictAsObj)
Nothing seems to work, and I can't seem to find any help in the documentation. Any ideas?
That string resembles a JSON object.
You could replace the square brackets with curly brackets and use NSJSONSerialization class to get a dictionary out of it.
Worst case scenario, you should write a little parser.
I suggest using Ragel.
Both tasks are an overkill for a string like that, though.
I have a list AllIDs:
List<IAddress> AllIDs = new List<IAddress>();
I want to do substring operation on a member field AddressId based on a character "_".
I am using below LINQ query but getting compilation error:
AllIDs= AllIDs.Where(s => s.AddressId.Length >= s.AddressId.IndexOf("_"))
.Select(s => s.AddressId.Substring(s.AddressId.IndexOf("_")))
.ToList();
Error:
Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<MyCompany.Common.Users.IAddress>'
AllIDs is a list of IAddress but you are selecting a string. The compiler is complaining it cannot convert a List<string> to a List<IAddress>. Did you mean the following instead?
var substrings = AllIDs.Where(...).Select(...).ToList();
If you want to put them back into Address objects (assuming you have an Address class in addition to your IAddress interface), you can do something like this (assuming the constructor for Address is in place):
AllIDs = AllIDs.Where(...).Select(new Address(s.AddressID.Substring(s.AddressID.IndexOf("_")))).ToList();
You should also look at using query syntax for LINQ instead of method syntax, it can clean up and improve the readability of a lot of queries like this. Your original (unmodified) query is roughly equivalent to this:
var substrings = from a in AllIDs
let id = a.AddressId
let idx = id.IndexOf("_")
where id.Length >= idx
select id.Substring(idx);
Though this is really just a style thing, and this compiles to the same thing as the original. One slight difference is that you only have to call String.IndexOf() one per entry, instead of twice per entry. let is your friend.
Maybe this?
var boundable =
from s id in AllIDs
where s.AddressId.Length >= s.AddressId.IndexOf("_")
select new { AddressId = s.AddressId.Substring(s.AddressId.IndexOf("_")) };
boundable = boundable.ToList();
I've tried with #DbColumn but I get Infinity, this is my current code:
var dbName:NotesDatabase = session.getDatabase("the server", "the database.nsf");
var v:NotesView = dbName.getView("the view").getColumnValues(0);
return v;
But this returns about a hundred results and after that I get distorted text, hieroglyphs, values in different lines etc.
A screenshot of the values:
Now what? Thank you very much!
This is something of a guess because I'm not an XPages guy, but it looks to me like you're saying:
var v:NotesView
And then
return v;
So this means you are declaring that v is a NotesView object. You're actually assigning the columnValues, but the data type is wrong and there is apparently no type-checking going on. So then you return v, and it is being treated as a NotesView object instead of as an array of string values.
Perhaps this is what you need:
var v:NotesView = dbName.getView("the view");
return v.getColumnValues(0);
I need to change a string value if it meets certain criteria, but I'm not having any luck so far. I'm not sure if it's the input string or if I'm doing something fundamentally wrong - here's my code:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableString *stadium = [NSMutableString stringWithString:[prefs stringForKey:#"stadiumname"]];
NSLog(#"stadium: %#", stadium);
if([prefs stringForKey:#"stadiumname"]==#"Brighton & Hove")
{
[stadium setString:#"Hove"];
}
if([prefs stringForKey:#"stadiumname"]==#"Monmore Green")
{
[stadium setString:#"Monmore"];
}
NSLog(#"stadium: %#", stadium);
In the NSLog output on both sides I still get "Brighton & Hove" or "Monmore Green", rather than just "Hove" or "Monmore" as I was expecting. I've tried changing the input field to allow for possible encoding by doing:
NSMutableString *stadium = [NSMutableString stringWithString:[[prefs stringForKey:#"stadiumname"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
but that makes no difference. Would NSLog show up any encoding in the string anyway?
I just need to be able to use the shortened string once it comes out the other side, but so far no luck.
Can anyone enlighten me?
it's because of how you compare the string, so instead of
if([prefs stringForKey:#"stadiumname"]==#"Brighton & Hove")
use
if([[prefs stringForKey:#"stadiumname"] isEqualToString:#"Brighton & Hove"])