Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

core data - Swift - Is there a way to include single quotes (') in a concatenated string?

I have already searched for answers and whatever I tried unfortunately did not work. My case is as follows: I have an array of keywords and I am building an NSPredicate in order to filter data from CoreData. The code below

var strKeywordsOrFilter = "(1 == 1)" //just an initial {true} statement to assist with the OR conditional
            for intIndex in 0...pInputWords.count - 1
            {
                let strKeyword = "'" + pInputWords[intIndex] + "'"
                strKeywordsOrFilter = strKeywordsOrFilter + " OR (imageDescr CONTAINS[c] " + strKeyword + ")"
            }

"imageDescr" is one of the entity attributes in my CoreData. What I want to accomplish is to create a concatenated string of OR conditions for as many keywords exist in the array "pInputWords". When I debug print the variable "strKeywordsOrFilter" it does NOT contain any backslashes before any single quote character, e.g.

(1 == 1) OR (imageDescr CONTAINS[c]  'camping') OR (imageDescr CONTAINS[c]  'lake')

but the same variable when it reaches the "predicate" command, it contains backslashes before every single quote, e.g.

(1 == 1) OR (imageDescr CONTAINS[c]  'camping') OR (imageDescr CONTAINS[c]  'lake')

I have tried a number of workarounds but nothing seems to get rid of the "". Any help would be appreciated because I am really stuck!

Thank you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...