objective-c concatenate NSString - iphone

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.

Related

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

problem with unicode in NSString

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:#""];

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];

Issue posting variable with iPhone SDK

I am encountering an issue while posting a variable with Xcode:
While running this code the app crashes while posting the variable to a webservice:
NSArray *array = [stringFromFile componentsSeparatedByString: #","];
NSString *time = [array objectAtIndex:1];
UTCorLocal = time;
NSLog(#"%#", UTCorLocal);
UTCorLocal variable is declared earlier in the code. The NSLog outputs the correct string, but when I try to use it further along in the code it crashes.
When I give the variable a static value like this:
UTCorLocal = #"UTC";
It all runs like it should do!
Could anybody please help, it's driving me crazy!
Thanks a lot,
Ron
It's probably released somewhere along the way, I would guess, without knowing the rest of your code. Try copying or retaining 'time' and see what happens.