problem with unicode in NSString - iphone

i have string like: "YouTube - ‪VID 0001‬‏" and i want the string like : YouTube - VID 0001 that means i want to remove the unicodes.
is there a way to remove unicodes like ‪ in iphone apps, if so please help me.

Does this similar question and solution solve your problem?
I'd also recommend heeding the advice given in the comments on the linked question and making sure that you have a valid reason for not wanting unicode.

Try this if you just want to remove the quotes (")
NSString *urString = #"\"YouTube - VID 0001‏\"";
NSString *formattedString = [urString stringByReplacingOccurrencesOfString:#"\"" withString:#""];

Related

how can i query google places for country specific cuisine?

i am trying to accomplish country specific Cuisine places, means like mexican food, spanish food, Thai food, indian food, via google places API on an iPhone app.
How can i do that?? As i see supported types, there is only food, restaurant, etc in it. besides whenever i use this query..
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%#&type=restaurant&query=%#&sensor=true&key=%#", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:#"%i", currenDist], googleType, kGOOGLE_API_KEY];
i am getting few results only, whenever i try to add more types like food|restaurant|establishment like below query my app crashes.
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%#&type=food|restaurant|establishment&query=%#&sensor=true&key=%#", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:#"%i", currenDist], googleType, kGOOGLE_API_KEY];
As i told my app crashes with this error may be i am doing something wrong with url.
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
* First throw call stack: (0x13be022 0x200fcd6 0x1366a48 0x13669b9 0xbadfad 0x2dd2 0x13bfe42 0xaa09df 0x139294f 0x12f5b43 0x12f5424
0x12f4d84 0x12f4c9b 0x15d47d8 0x15d488a 0x17a626 0x236d 0x22d5)
terminate called throwing an exception(lldb)
Please can someone guide me with this?? i hope i cleared all my points.
Thanks & regards,
Malhaar
It appears you are trying to use the Places API Textsearch as you are using the query parameter. If this is correct make sure you are hitting the correct backend:
maps.googleapis.com/maps/api/place/textsearch/json
Also make sure you are using the correct parameter to specify types:
types=food|restaurant|establishment
I would recommend reading the documentation thoroughly and and performing a few test html requests through your browser to make sure you have the right request before plugging it into your application.
Although this is almost 7 months old, and I'm sure you've either solved it or given up on it by now, I have just had the same issue and it took some resolving. I thought I'd put the answer here to help anyone else with a similar error.
The problem I found was to do with the URL encoding not liking the | character.
The way around it I found was to:
// Create an NSString with the URL which includes the '|' characters.
NSString * urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.189781,-114.488907&radius=6918&types=food|bar&keyword=&sensor=true&key=YOUR KEY HERE";
// Encode the URL
NSString* encodedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
// Covert to a NSURL
NSURL *googleRequestURL=[NSURL URLWithString:encodedURLString];
And then use the googleRequestURL as the URL.
Hopefully that will solve it for you!

Strange BAD_EXC_ACCESS when declaring a string

Okay, all I am doing is setting an NSString to a value with this code:
NSString *stringURL = [NSString stringWithFormat:#"http://api.themoviedb.org/3/movie/%#/trailers?api_key=1523229ded5824dab8bb7840782db266",searchID];
This is a string that I then turning into a URL for querying the TMDB database. This line of code gives me a BAD_EXC_ACCESS and it is blowing my mind because using this sort of NSString construction is something I have done thousands of times without a problem.
The one other thing to note is that this line is being executed right after another query call is made. The weird thing is that call makes the stringURL the same way, yet it works fine.
Any help would be appreciated...
You need to use %i to log an NSInteger, not %#
You need to use the following
NSString *stringURL = [NSString stringWithFormat:#"http://api.themoviedb.org/3/movie/%d/trailers?api_key=1523229ded5824dab8bb7840782db266",searchID];
Because searchID has NSInteger type and you are using "%#"
If it's an NSInteger you need to use %ld or you will got a warning, you can also use %d and explicitly cast to int via (int)searchID

objective-c concatenate NSString

I have problems to concatenate NSString.
Each time I pushed a button I want that something ("aux") is added to my string ("myString"). so:
NSString *aux = [NSString stringWithFormat: #"%d", buttonIndex];
myString=[NSString stringWithFormat:#"%#/%#",posTargetaText,aux];
aux = nil;
The first time i pushed the button it works good but the second it doesn't work.
Some help please?
So you can certainly use stringWithFormat, but why don't you use stringByAppendingString instead, since that's exactly what you want to do?
NSString *newString = [firstString stringByAppendingString:secondString];
You really don't need to use a mutable string unless you have a compelling reason to.
Not sure what exactly you want to do. But as per your code aux will have new buttonIndex value each time and You will have always new mystring when ever you tap button.
If you want to append string always in myString that you need to do like this.
myString=[NSString stringWithFormat:#"%#%#/%#",myString,posTargetaText,aux];
You suppose to add previous value of myString as well in new myString string ?
Not sure this is what you want or something different. Please explain in detail if this is not.
If you wanna concatenate two strings use NSMutablestring and method appendstring instead of NSString.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html
You need to use NSMutableString.

Searching a word in the NSString

I am getting a request data and putting it in NSString. after that, I am getting the following string,
_VIEWSTATE=%2FwEPDwULLTE1MjI5NTA5MzBkZBfGgdOVhk6K8WsSgq64ngCpAncw&_EVENTVALIDATION=%2FwEWBgKdhbkbAvOQn7cGApKGyNkMAsKL2t4DApSbgLYHArS6otoP2nSQkmm0E6zJe2u91W5ntimqJ18%3D&x-rim-queue-id=MyOfflineQueue&form_id=723&txt2=siddhesh.b.chavan%40gmail.com&btnSubmit=Submit&x-rim1-request1-title=SignatureShouldBeDoneHere&x-rim-request-title=iPhone3+4%2F6%2F2011+3%3A57%3A21+AM
The thing I have to ask is, I want to get the "form_id" from this string which is "723".
so, How do I get that??
I want to get the form_ID for a request everytime. So, kindly help me out of this.
Thanking you.
These options are more intended for URL arguments parsing, which you are trying to do.
1. Range search : for a word, a character
most efficient but can be fastidious to write... (and read!)
See rangeOfString: and its friends on NSString documentation
2. Split
quick and elegant to write, but not so efficient
Since it is a URL argument style string, it is easily parsable by splitting on & and = witch can be done easily using componentsSeparatedByString: or componentsSeparatedByCharactersInSet:
3. Regular expressions
clean code, powerful
Regular expressions are imho the best choice to manipulate text, but they can be harder to use/learn than previous solutions. To use regular expressions I suggest two options:
iPhone OS >= 3.2 has regular expressions :
NSString rangeOfString:options:NSRegularExpressionSearch
But this is close to option 1.
RegexKitLit, with provides an excellent regular expression engine on OSX/iOS would provide, imho, the best and most powerful solution to your problem (and many others!!!)...
4. Other Kits/API/SDK
the missing api/toolkit/sdk? don't write code thousands people already wrote...
I wished that NSURL would support URL arguments, for parsing and build urls... but it does not.
I don't know a good URL parsing/toolbox library that offers such URL Arguments manipulation tools (Google Toolbox does not provide such URL arguments tools except for escaping which is already really useful) but I'm sure that exists! And a good library, with tested and reliable code would be for sure your best solution...
5. Others... there are many
I forgot to mention NSScanner which I never really looked into (bad me)
More generally, Apple documentation on this topic is interesting.
if you would like to try this: Let me know if it works.
NSInteger formId;
//separate the whole string first by "&" characters
NSArray *array = [myString componentsSeparatedByString:#"&"];
for (NSString *queryString in array)
{
//for every separated string, look if you have the "form_id" key
NSRange range = [queryString rangeOfString:#"form_id=" options:NSLiteralSearch];
if (range.location != NSNotFound)
{
NSLog(#"form_id query string does exist");
//form_id= has 7 characters, pick the character which comes after the "=" sign.
NSString *value = [queryString substringFromIndex:8];
formId = [value integerValue];
}
}
That solution is assuming that you only have 1 "form_id".
Try this :
[myString substringToIndex:index];
[myString substringFromIndex:index];
[myString substringWithRange:range];
Or
if ( [yourString rangeOfString:#"form_id"].location == NSNotFound )
{
NSLog(#"form_id not found");
}
Use 'rangeOfString:options:range:' start by searching the entire string for "form_id=" and then search from where that was found to for an "&". You will need to handle the case where the ampersand isnt found (when form_id is the last in the list).
You can use NSScanner object too. Like-
NSScanner *scanner = [NSScanner scannerWithString:reqData];
[scanner scanUpToString:#"form_id" intoString:nil];
[scanner scanUpToString:#"&" intoString:&strObj];
valueStr = [strObj substringFromIndex:1];

How do I localize a string with formatting placeholders?

How do I localize a string that has placeholders in it with NSLocalizedString?
For example:
[NSString stringWithFormat:#"You can afford %i at %#%li.",[kCash integerValue]/self.price, kYen, self.price]
How do I localize this? Do I do break up the strings into multiple localized strings? How then do I deal with varying sentence structure and grammar?
NSLocalizedString won't alter your placeholders, so stringWithFormat can use them as normal. In your example, using numbered placeholders is probably a good idea -
[NSString stringWithFormat:#"You can afford %1$i at %2$#%3$li.",
[kCash integerValue]/self.price, kYen, self.price]
More info here:
Is there a way to specify argument position/index in NSString stringWithFormat?
Have the localized strings include the placeholders. That's pretty much the only proper way to do it as otherwise, as you mentioned, you couldn't take varying word order into account.
Something along these lines:
[NSString stringWithFormat:NSLocalizedString(#"Foo %i", #"Foo %i"), 123]
Another approach to this is in your localized file, you can have:
key = "String with %# placeholder";
and in your implementation:
[NSString stringWithFormat: NSLocalizedString(#"key", ""), #"string replacing placeholder"];
You can do this with any number of arguments, they just need to be consistent across your localization files.
This way seems more efficient:
[NSString stringWithFormat:#"%# %#", NSLocalizedString(#"Key", #"Comment for localised string"), value];