Reputation: 1
I have a simple program that I'm testing a printer class in.
-(void) setInkType {
NSMutableString *theInkType;
InkType typeOfInk;
char inkFromInput[50];
NSLog(@"What type of ink are you using?");
NSLog(@"Options are photoInk, lazerJet, regularInk");
fgets(inkFromInput,50,stdin);
theInkType = [[NSMutableString alloc] initWithUTF8String:inkFromInput];
NSLog(@"%@",theInkType);
if([theInkType compare: @"photoInk"]==true) {
typeOfInk.photoInk = 564;
NSLog(@"Your using a photo ink of type %d",typeOfInk.photoInk);
inkType.photoInk = typeOfInk.photoInk;
}
else { if ([theInkType compare: @"lazerJet"] == true) {
typeOfInk.lazerJet = 94;
NSLog(@"Your using a lazer toner of type %d",typeOfInk.lazerJet);
inkType.lazerJet = typeOfInk.lazerJet;
}
else { if ([theInkType compare: @"regularInk"] == true) {
typeOfInk.regularInk = 910;
NSLog(@"Your using a regular ink of type %d",typeOfInk.regularInk);
inkType.regularInk = typeOfInk.regularInk;
}
}
}
}
When I run this I can enter in "photoInk" and "lazerInk" and I get a proper output. Why is it when I type "regularInk" I get a bad output?
I'm thinking it could be my {}'s but I'm not quite sure. I've been scratching my head for a few hours at this.
If there is anymore Cocoa flavoring I can do to make this look smoother let me know too please.
Upvotes: 0
Views: 109
Reputation: 19469
I think this should work for you. This is my answer which I have taken from the link:
SOLUTION-1: I have modified it here a bit to make it more easier for your case:
Let us assume String1 is one NSString.
//Though this is a case sensitive comparison of string
BOOL boolVal = [String1 isEqualToString:@"My Default Text"];
//Here is how you can do case insensitive comparison of string:
NSComparisonResult boolVal = [String1 compare:@"My Default Text" options:NSCaseInsensitiveSearch];
if(boolVal == NSOrderedSame)
{
NSLog(@"Strings are same");
}
else
{
NSLog(@"Strings are Different");
}
Here if boolVal is NSOrderedSame then you can say that strings are same else they are different.
SOLUTION-2: Also you don't find this easy, you can refer to Macmade's answer under the same link.
Hope this helps you.
Hope this helps you.
Upvotes: 0
Reputation: 27984
-compare:
doesn't return a boolean true/false value, it returns an NSComparisonResult
, which is either NSOrderedAscending
, NSOrderedSame
, or NSOrderedDescending
.
So you could do this:
if ([theInkType compare: @"photoInk"] == NSOrderedSame)
But really, the -isEqual:
method is closer to your true intention.
if ([theInkType isEqual: @"photoInk"])
Also: you're doing your else
clauses wrong. Not this:
if (x) {
...
}
else { if (y) {
...
} }
But this:
if (x) {
...
} else if (y) {
...
}
Upvotes: 3