If statement does not work properly - iphone

This is my code:
NSString * atime = [NSString stringWithFormat:#"%#",[theDataObject.minuteArray objectAtIndex:indexPath.row]];
NSLog(#"value of atime is:%#",atime);
if (atime==#"0") {
NSString * timeStr = [NSString stringWithFormat:#"%#s",[theDataObject.secondArray objectAtIndex:indexPath.row]];
cell.timeLabel.text = timeStr;
}
else {
NSString * timeStr = [NSString stringWithFormat:#"%#m%#s",[theDataObject.minuteArray objectAtIndex:indexPath.row],[theDataObject.secondArray objectAtIndex:indexPath.row]];
cell.timeLabel.text = timeStr;
}
But it never return first statement only return second statement,even is atim value is 0.
log value:
MyAudioRecorder[2484:10703] value of atime is:0
also tried to put 0 instead of #"0",still does not work.

if (atime==#"0")
Compares the memory address of atime with the address of constant string #"0".
if ([atime isEqualToString:#"0"])
Compares the values that are stored in the two memory locations.
From your requirement it is seen that you want to compare the value not the memory location. So go for 2nd one.

Try the following:
if ([atime isEqualToString : #" "]) {}

isEqualToString: ->
- (BOOL)isEqualToString:(NSString *)aString
Returns a Boolean value that indicates whether a given string is equal to the receiver using a literal Unicode-based comparison.
Discussion -> When this method compares two strings, if the individual Unicodes are the same, then the strings are equal
For more you can access apple NSString Class Reference
if([atime isEqualToString:#"0"])
{
}
else
{
}

Related

Condition always fails though the comparative values seems correct in iOS

When I check the value of number in nslog it shows '0'
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSNumber *number=[data objectForKey:#"serial"];
NSLog(#"%#",number);
if(number ==0 )
{
imgButton.hidden=YES;
}
But the condition always fails , I also changed the code like this
NSString *number=[data objectForKey:#"serial"]
NSLog(#"%#",number);
if(number == #"0" )
{
imgButton.hidden=YES;
}
But here too the condition fail ,What is the issue with this?
In the first code you are checking a NSNumber, object, against an int.
The correct check is:
if([number intValue] == 0) {
imgButton.hidden = YES;
}
In the second code you are checking two NSString, but you have to use the "isEqualToString" method and not "==". The correct code is:
if([number isEqualToString:#"0"]) {
imgButton.hidden = YES;
}
NSNumber is an object, 0 is an integer (a primitive type). They will never be equal. But you can change the comparison like this [number intValue] == 0 and this will work when the value of your NSNumber is 0.
On the string comparison, you should use the method
isEqualToString:NSString *)string
for the comparison.
For NSNumbers its
isEqualToNumber:(NSNumber *)number
Because otherwise you arent comparing if they have the same value, but if they are stored in identical memory space.

Deleting a number digit by digit on a calculator screen

I want to delete a number digit by digit on the calculator screen but am only being able to delete the full number . Is there any method ?
-(IBAction)cancelInput:(float)result1{
NSString *myString = [[NSNumber numberWithFloat:result] stringValue];
int str=[myString length]-1;
NSString *newstring = [[NSNumber numberWithInt:str] stringValue];
calculatorScreen.text= [NSString stringWithFormat:newstring];
//NSLog(#"%d ",str);
}
Here you go,
NSString *myString = [[NSNumber numberWithFloat:result] stringValue];
myString = [myString substringToIndex:[myString length]-1];
calculatorScreen.text= myString;
int str=[myString length]-1 will only return the length of your string, say if your number is 9876, it will return 3(4-1) and your newstring will be 3.If you want to delete the last character you have to create substring of myString.
string = [string substringToIndex:[string length] - 1];
-(IBAction)backPressed:(UIButton *)sender
{
self.display.text=[self.display.text substringToIndex:[self.display.text length]-1];
if ( [self.display.text isEqualToString:#""] || [self.display.text isEqualToString:#"-"])
{
self.display.text =#"0";
}
}
This is the core function for the button. I took the liberty to add in the if feature so that it could handle some of the basic issues. As usual, replace display with your primary label. The If function help to counter check that your label does not become empty " " or have a negative sign "-" and will automatically replace it with "0", similar to clear in that aspect. in which you can run the clear method instead. But use
self.display.text =#"0";
for better clarification instead.
Use this code for it:
else if([character isEqualToString:(NSString*) Delete])
{
NSInteger index_of_char_to_remove=[_display length]-1;
if(index_of_char_to_remove>=0)
{
[_display deleteCharactersInRange:NSMakeRange(index_of_char_to_remove, 1)];
last_character_is_operator=NO;
}
}
When press 'C' it will checked this condition and delete the data one by one

NSString substring wont compare

My string comparison keeps returning false and I dont understand why. Even my nslog says the value is correct. Do you know why my comparison keeps returning false even though the strings appear to be the same? If I step through the program type shows SV as its value. I have ensured there are no spaces in this string as well. We get the first two chars of this:
SV2B799E5B-4306-4965-B5DD-944D3970E6B6
NSString *fPath = [path stringByAppendingPathComponent:[directoryContent objectAtIndex:x]];
NSString *fName = [directoryContent objectAtIndex:x];
NSString *type = [fName substringToIndex:2];
NSLog(#"TYPE: %#",type);
if ([type caseInsensitiveCompare:#"SV"])
{
NSData *file = [[NSData alloc] initWithContentsOfFile:fPath];
if (file)
{
[[WebService sharedWebService]saveVolunteer:nil :YES :[directoryContent objectAtIndex:x] :file];
[file release];
}
}
[NSString -caseInsensitiveCompare:] does not return a BOOL, it returns an NSComparisonResult. This is going to be 0 if the strings are equal (in a case insensitive fashion), which is why you're seeing that result.
Invert your result and you'll be set, or to be more correct, check to see if it is == NSOrderedSame.
The method you are calling returns an NSComparisonResult, not a boolean value. It so happens that an NSComparisonResult of equal has the value zero, which is interpreted as false.
caseInsensitiveCompare: returns a NSComparisonResult. Try [type caseInsensitiveCompare:#"SV"] == NSOrderedSame

Why isn't this conditional statement working with NSUserDefault?

I'm having a problem getting one of my conditional statements to display data correctly. Here is the code I'm working with:
NSUserDefaults *pickerDefaults = [NSUserDefaults standardUserDefaults];
NSString *myString = [pickerDefaults stringForKey:#"userpicker"];
NSString *string = #"Name1";
NSLog(#"%#",myString); //This prints out Name1
NSLog(#"%#",string); //This also prints out Name1
if (myString == string) {
[pickerArray addObject:#"Name Other"];
}
else {
return;
}
I can't get this if statement to add that object to the UIPicker eventhough both strings are equal to each other. However, if I change it to not equal to != then it display's the object in the UIPickerView. I don't understand what I'm doing wrong. Any help would be great. Thanks.
To compare string objects, don't use their pointer values, but compare using
[string isEqualToString:myString]
if (myString == string) // Wrong : It compares address of two NSStrings
if ([myString isEqualToString:string]) // This compares values of NSStrings
{
}

Can't get rid of this warning?

I'm getting this warning "Format not a string literal and no format arguments? Any ideas?
-(BOOL)isFirstPointReached{
NSString *firstPoint = [NSString stringWithFormat:[pointsToFillArray objectAtIndex:0]];
NSString *lastPoint = [NSString stringWithFormat:[pointsToFillArray lastObject]];
if([firstPoint isEqualToString:lastPoint]){
return YES;
}
else{
return NO;
}
}
A few points...
The pointsToFillArray is an array of objects and the compiler does not know if it contains NSStrings or any other type of object. To get rid of the error you would cast it to (NSString*)
Secondly, the stringWithFormat is normally used to create a string from a few different pieces of data and does not need to be used in this case
Thirdly, you could just create pointers to the objects within the array and then do your check
The following should work for you:
NSString *firstPoint = (NSString*)[pointsToFillArray objectAtIndex:0];
NSString *lastPoint = (NSString*)[pointsToFillArray lastObject];
if ([firstPoint isEqualToString:lastPoint]) {
return YES;
}