NSLocalizedString: confusion with short string - substring - objective-c

In my code I use a lot of localized strings and all works great, except with short strings.
Example
NSString* text = [NSString stringWithFormat:NSLocalizedString(#"%# at %#", nil), dateStart, timeStart];
In file localizable.string I have:
"%# at %#" = "<translation>"
"attending %#" = "<translation>"
In my opinion, compiler fails when try to get the right string.. how can I achieve this?

The left side is the key for the copy on the right side.
It's not the key that should have the placeholders in it, as the string is exchanged at runtime by the string from the locale.strings.
In other words: after localization you are missing the %#
To achieve what you want, you need the placeholders to be in the translated string:
"SOME_KEY" = "%# <localized> %#"

Related

Format String is not string literal

How do i make this a more secure method? As this relies upon an array to distribute the information that can be appended outside of an app update.
methods = [NSArray arrayWithObjects:#"martingale", #"reverse_martingale", nil];
methodString = [NSString stringWithFormat:methods[0]];
<- Format string is not a string literal (potentially insecure)
Simply dump the use of stringWithFormat given:
Those aren't format strings.
You aren't formatting anything.
Use this instead:
methodString = methods[0];
Do you realise what you are doing? methods [0] is used as format string, as they are used for printf or NSLog. If you change it from #"martingale" to #"martingale %s" then it will likely crash. If you didn't want to use a format string, don't call stringWithFormat.

how do i do: label.text = (#"some string" + _label2.text);

I am completely new to Objective-C and although i have some experience with java and C#, I just can't get this to work.
My code is:
- (IBAction)btnClickMe_Clicked:(id)sender {
Label_1.text = (#"some string" + _Label_2.text);
}
I am also curious as to why Label_1 does not need an underscore infront of it, like _Label_2 does?
To concatenate strings, you use
Label_1.text = [#"Some string" stringByAppendingString:_Label_2.text];
You can use %# to append your additionnals strings with stringWithFormat
Label_1.text = [NSString stringWithFormat: #"Some string %#", _Label_2.text];
More example : Apple - Formatting String Objects
NSString provides a vast variety of methods for string manipulations. Amongst them are several ways for conatination.
You should get familiar with the factory method stringWithFormat. It is one of the most powerful and especially good at a bit more complex requirements.
In your case:
Label_1.text = [NSString stringWithFormat:#"Some string%#", _Label_2.text);
or
Label_1.text = [NSString stringWithFormat:#"%#g%#", #"Some string", _Label_2.text);
The format string corresponds to the usual standard c printf format string plus the %# tag which is replaced by any objects description value. So you could have an NSNumber there or even an NSArray or so. However, the description of NSArray, NSDictionary, NSSet etc. may not really be useful for production but come quite handy for debugging. NSLog() uses the same format.

How to Use An Escape URL String With Formatting in Objective-C

I need to gather data from a website based on the user's input.
searchString is the user inputted value, such as "search this string".
NSString *withoutSpaces = [searchString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Here, I need to replace spaces with %20
Next, I need to put the new string without spaces (replaced with %20) into another string.
NSString *unescapedSearchString = [NSString stringWithFormat:
#"website.com/query?=%22%#%22", withoutSpaces];
The site I need is not really "website.com", but that's just an example. I also need the %22 to remain at the beginning and end.
As you can see, I need the %# to format the new withoutSpaces user input into the website URL.
I did a search and found examples but I could not find any with formatting such as in my case using %#.
What's the best way to "escape" the characters and keep my formatted string? Currently, when I try to access data from the website, it comes back as null. However, when I try a string without the %# formatting and an actual value, I successfully retrieve the data from a website.
Any help is greatly appreciated.
You should do things this way:
NSString *searchString = ... // the raw search string with spaces and all
NSString *quoted = [NSString stringWithFormat:#"\"%#\"", searchString];
NSString *escaped = [quoted stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"website.com?query=%#&value=all", escaped];
BTW - the URL seems a little off. There should be a variable name before the = and after the ?.

Keychain kSecValueData with weird not-showing characters at last

I've been searching a solution for this problem, but it seems I'm the only one on the Internet who has encountered a problem like this.
I'm using the keychain wrapper class provided by Apple to store the user and password as it should be stored. When I want to get the user value back, is as easy as doing:
NSString *user = [keychain objectForKey:(id)kSecAttrAccount];
Retriveing the password should be as straightforward as the username:
NSString *pass = [keychain objectForKey:(id)kSecValueData];
But after that, trying to print them with an NSLog, nothing is shown on console AFTER the pass. For example:
NSLog(#"user: <%#>, pass: <%#>, something after the pass", user, pass);
The output of this NSLog is:
user: <123456>, pass: <5433
Invoking [pass length] gives me always a number greater than the actual length of the pass (in this example, 10, when I would say its length is actually 4).
I have no idea of what's going on. I made a workaround to patch this problem while I try to find a proper solution (looking every character's integer value and allowing only the ones which are numbers, letters and some symbols).
Thank you in advance!
The problem here is that you are trying to store a CFDataRef object as an NSString object, and then print it is a string using %#. It is a data object, not a string. If you'd like to see it as a string, you must first convert it into a string. Try something like the code below to convert it into a string before you try to log it:
NSData *passData = [keychain objectForKey:(id)kSecValueData];
NSString *pass = [[NSString alloc] initWithBytes:[passData bytes] length:[passData length] encoding:NSUTF8StringEncoding];
kSecValueData is of type: typedef const void * CFTypeRef;
You shouldn't typecast it to a NSString.
Try directly posting it into the NSLog like this.
NSLog(#"user <%#> pass <%#>", [keychain objectForKey:(id)kSecAttrAccount], [keychain objectForKey:(id)kSecValueData]);
Goodluck!

objective C NSString stringwithformat of a URL

i need my application to send an HTTP request to my server, this is the link, but for some reason, when i create an NSString stringwithformat not all of the string is copied into the string,
this is my URL:
http://192.168.50.204:8080/webapi/originate?sofia/internal/408%25192.168.50.204%20'set:effective_caller_id_number=722772408,bridge:sofia/gateway/012smile/<PHONENUMBER>#212.199.220.21'%20inline%200545890183%200545890183
if i put it in my browser it is working fine.
and this is the code:
self.feedURLString = [NSString stringWithFormat:#"http://192.168.50.204:8080/webapi/originate?sofia/internal/%#%25192.168.50.204%20'set:effective_caller_id_number=722772%#,bridge:sofia/gateway/012smile/%##212.199.220.21'%20inline%20%#%20%#",extention,extention,PhoneNumber,PhoneNumber, PhoneNumber];
keep in mind that there are some %20 and %25 in the URL, maybe it causes the problem...
the string i get in the NSLog is:
feedURLString = http://192.168.50.204:8080/webapi/originate?sofia/internal/408220'set:effective_caller_id_number=722772408,bridge:sofia/gateway/012smile/0545890183#212.199.220.21' 93610576nline2#2#
remove the %20 and %25 from the string
then use the stringByAddingPercentEscapesUsingEncoding command from NSString
string = [sURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
You're right, the %20 are almost certainly causing a problem.
Any places in the string you want an actual % symbol in the result, you should be writing %%.
Another option that some people use is to leave the %s as they are, but use stringByReplacingOccurrencesOfString:withString: to do the substitiation.
eg:
NSString *str = #"http://{HOST}/{USER}/blah";
str = [str stringByReplacingOccurrencesOfString:"{HOST}" withString:hostname];
str = [str stringByReplacingOccurrencesOfString:"{USER}" withString:username];
Try writing the url with the normal characters instead of %20 and %25, then add in your variables, and when you have the complete url string use
[feedURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEndocing];
Edit: sorry for double, I guess the anwser was obvious :)