objective c - == and isEqualsToString giving strange behavior -
this question has answer here:
i have below code
nsstring *firstname = @"1.6.1"; nsstring *secondname = @"1.6.1"; if (!(firstname==secondname)) { nslog(@"lock app"); } else { nslog(@"do not lock app"); } if (!([firstname isequaltostring:secondname])) { nslog(@"lock app"); } else { nslog(@"do not lock app"); }
output getting
do not lock app not lock app
however when use actual values firstname
& secondname
, output
lock app not lock app
below details of firstname
& secondname
// coming server firstname = [[nsuserdefaults standarduserdefaults] stringforkey:@"iphoneappversion"]; // coming app version iphone secondname = [self appnameandversionnumberdisplaystring]; - (nsstring *)appnameandversionnumberdisplaystring { nsdictionary *infodictionary = [[nsbundle mainbundle] infodictionary]; nsstring *appdisplayname = [infodictionary objectforkey:@"cfbundledisplayname"]; nsstring *majorversion = [infodictionary objectforkey:@"cfbundleshortversionstring"]; nsstring *minorversion = [infodictionary objectforkey:@"cfbundleversion"]; return [nsstring stringwithformat:@"%@", minorversion]; }
if print firstname
& secondname
, values 1.6.1
, 1.6.1
respectively.
any idea why there 2 different outputs when using equals?
you different behaviour ==
, isequaltostring:
. because ==
operator compares address of object , isequaltostring:
compare string value.
you should not use ==
string comparison.
Comments
Post a Comment