This question already has answers here:
Understanding NSString comparison
(7 answers)
Closed 7 years ago.
I´m trying to make a registration form. There are two UITextField to write the Password and to confirm it. When i write exactly the same password, it always shows: "Passwords do not match ".
-(IBAction)signUp:(id)sender{
#try {
if([[name text] isEqualToString:#""] || [[mail text] isEqualToString:#""] || [[phone text] isEqualToString:#""] || [[password text] isEqualToString:#""] || [[rpassword text] isEqualToString:#""] ) {
[self alertStatus:#"Please check every field" :#"¡Alert!"];
}else if (password.text!=rpassword.text){
[self alertStatus:#"Passwords do not match " :#"¡Please Check!"];
}else {
NSString *strURL = [NSString stringWithFormat:#"http://miwebsite.com/signup.php?var1=%#&var2=%#&var3=%#&var4=%#", name.text, mail.text, phone.text, password.text];
NSURL *url2 = [NSURL URLWithString:strURL];
NSLog(#"url: %#", url2);
NSError *error;
NSData *dataURL = [NSData dataWithContentsOfURL:url2 options:0 error:&error];
if (dataURL == nil) {
NSLog(#"error: %#", error);
}
else {
NSLog(#"dataURL: %#", dataURL);
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
// THE REST OF THE CODE GOES HERE...
}
}
} #catch (NSException * e) {
NSLog(#"Exception: %#", e);
[self alertStatus:#"Sorry." :#"Failed"];
}
}
I used the debugger. password.text and rpassword.text are exactly the same word. ¿Why my code goes inside that sentence?
Thanks in advanced.
== in objective C is a pointer comparison instead of the content comparison. The text NSString object have 2 different memory address or pointers associated to them and although the content is same the memory they point is different and hence the statement inside this
else if (password.text!=rpassword.text){
[self alertStatus:#"Passwords do not match " :#"¡Please Check!"];
}
is executed. The pointers are different.
What you need is string comparison by doing so:
else if (![password.text.isEqualToString:rpassword.text]){
[self alertStatus:#"Passwords do not match " :#"¡Please Check!"];
}
For more explanation please check this link:
Understanding NSString comparison
Related
This question already has answers here:
How to detect if NSString is null?
(5 answers)
Closed 8 years ago.
I'm new to iOS development. I get an error when I compare NSString with NSString nil value. It is not working in if condition.
my code is:
NSDictionary *responseFromJSON = [JSON objectForKey:#"response"];
NSString *strResponseMsg = [responseFromJSON objectForKey:#"104"];
if ([strResponseMsg isEqualToString:nil])
{
NSLog(#"login Invalid");
}
else
{
NSLog(#"login success");
}
You can simply do like this,
NSString * string = nil;
if (string!=nil && // not nil, means 0x0 object
string.length>0 && // at leaset one character should exists
[string isEqual:[NSNull null]]) { // to avoid 'null' in string
// valid string
}
You can do like this,
if ([yourString isEqual:[NSNull null]])
{
//your code goes here
}
Hope this helps.
Use below
NSString *strResponseMsg = [responseFromJSON objectForKey:#"104"];
if (!strResponseMsg)
{
NSLog(#"login Invalid");
}
else
{
NSLog(#"login success");
}
If you want to check the NSString whether it's nil or empty.
You just need to do something like that:
if (strResponseMsg.length) {}
Because it will not go inside if the strResponseMsg is nil or empty. It will only go inside when strResponseMsg is not nil and no empty.
This question already has answers here:
NSString question - rangeOfString method
(2 answers)
Closed 8 years ago.
I'd like to check if this string contains "gif" value inside. I'm trying to achive this goal doing this:
NSString *originalSourceLinkToContent = #"site_media/obrazki/2014/07/faa4f4da470d7650cffe88f70cde230b_cropped.gif?1404832"
NSRange isRange = [originalSourceLinkToContent rangeOfString:#"cropped"; options:NSCaseInsensitiveSearch];
if(isRange.location != 0) {
//found it...
NSLog(#"HAS STRING");
} else {
NSLog(#"HAS NO STRING");
}
where originalSourceLinkToContent is my string. I always get "HAS NO STRING" message even if there is this substring. How to make it working? In android it is simple because there is a method for that and always woks. What is equivalent method in iOS?
Use NSNotFound instead of 0
NSString *str2 = #"site_media/obrazki/2014/07/faa4f4da470d7650cffe88f70cde230b_cropped.gif?1404832";
NSRange isRange = [str2 rangeOfString:#"cropped" options:NSCaseInsensitiveSearch];
//use `NSNotFound`
if(isRange.location != NSNotFound) {
//found it...
NSLog(#"HAS STRING");
} else {
NSLog(#"HAS NO STRING");
}
This code will work
Being an iOS and Objective-C newbie I'm trying to construct an app, where the user can authenticate through Facebook, Google+ or 3 further (Russian) social networks.
For Facebook I know, that I could use the Facebook SDK or Social.framework, but for the others - I have to use OAuth and UIWebView, because there are no good SDKs for them yet.
(I have already succeeded in that once - but that was for an Adobe AIR app and now I'm trying to learn native...)
In Xcode 5.2 I have prepared a very simple Master-Detail app for iPhone and checked it into GitHub:
My question is about constructing a NSString for a GET (or body in POST) request -
Currently I have the following awkward source code in the DetailViewController.m:
- (NSString*)buildUrl
{
NSString *key = _dict[kKey];
NSString *str = _dict[kAuthUrl];
if ([key isEqual: kFB]) {
str = [str stringByAppendingString:#"display=touch"];
str = [str stringByAppendingString:#"&response_time=token"];
str = [str stringByAppendingString:#"&client_id="];
str = [str stringByAppendingString:_dict[kAppId]];
str = [str stringByAppendingString:#"&redirect_uri="];
str = [str stringByAppendingString:_dict[kAppUrl]];
//str = [str stringByAppendingString:#"&state="];
//str = [str stringByAppendingString:rand(1000)];
} else if ([key isEqual: kGG]) {
} else if ([key isEqual: kMR]) {
} else if ([key isEqual: kOK]) {
} else if ([key isEqual: kVK]) {
}
return str;
}
My questions:
Instead of using stringByAppendingString could I use something nicer? Like maybe an NSArray (or even better NSDictionary) and then somehow joining it with ampersands inbetween?
How to escape the HTML entities in the value of redirect_uri= ?
I need to append a random number as the value of state=, but I am not sure what function to use there best...
Here is what my app prints at the moment, for the above code:
MyAuth[9626:70b] request: { URL: https://graph.facebook.com/oauth/authorize?display=touch&response_time=token&client_id=432298283565593&redirect_uri=https://www.facebook.com/connect/login_success.html }
(which is no good: the URL at the end is not escaped and there is no random state number).
stringWithFormat: is your friend.
- (NSString*)buildUrl
{
NSString *key = _dict[kKey];
NSString *str = _dict[kAuthUrl];
if ([key isEqual: kFB]) {
NSString *escapedURI = [_dict[kAppUrl] stringByAddingPercentEscapesUsingEncoding:NSUTF8Encoding];
int state = arc4random_uniform(1000);
str = [NSString stringWithFormat:#"%#display=touch&response_time=token&client_id=%#&redirect_uri=%#&state=%d", _dict[kAuthUrl], _dict[kAppId], escapedURI, state];
} else if ([key isEqual: kGG]) {
} else if ([key isEqual: kMR]) {
} else if ([key isEqual: kOK]) {
} else if ([key isEqual: kVK]) {
}
return str;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
xcode iOS compare strings
Trying to get a UIImageview to show a different image based on the location (city) of the user, here is the code I got:
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(#"Found placemarks: %#, error: %#", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
//Display country and city
country.text = [NSString stringWithFormat:placemark.country];
city.text = [NSString stringWithFormat:placemark.locality];
if (placemark.locality==#"Cupertino")
banner.image = [UIImage imageNamed:#"cupertino.png"];
} else {
NSLog(#"%#", error.debugDescription);
}
} ];
I can get the labels to display the country and city, but can't get the UIImageview to change based on the city, any ideas?
Use isEqualToString: method to compare the string, like
if ([placemark.locality isEqualToString:#"Cupertino"])
banner.image = [UIImage imageNamed:#"cupertino.png"];
} else {
NSLog(#"%#", error.debugDescription);
}
Make sure you have allocated the image using [UIImage alloc] ..
In my project i have implemented multiple service and getting response. Among that one WSDL service that returns one data set as a response,
+ (id)deserializeAsDictionary:(CXMLNode*)element {
NSLog(#"deserializeAsDictionary = %#, children: %d", element.stringValue, [element childCount]);
if([element childCount] == 1) {
CXMLNode* child = [[element children] objectAtIndex:0];
if([child kind] == CXMLTextKind) {
NSLog(#"child %# added", [child stringValue]);
return [[[element children] objectAtIndex:0] stringValue];
}
}
NSMutableDictionary* d = [NSMutableDictionary dictionary];
NSInteger i = 1;
NSString *objKey;
for(CXMLNode* child in [element children]) {
id v = [Soap deserialize:child];
if(v == nil) {
v = [NSNull null];
} else {
if([[child name] isEqualToString:#"(null)"]) {
objKey = [NSString stringWithFormat:#"%#",[child stringValue]];
} else if([[child name] isEqualToString:#"key"] || [[child name] isEqualToString:#"value"]) {
objKey = [NSString stringWithFormat:#"%#",[child name]];
} else {
objKey = [NSString stringWithFormat:#"%#%d",[child name],i++];
}
}
[d setObject:v forKey:objKey];
NSLog(#"child %# added", objKey);
}
return d;
}
soap class will use this method to convert that data set into dictionary. Its working perfectly in IOS 5 but its not working in ios 6. Application will crash in ios 6 and through error as " CXML copy with zone "Please help me to fix this issue.
I think u get the soap classes with ARC enabled for ios6.If so you need to update the soap request class.Please refer this link Sudzc with iOS 5 and ARC
I think it may useful to you