stringByAppendingPathComponent adds a '/' character to the text - ios

I have the following code:
NSString *message;
if (some_condition) {
message = #"String 1. ";
} else {
message = #"String 2. ";
}
message = [message stringByAppendingPathComponent:#"Bla bla bla."];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Alert"
message:message
preferredStyle:UIAlertControllerStyleAlert];
The text that I see when the alert pops up has an extra '/' character where the string were appended:
Where did it come from and how do I remove it?

You are using the wrong method there.
Instead of :
message = [message stringByAppendingPathComponent:#"Bla bla bla."];
Use:
message = [message stringByAppendingString:#"Bla bla bla."];
When you use stringByAppendingPathComponent: it will return a new string made by appending the provided string to the receiver, preceded if necessary by a path separator.
Reference : NSString Class Reference

Related

Data Not inserted In Sqlite objective c

I want to insert some values in a table of database.but it is showing (NULL) on insertion
I am using This code
http://pastie.org/10929618
I have used this Link for Reference
http://www.tutorialspoint.com/ios/ios_sqlite_database.htm
//Insert Method
-(void)SaveAction{
BOOL success = NO;
NSString *alertString = #"Data Insertion failed";
success = [[DBManager getSharedInstance]saveData:#"100" username:#"byname" type:#"one2one" user_to:#"144" user_from:#"145" timestamp:#"42015"];
if (success == NO) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:
alertString message:nil
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
You are missing the column value.
// you are passing the value use below method but dnt have "message1" column value
success = [[DBManager getSharedInstance]saveData:#"100" username:#"byname" type:#"one2one" user_to:#"144" user_from:#"145" timestamp:#"42015"];
// compare the below query
NSString *insertSQL = [NSString stringWithFormat:#"insert into chatDetail (chat_id,username, type, message1,user_to,user_from,timestamp) values (\"%ld\",\"%#\", \"%#\", \"%#\",\"%#\",\"%#\")",(long)[chat_id integerValue], username, type, user_to,user_from,timestamp];

Confirm email address exists and is not already taken, using Parse and email Regex

I am at the part of my register process where I would like to confirm if the email given by the user exists and is not already taken.
So far, I have the regex set and customized to how I want it (I am limiting the user to a student email address).
- (BOOL)validateEmail:(NSString *)emailStr {
NSString *emailRegex = #"[A-Za-z]+\\.[A-Za-z]+#[ace]+\\.[tamut]+\\.[edu]";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
[self checkEmailAndDisplayAlert];
return [emailTest evaluateWithObject:emailStr];
}
I throw an alert if the email is not acceptable.
- (void)checkEmailAndDisplayAlert {
if(![self validateEmail:[_email text]]) {
[self.email becomeFirstResponder];
// user entered invalid email address
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Enter a valid email address." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
} else {
// user entered valid email address
[self registerNewUser];
}
}
If I enter an email that does not meet the regex requirements, the proper alert is not shown, and I am still allowed to pass through the registration as a new user. What am I doing wrong?
Please don't hold back on any feedback, I am new to programming and this is something I need to know, no excuses.
In your validateEmail method, you call checkEmailAndDisplayAlert ([self checkEmailAndDisplayAlert];), and it also calls validateEmail. I'd delete [self checkEmailAndDisplayAlert]; to avoid endless loops.
Also, try changing the regex to
NSString *emailRegex = #"^[A-Za-z]+\\.[A-Za-z]+#ace\\.tamut\\.edu$";
The function to check validity could also look like:
- (BOOL)validateEmail:(NSString *)emailStr
{
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"^[a-z]+\\.[a-z]+#ace\\.tamut\\.edu$" options:NSRegularExpressionCaseInsensitive error:&error];
NSAssert(regex, #"Unable to create regular expression");
NSRange txtRng = NSMakeRange(0, emailStr.length);
NSRange matchRng = [regex rangeOfFirstMatchInString:emailStr options:NSMatchingReportProgress range:txtRng];
BOOL validated = NO;
if (matchRng.location != NSNotFound)
validated = YES; // Match found
return validated;
}
^ and $ are added at the beginning anf end of the regex pattern to enable whole string search.

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.

ios i want localize by stringWithFormat

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.

Resources