1

I'm currently using iOS UIAutomation and identifying elements with a string generated from an external database that is populated with dynamic data. But iOS uiautomation throws a parser error when the string predicate contains a single quote.

Example:

UIATarget.localTarget().frontMostApp().mainWindow().collectionViews().firstWithPredicate(\"ANY visibleCells.name CONTAINS '" + title + "'")

Note that if title = "Todds Apartment" this locator works fine. But if the string contains a single quote it throws the parser error. So for example if title = "Todd's Apartment" this wouldn't work.

Is there a way for the predicate evaluation within single quotes to contain a single quote?

Richard87
  • 1,592
  • 3
  • 16
  • 29
B Clark
  • 11
  • 1
  • Escape the single quote? try using \' – Braains Aug 03 '15 at 20:55
  • Thanks. Problem is that the string is a variable coming from a third party location that gets passed to the locator without me knowing what it is or if it contains a single quote. I guess I could always do a replace on the variable. Something like title.replace("'", "\'") – B Clark Aug 04 '15 at 21:27
  • That was going to be my next suggestion. – Braains Aug 04 '15 at 21:40

1 Answers1

0

This is really a question about sanitizing input -- escaping strings for inclusion in a query string of some kind.

You should check out this question on escaping javascript strings for sql. You can probably solve your problem minimally by simply replacing \ with \\ and ' with \' in your input string.

Apple's documentation on predicates didn't seem to offer any information about what characters might need to be escaped.

Community
  • 1
  • 1
Ian
  • 11,280
  • 3
  • 36
  • 58