stringByAddingPercentEscapesUsingEncoding adds unexpected characters? - nsstring

I'm having a hard time getting my NSURL to work, when I create the final string before converting to URL it adds unwanted character to the end of the string, why is this happening and how can I fix it?
Here is my code:
NSString *remotepathstring = [[NSString alloc] init];
remotepathstring=newdata.remotepath;
NSLog(#"remotepathstring = %#",remotepathstring);
NSString *remotepathstringwithescapes = [remotepathstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"remotepathstring = %#",remotepathstringwithescapes);
remotepathURL =[NSURL URLWithString:remotepathstringwithescapes];
NSLog(#"RemotePathUrl=%#",remotepathURL);
Log outputs as follows:
"remotepathstring = http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf‎"
"remotepathstring = http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf%E2%80%8E"
"RemotePathUrl=http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf%E2%80%8E"

The sequence %E2%80%8E is a Unicode LEFT-TO-RIGHT MARK. This is present in your original remotepathstring, but invisible when printed out via NSLog.
The question becomes: how does newdata.remotepath get populated in the first place? Somewhere along the line it sounds like you need to perform some extra cleanup of input strings to strip out such a character.
Unrelated to the core question, it would seem you're a newcomer to Objective-C. This code is redundant and wasteful:
NSString *remotepathstring = [[NSString alloc] init];
remotepathstring=newdata.remotepath;
You create a string, only to immediately throw it away and replace it with another. If you're not using ARC, this has the additional problem of leaking! Instead do:
NSString *remotepathstring = newdata.remotepath;

Related

How to remove double qoutes in Objective-C

Let me introduce myself.
My name is Vladimir, C++ programmer, I am from Serbia. two weeks ago I have started to learn objective-C and it was fine until tonight.
Problem:
I cant remove double quotes from my NSLog output.
NSLog(#"The best singers:%#", list.best);
Strings are joined with componentsJoinedByString:#" and "
I would like to get something like this:
The best singers: Mickey and John.
But I get this:
The best singers: ("Mickey", and "John").
I cant remove comma (,) and parentheses either.
I have tried with "replaceOccurencesOfString" but with no success. It can remove any character except qoute and comma.
Also I have used -(NSString *)description method to return string.
You are getting the raw output from your list (which I assume is an array). You will have to do your own formatting to get this to display in the format that you want. You can achieve this by building your string by iterating through your array. Note that this probably isn't the most efficient nor the most robust way to achieve this.
NSMutableString *finalString = [NSMutableString string];
BOOL first = YES;
for (NSString *nameString in list) {
if (first) {
[finalString appendString:nameString];
first = NO;
} else {
[finalString appendString:[NSString stringWithFormat:#" and %#", nameString]];
}
}

Xcode how trim a string with special character $

I've a string with this kind of value /finocchi$FINOCCHamelo
Now I need a new string with only this part of the word FINOCCHamelo
I've try this:
NSString * newString = [item.label stringByReplacingOccurrencesOfString:#"$" withString:#""];
But it's not right becouse change the $ with blank char.
I need to filter the string afther $
How Can I do?
Assuming in all the case you have only one $ in your string and you need the value after $.
NSArray *brokenStrings = [item.label componentsSeparatedByString:#"$"];
NSString *filteredString = [brokenStrings objectAtIndex:1];
There are a lot more ways to do this:
You can opt any one from these methods, or dozen other methods are available in NSString.
– componentsSeparatedByString:
– substringFromIndex:
- rangeOfString:
– stringByTrimmingCharactersInSet:

NSLog NSRange problem

I have a NSSting resultcontent that contains html data which is dynamic and length of html data is always different according to user input.
I could insert my html data in SQLite3 by replacing " from NSString. Now the problem is that SQlite3 doesn't have any inbuilt function to find the position of a particular substring. In SQL you can use CHARINDEX or Patindex for this purpose.
Now I am trying to find the position of this substring "sphinx". I tried NSrange, substringWithRange etc. but I am not getting the exact start and end positions of this substring. How do you show NSRange in console by using NSLog. Here is my code
//find substring "sphinx"
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"sphinx"
options:NSRegularExpressionCaseInsensitive
error:&error];
//Get total length of resultcontent (NSString which contains whole html code)
NSInteger lengthTotal=[resultContent length];
NSLog(#"lengthTotal is %i", lengthTotal);
// Get NSRange
NSRange rangeOfSubstring = [regex rangeOfFirstMatchInString:resultContent
options:0
range:NSMakeRange(0, lengthTotal)];
NSString *substringData = [resultContent substringWithRange:rangeOfSubstring];
if (!NSEqualRanges(rangeOfSubstring, NSMakeRange(NSNotFound, 0)))
{
NSString *substringData = [resultContent substringWithRange:rangeOfSubstring];
NSLog(#"substringData is %#", substringData);
[b] // so far it works fine and shows substringData ="sphinx" in Console
//But folowing line doesn't show start and end position of substringData "sphinx"
NSLog([#"range of substring is =%#",[resultContent substringWithRange:rangeOfSubstring] ];[/b]
}
How do I find the exact position of a substring from NSString? I also tried %S in NSLog but no results.
I also have the same NSString resultcontent Data in SQLite table (text datatype). How do you find index of a character or position of a substring in Sqlite without Charindex and patindex function. As html code is dynamic and length varies according to the user input so I need to know the index or position of substring after each user input.

How to get string from a long string?

I have a string like this:
NSString *aString =
[NSString stringWithFormat:"********************Documents/image%#.jpg",aNumber];
I want to get "Documents/image%#.jpg" out of the string?
What can I do? I want to use "substringFromIndex" but I don't know the index.
You can use rangeOfString to find the index of "Documents...".
NSString class reference
And then use that with 'substringFromIndex' to get the substring you want.
For example:
[astring substringFromIndex:[aString rangeOfString:#"Documents"].location]
You should add error checking to make sure that the range returned by the 'rangeOfString' method is good.

iPhone CoreData join

this is my core data model:
I'm trying to get all LanguageEntries from a database for a given category.categoryName and languageset.languageSetName e.g.
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"LanguageEntry" inManagedObjectContext:del.managedObjectContext];
[fetchRequest setEntity:entity];
NSString* predicateString = [NSString stringWithFormat:#"Category.categoryName = %# AND LanguageSet.languageSetName = %#",
#"Food", #"English####Spanish"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:predicateString];
NSError *error = nil;
NSArray* objects = [del.managedObjectContext executeFetchRequest:fetchRequest error:&error];
This always returns 0 objects. If I set the predicate string to match on one relationship (e.g. Category.categoryName = Food or languageSet.languageSetName = English####Spanish) it will return data.
This is baffling, can anyone shed some light?
->Ken
Your can't think of Core Data as SQL. There is no such thing as a "join" in Core Data. In this case, your trying to find the intersection of two sets of objects. Close to a join logically but not in the implementation details. And the programming devil is always in the details.
Try using the logical equal "==" like so:
#"Category.categoryName == %# AND LanguageSet.languageSetName == %#"
I believe that will solve your problem.
The data model has a predicate editor hidden way in it. It can help you set up predicates even if you don't embed them in fetches in model itself. Just select an entity, in your case "LanguageEntity", then add a Fetch Request. The edit predicate will appear and you can use the dialog to create the predicate. It will display a textual version of the predicate that you can copy into your code.
The predicate properly wasn't created correctly, you must pass the parameters to predicateWithFormat. It should have been:
fetchRequest.predicate = [NSPredicate predicateWithFormat:#"Category.categoryName = %# AND LanguageSet.languageSetName = %#",
#"Food",
#"English####Spanish"];
In case you were wondering what that does is it puts quotes around the string parameters automatically which are required when using a string in a predicate. When you created the query using NSString's format method that did not put the required quotes in.

Resources