ios i want localize by stringWithFormat - ios

i want localize stringWithFormat by this:
NSString *string = [NSString stringWithFormat:#"Login %d in",2013];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#string, nil)
message:#"Message"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"sure", nil];
[alertView show];
also i write this in Localizable.string:
"Login %d in" = "LOGIN %d IN";
and it doesn't work.can you help me?Thank...

You have to localize the format string itself. Doing anything else doesn't make sense because the string can be practically anything when it's formatted (that's the purpose of format strings, after all). Just out of curiosity, haven't you seen things like
"%d seconds remaining" = "%d secondes restants";
"Hello, %#!" = "Bonjour, %# !";
in the Localizable.strings file of applications you used yet?
NSString *localizedFmt = NSLocalizedString(#"Login %d in", nil);
UIAlertView *av = [[UIAlertView alloc]
initWithTitle:[NSString stringWithFormat:localizedFmt, 2013],
// etc...
];
The Security Freak's Notice: although this is the most common and easiest approach to localize formatted strings, it's not entirely safe.
An attacker can change the localized format string in the aforementioned Localizable.strings file of your app to something bogus, for example, to a string that contains more conversion specifiers than stringWithFormat: has arguments (or even mismatching specifiers - treating integers as pointers, anyone?), and then a stack smashing-based attack can be carried out against your application - so beware of hackers.

Related

Fetching and comparing CFBundleVersion from plist

I'm trying to compare CFBundleVersion key of 2 Apps inside com.apple.mobile.installation.plist which include the info of every installed application on iPhone
NSString *appBundleID =#"net.someapp.app";
NSString *appBundleID2=#"net.someapp.app2";
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:
#"/var/mobile/Library/Caches/com.apple.mobile.installation.plist"];
NSDictionary *User = [dict valueForKey:#"User"];
//get first app version
NSDictionary *bundleID = [User valueForKey:appBundleID];
NSString *appVersion = [bundleID valueForKey:#"CFBundleVersion"];
//get second app version
NSDictionary *bundleID2 = [User valueForKey:appBundleID2];
NSString *appVer2 = [bundleID2 valueForKey:#"CFBundleVersion"];
[dict release];
if ([appVersion isEqualToString:appVer2]) {
NSString *str1=[NSString stringWithFormat:#"Original Version: %#",appVersion];
NSString *str2=[NSString stringWithFormat:#"2nd Version: %#",appVer2];
NSString *msg=[NSString stringWithFormat:#"%#\n%#",str1,str2];
UIAlertView* alertView = [[UIAlertView alloc]
initWithTitle:#"Same Versions!" message:msg delegate:nil
cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
}
else {
NSString *str1=[NSString stringWithFormat:#"Original Version: %#",appVersion];
NSString *str2=[NSString stringWithFormat:#"2nd Version: %#",appVer2];
NSString *msg=[NSString stringWithFormat:#"%#\n%#",str1,str2];
UIAlertView* alertView = [[UIAlertView alloc]
initWithTitle:#"Different Versions!" message:msg delegate:nil
cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
}
The version of both apps is currently set to 2.11.8
I am getting the following wrong result:
If i set the NSString manually:
NSString *appVersion =#"2.11.8";
NSString *appVer2 =#"2.11.8";
i get the correct desired result:
I also tried other ways to compare the strings but the result was always the same, so i guess the problem is with fetching the values of the keys?
Any help is appreciated
I am so used to ARC that I am not 100% sure about the MRC rules anymore. But I assume
that you either have to retain the values appVersion and appVer2 from the dictionary,
or alternatively, postpone the [dict release] until after the values are no longer needed.
Since you don't own the values fetched from the dictionary, they become invalid if the
dictionary is released.
(This would not be a problem if you compile with ARC!)
Remark: The designated method to get a value from a dictionary is objectForKey:.
valueForKey: works also in many cases, but can be different. It should only be used
for Key-Value Coding magic.

how to reveal all string padding from txt file in Xcode?

I making a word game so Ive set all the word in a .txt file, and i've added a show 1st letter button:
Code:
if ([buttonTitle isEqualToString:#"reveal"]) {
UIAlertView *alertuser;
NSString *prla = [NSString stringWithFormat:#"%#", [lst_word objectAtIndex:nCurrentWord]];
NSString *finished = [[prla substringToIndex:1] stringByPaddingToLength:prla.length withString: #"_" startingAtIndex:0];
alertuser = [[UIAlertView alloc]initWithTitle:#"This is the first letter:" message:finish delegate:self cancelButtonTitle:#"Thanks" otherButtonTitles:nil, nil];
[alerta show];
How would I make it show and reveal all the letters from the word in the .txt file?
NSString *finishText = [[prlt substringToIndex:1] stringByPaddingToLength:prlt.length withString: #"_" startingAtIndex:0];
alerta = [[UIAlertView alloc]initWithTitle:#"First Letter is:" message:finishText delegate:self cancelButtonTitle:#"Thanks" otherButtonTitles:nil, nil];
[alerta show];
I need this reversed to allow the full answer from the txt file!
substringWithRange: may be the function you are looking for.
The sample code below will print word by word of a string
NSString *sampleString = #"abcedefgh";
for(int i = 0; i < [sampleString length]; i++)
{
NSLog(#"word = %#", [sampleString substringWithRange:NSMakeRange(i, 1)])
}
EDIT:
In place of
NSString *finishedText = [[prlt substringToIndex:1] stringByPaddingToLength:prlt.length withString: #"_" startingAtIndex:0];
use below code, this will solve your issue
NSString *finishedText= [prlt substringWithRange:NSMakeRange(0, prla.length)];

Trying to retrieve only NSStrings excluding whitespace from UITextField in iOS

I am working on an iOS application where a user is required to enter their first and last name into a UITextField. Now, I want my UITextField to be able to check to see that the user has entered:
(1) two names and (2) each name is a minimum of two characters.
My problem is that when I retrieve the name(s) from the text field, I am unable to distinguish between an empty NSString (i.e. " "), and an NSString that is composed of characters. Because of this, when I check to see what the length of the shortest word that has been entered is, the empty spaces are always counted as being valid strings, which I don't want. So in other words, if a user enters:
" larry bird "
I need the string count to be two, and the length of the shortest word to be 4 (thus passing both tests). So I need the leading, trailing whitespaces as well as the whitespaces in between to be removed, but keep the NSString objects that are valid to be distinct.
Here is the code that I have thus far:
NSString *name = [textField text];
NSArray *trimmedArray = [name componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableArray *trimmedNames = [NSMutableArray array];
for (NSString *cleanString in trimmedArray) {
NSString *trimmedString = [cleanString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#"The trimmed name is: %#", trimmedString);
[trimmedNames addObject:trimmedString];
}
self.alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please Enter Full First and Last Name" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
if(trimmedNames.count > 1) {
NSNumber *lengthOfShortestWord = [trimmedNames valueForKeyPath:#"#min.length"];//here is where I am counting the characters in the smallest string entered the textfield
if (lengthOfShortestWord.intValue > 1) {
[textField resignFirstResponder];
}
else if (lengthOfShortestWord.intValue <= 1) {
[self.alert show];
}
}
else if (trimmedNames.count < 2) {
[self.alert show];
}
Does anyone see what it is that I am doing wrong?
Trim the white space from the initial string from the text field.
NSString *name = [[textField text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Now you don't need to trim each individual string.

How to get a float value into a UIAlertView [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Im trying to find the correct way to get a float value into a UIAlertView. My float value is only used to check another value rather than passed to string somewhere.
I suppose I could set my float value to a label and set it to hidden and pass that to my alert, but im sure this cant be the proper way to do this, some advice would be appreciated
float x = ([_continuityRingFinalR1.text floatValue]); /stringWithFormat:#"%.1f", x * y]]
float y = (1.67);
if ([_continuityRingFinalRn.text floatValue] > x * y ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Advisory Warning"
message:[NSString stringWithFormat: #"Based on your value of %# this value may not be acceptable. %# would be acceptable",_continuityRingFinalR1.text, ]///<<< my float value here
delegate:self cancelButtonTitle: #"Ignore" otherButtonTitles: #"Retest", nil];
[alert show];
}
%f is used in an NSString for a float/double instead of %#
float x = ([_continuityRingFinalR1.text floatValue]); /stringWithFormat:#"%.1f", x * y]]
float y = (1.67);
float example;
if ([_continuityRingFinalRn.text floatValue] > x * y ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Advisory Warning"
message:[NSString stringWithFormat: #"Based on your value of %# this value may not be acceptable. %f would be acceptable",_continuityRingFinalR1.text, example];
delegate:self cancelButtonTitle: #"Ignore" otherButtonTitles: #"Retest", nil];
[alert show];
}
Here's a helpful link
I think you want this
message:[NSString stringWithFormat: #"Based on your value of %# this value may not be acceptable. %0.2f would be acceptable",_continuityRingFinalR1.text, x*y ];
//Passing second argument as x*y

How to read seperate fields in a csv file and compare with user input?

I am building an app where use can upload a csv file that contains First Name, Last Name and email in 3 seperate columns. User interface will have 3 text fields and a button. When user enters the first name or last name or email, and click search button, the whole documenent must be searched and display an alert saying that the record was found in the file. This is the function that I am using, but it only reads the first row and first column. Please help
- (void) SearchStudent
{
NSArray *DocumentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocumentDirectory = [DocumentPath objectAtIndex:0];
NSString *FullPath = [DocumentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"example.csv"]];
NSString * pstrCSVFile= [NSString stringWithContentsOfFile:FullPath encoding:NSASCIIStringEncoding error:NULL];
NSArray * paRowsOfCSVFile= [pstrCSVFile componentsSeparatedByString:#"\n"];
NSArray *paColumnsOfRow;
NSString *pstrFirstColumn;
for(NSString * pstrRow in paRowsOfCSVFile)
{
paColumnsOfRow= [pstrRow componentsSeparatedByString:#","];
pstrFirstColumn= [paColumnsOfRow objectAtIndex:0];
if([pstrFirstColumn localizedCaseInsensitiveCompare:GWIDText.text] == NSOrderedSame)
{
UIAlertView *alertingFileName = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Found" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertingFileName show];
break;
}
else
{
UIAlertView *alertingFileName1 = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Not Found" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertingFileName1 show];
break;
}
}
}
It looks like your else statement is breaking out of the for loop if the first name doesn't match. You'll want to remove that and perhaps add a variable to track if you've found a match. Only set it if you match the name. After your for loop, check the variable to see if there was a match. If not, then show your UIAlertView for "Not Found".
Couple of hints:
You are checking for one column only.
if([pstrFirstColumn localizedCaseInsensitiveCompare:GWIDText.text] == NSOrderedSame)
Do the same with the other two columns. Then you will solve your first problem of only checking one column.
Looks like the line terminator is not correct. Depending on which platform the csv file was created, it might have different line terminator. I would suggest taking a text editor like notepad++ for instance and viewing hidden code to find out your line terminator. Then use the terminator to split rows. Make sure you are getting more than 1 row (just output the rows variable].

Resources