How do i compare a website result with a predicted result.
#"document.getElementsByTagName('body')[0].outerHTML"
is predicted to contain:
<body>OK</body>
But i always get an error saying that they don't match. I used this code below to compare them:
if (webresult == cmp){
then it shows an alert saying success. Or in else it'll say error. It always goes to else. Heres the code block, Please help.
- (IBAction)displayresult:(id)sender {
webresult = [webview2 stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].outerHTML"];
NSString *cmp = [[NSString alloc] initWithFormat:#"<body>OK</body>"];
if (webresult == cmp) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Logged in" message:#"Logged in, Proceeding to the game" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:webresult delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
I assume that webresult is an NSString. If that is the case, then you want to use:
if ([webresult isEqualToString:cmp]) {
instead of:
if (webresult == cmp) {
as the above method checks if the strings are equal character by character, whereas the bottom method checks if the two strings are the same pointer.
if (webresult == cmp)
Here, == checks whether webresult, cmp are pointing to the same reference or not. You should instead compare value of the object by using NSString::isEqualToString.
if ( [ cmp isEqualToString:webresult ]) {
// ..
}else {
// ..
}
Note that isEqualToString is a good option because it returns boolean value.
We cannot comapre the strings with ==
We have to use isEqualToString:
if([str1 isEqualToString:str2])
{
}
else
{
}
When comparing strings you need to use isEqualToString:
if ([cmp isEqualToString:webresult]) {
...
} else {
...
}
for Swift 4.0:
if str1==str2 {
//both string are equal
}
else {
//do something expression not true
}
for Objective-C:
if ([str1 isEqualToString:str2]) {
//both string are equal
}
else {
//do something expression not true
}
Related
I'm trying to build a login page for an app. Something went wrong when testing the login condition.
There are two textfields, username and password. When the Login button is clicked, alert shows when
1) either fields are empty
2) both filled.
The code reflects this below. But the result came out no matter that the textfield is empty or not, it only showed as textfields were filled ( only the else part run). So I assume there was something wrong with the if condition. But when I checked the if condition in other view, it worked.
Can anyone help?
if ([_pLoginIDField.text isEqualToString:#""] || [_pPasswordField.text isEqualToString:#""]) {
UIAlertView *error = [[UIAlertView alloc] initWithTitle:#"Yeah !" message:#"must complete all fields" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[error show];
}
else {
UIAlertView *error2 = [[UIAlertView alloc] initWithTitle:#"Oooops !" message:#"hhhhhhhhhh" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[error2 show];
}
Try this:
if (_pLoginIDField.text.length > 0 || _pPasswordField.text.length > 0)
NSString *text = [NSString stringWithString: nameUITextField.text];
if(text.length == 0){
}
You may try this,
you can count how many characters are in string, it is indirectly checking to null.
NSString *inputString = [NSString stringWithString: nameUITextField.text];
if(inputString.charachters.count == 0){
// inputString is Blank
// Do your stuff
}else{
//inputString is not blank, Do other Stuff.
}
the above condition satisfy if inputString is blank otherwise it will enter into else block.
I'm building an iOS app and I need to provide a forgot password function. I have a db that stores registered users and their details(userArray). I have searched by email inputed so, how do I do a UIAlert that shows the found password??? below is code I tried but my if condition is wrong.
[self.theuser valueForKey:#"password"] is the retrieved password from the database and its a string.
- (IBAction)ForgotPassword:(id)sender {
if ([[self.theuser valueForKey:#"password"] > 0 ]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Success"
message: #"Your Password is:"
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
You need to check that the length of the string is higher than 0, because using this:
if ([self.theuser valueForKey:#"password"])
will always return true if the value is other than nil, meaning if you server code returns that the value of password is "" (two quote marks with no space in between) then the app will recognize that the value is not nil and display an alert with no text in it
So the final code should look something like this:
- (IBAction)ForgotPassword:(id)sender {
NSString *retrievedPassword = [self.theuser valueForKey:#"password"];
if ( retrievedPassword.length > 0) {
NSString *message = [NSString stringWithFormat:#"Your password is: %#", retrievedPassword];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Success"
message: message
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
}
Change:
#"Your Password is:"
to:
[NSString stringWithFormat:#"Your Password is: %#", [self.theuser valueForKey:#"password"]]
You can also change this:
if ([[self.theuser valueForKey:#"password"] > 0 ]) {
to this:
if ([self.theuser valueForKey:#"password"]) {
If it didn't exist, it would count as "false". Any other value is "true".
I have a calculator that multiplies a value with a certain $ rate. The user can edit the $ rate, and the value is multiplied with the user-entered rate. Here is my code for this:
- (IBAction)edit
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Edit New Amount"
message:#"Enter new rate"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex) {
UITextField *field = [alertView textFieldAtIndex:0];
field.placeholder = #"Enter New Rate";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet];
if ([field.text rangeOfCharacterFromSet:set].location != NSNotFound)
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Only numbers are allowed in this field."delegate:self cancelButtonTitle:#"OK."otherButtonTitles:nil];
[errorAlert show];
rate.text=#"";
}
else
{
rate.text = field.text;
}
}
else
{
//Cancel
}
}
However, when the app closes and is restarted, the old rate is displayed, not the new user-entered rate. How can I save the new user-entered rate, so that it is retained within the app, even when it is restarted?
If you want the value to be retained after app is closed, then you got to store it somewhere in file system. Easiest and convenient way is to use NSUserDefaults:
//Storing in user defaults
[[NSUserDefaults standardUserDefaults] setObject:field.text forKey:RATE_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
//Retrieving (possibly in app start or viewdidload)
self.rate.text = [[NSUserDefaults standardUserDefaults] objectForKey:RATE_KEY];
One note on usage of NSUserDefaults is that it doesn't support storing every object type, it supports NSString,NSArray,NSNumber etc and primitive types. Since you used NSStrings in your code I assumed you can save it as string to defaults.
Have you tried creating a new variable to hold the new input as the user enters the new input, and then setting whatever the new input is to that new variable? It sounds like it is restarting from the last variable that was set in the input.
I am building an application which has a login through a mobile SAAS - Parse.
There are multiple error codes that could be returned from a login request. At the moment run an if statement for each error code and display a relevant alert view like this:
if (error == nil) {
// Something went wrong
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"LoginAlertErrorTitle", #"Login Error Alert View Title") message:NSLocalizedString(#"LoginStandardError", #"Login error message text - standard error") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(#"GlobalOKButtonTitle", #"Global Ok button title"), nil];
[alertView show];
} else if ([error code] == kPFErrorObjectNotFound) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"LoginAlertErrorTitle", #"Login Error Alert View Title") message:NSLocalizedString(#"LoginErrorObjectNotFound", #"Login error message text - object not found") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(#"GlobalOKButtonTitle", #"Global Ok button title"), nil];
[alertView show];
} else if ([error code] == kPFErrorConnectionFailed) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"LoginAlertErrorTitle", #"Login Error Alert View Title") message:NSLocalizedString(#"LoginAlertErrorConnection", #"Login error message text - connection failed") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(#"GlobalOKButtonTitle", #"Global Ok button title"), nil];
[alertView show];
} else {
NSLog(#"A Login error occurred: %i",[error code]);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"LoginAlertErrorTitle", #"Login Error Alert View Title") message:[[error userInfo] objectForKey:#"error"] delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(#"GlobalOKButtonTitle", #"Global Ok button title"), nil];
[alertView show];
}
Is there a more efficient way to do the same with case/switching?
The actual error codes are setup like this:
/*! #abstract 100: The connection to the Parse servers failed. */
extern NSInteger const kPFErrorConnectionFailed;
Which makes me think I can setup this in a case statement. Would this be the correct/best way to approach this? Should it be in a separate method like handleErrorAlert: possibly?
How would I code this switch in the example above?
Whether you use a switch statement or a series of if-else if is really just a matter of taste in this case. Yes, the switch statement is slightly more efficient, but in a case like this, it really doesn't matter (it's not like you call this thousands of times per second). Use what you find more readable.
You might want to refactor your alert view code a little though – you're doing the same thing in all cases with only the error message being different, so there's quite a bit of repeated code. You could refactor it like this:
NSString *errorMessage = nil;
if (error == nil) {
errorMessage = NSLocalizedString(#"LoginStandardError", #"Login error message text - standard error");
} else {
switch ([error code]) {
case kPFErrorObjectNotFound:
errorMessage = NSLocalizedString(#"LoginErrorObjectNotFound", #"Login error message text - object not found");
break;
case kPFErrorConnectionFailed:
errorMessage = NSLocalizedString(#"LoginAlertErrorConnection", #"Login error message text - connection failed");
break;
default:
errorMessage = [[error userInfo] objectForKey:#"error"];
}
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"LoginAlertErrorTitle", #"Login Error Alert View Title")
message:errorMessage
delegate:self
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(#"GlobalOKButtonTitle", #"Global Ok button title"), nil];
[alertView show];
A typedef enum being used on a switch, I think it would be the cleanest way. Something like this:
typedef enum
{
kServerError,
kInternetError,
kUnknowError
} kTypeError;
switch (aTypeError)
{
.
.
.
}
In your specific case, you take care about the message inside the switch... The UIAlertView is a common part. So:
NSString *aTitle = nil;
NSString *aMessage = nil;
switch (aTypeError)
{
case kUnknowError:
{
aTitle = ...;
aMessage = ...;
}
break;
}
UIAlertView *alertView = [UIAlertView alloc] ...
if (!error) {
// Handle error (?).
}
switch ([error code]) {
case kPFErrorObjectNotFound:
// Handle error.
break;
case kPFErrorConnectionFailed:
// Handle error.
break;
default:
// Handle error.
}
This only works if the value returned by -code can be used in a switch test expression. AFAIK, int is supported—I don't know about other types.
I want the user to confirm the password he typed it... so I use two text fields.. But somehow even if both have the same password it seems to think that the 2 strin differ
if (![self.typePTextField.text isEqualToString:self.retypePLabel.text]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error", #"Error") message:NSLocalizedString(#"Passwords do not match \n please retype", #"Passwords do not match \n please retype") delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
self.typePTextField.text = #"";
self.retypePLabel.text = #"";
return;
}
The alert appears even if I type the same string twice... and only the first text field geets reset to #"" ...
What will fix this?
I'm guessing in the isEqual: method
self.retypePLabel.text
should be
self.retypePTextField.text
try
if (![self.typePTextField.text isEqualToString:self.sometextfield.text]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error", #"Error") message:NSLocalizedString(#"Passwords do not match \n please retype", #"Passwords do not match \n please retype") delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
self.typePTextField.text = #"";
self.sometextfield.text = #"";
return;
}
if([_txtPassword.text isEqual:_txtconfirmPassword.text])
{
NSLog(#"Password =%# , ConfirmPassword = %# ",_txtPassword.text,_txtconfirmPassword.text);
}
else {
//// code show alert////
}
self.retypePLabel.text? are you sure you ask about this, because I think it should be like self.retypePTextField.text