Can't set a BOOL to yes - iphone

I've ran into this problem and so far I can't find any answers. so a have a BOOL property and I am trying to set it to YES/TRUE/true ( I have tried them all) but whenever I check the BOOL it always returns NO/FALSE/false. Below is my code
[myObject setAllowReg:YES];
myObject.allowReg = YES;
if (myObject.allowReg)
{
NSLog(#"YES");
}
else
{
NSLog(#"NO");
}
The output to this is always "NO". This must be something really simple that I am missing but it seems whatever I try doesn't work.
Thanks

Beat guess is that myObject.allowReg is nil and probably myObject is nil.
Add some NSLog statements to help figure this out:
NSLog(#"myObject: %#", myObject);
NSLog(#"myObject.allowReg: %i", myObject.allowReg);
Advice: In debugging break everything down to the simplest statements and test each step along the way.

myObject is probably nil. You can send messages to nil objects, they always return a 0 value.

Related

How can I Compact Code

I have multiple text fields in my app and want all of them to not crash if the textfield is empty.
I have the code that fixes this but I am wondering if there is something i can write that shortcuts this code?
SO i could write
fixNilError()
and it would run the code:
if textField.text != nil {
print("Success")
}
textField.text returns an optional value, which means that the value is either something, or nil.
To handle this value, you need to "unwrap" it, and one of the cleaner way to make it would be this one:
if let fieldText = textField.text {
print(fieldText)
}
Not sure it helps you to make your code shorter, but optionals is a clever way to make it secure.
use method hasText() to check multiple textfield empty or not see below code and code accordingly.
if !text1 .hasText(){ // in this case text1 does not contain text
// deliver appropriate message to user here
return;
}else if !text2 .hasText(){
// deliver appropriate message to user here
return;
}

NSString or NSCFString in xcode?

I m taking a NSMutabledictionary object in NSString like this :
NSString *state=[d valueForKey:#"State"];
Now sometimes state may be null and sometimes filled with text.So Im comparing it.While comparing state becomes NSString sometimes and NSCFString othertimes..So unable to get the desired result..
if([state isEqualToString#""])
{
//do something
}
else
{
//do something
}
So while comparing it is returning nil sometimes.So immediately jumping into the else block.
I need a standard way to compare if the state is empty whether it is a NSString or NSCFString ...
How can I do it?
If you're unable to get the result you want, I can assure you it's not because you get a NSCFString instead of a NSString.
In Objective-C, the framework is filled with cluster classes; that is, you see a class in the documentation, and in fact, it's just an interface. The framework has instead its own implementations of these classes. For instance, as you noted, the NSString class is often represented by the NSCFString class instead; and there are a few others, like NSConstantString and NSPathStore2, that are in fact subclasses of NSString, and that will behave just like you expect.
Your issue, from what I see...
Now sometimes state may be null and sometimes filled with text.
... is that in Objective-C, it's legal to call a method on nil. (Nil is the Objective-C concept of null in other languages like C# and Java.) However, when you do, the return value is always zeroed; so if you string is nil, any equality comparison to it made with a method will return NO, even if you compare against nil. And even then, please note that an empty string is not the same thing as nil, since nil can be seen as the absence of anything. An empty string doesn't have characters, but hey, at least it's there. nil means there's nothing.
So instead of using a method to compare state to an empty string, you probably need to check that state is not nil, using simple pointer equality.
if(state == nil)
{
//do something
}
else
{
//do something
}
You can do this
if([state isEqualToString:#""])
{
//do something
}
else
{
//do something
}
You must have to type cast it to get the correct answer.
NSString *state = (NSString *) [d valueForKey:#"State"];
if(state != nil)
{
if(state.length > 0)
{
//string contains characters
}
}

Making the app recognize "the right answer"

I am new in the game, so probably an easy issue. What I am trying to do is to make an app with a question asked, and a textFiled in which to answer. Then, i want the app to recognize when the answer is right (in this case the number 25) and when it is wrong (not 25). Everything is working fine, and I get the "wrong" message, but I cant make it recognize the right answer.
- (IBAction)btnSubmitAction:(id)sender {
if (textFieldAnswer.text == #"25") {
lblAnswer.text = #"Yes, your right!";
btnNext.hidden = 0;
} else {
lblAnswer.text = #"No, try again.";
}
}
Thanks a lot!
Well, this is how you're supposed to compare strings:
- (IBAction)btnSubmitAction:(id)sender {
if ([textFieldAnswer.text isEqualToString:#"25"]) {
lblAnswer.text = #"Yes, you're right!";
btnNext.hidden = NO;
} else {
lblAnswer.text = #"No, try again.";
}
}
Comparing objects in Objective-C is done by ==. This means that you compare pointers, which is not the same as comparing strings, because they are pointers. If you would like to compare strings for equality you should call the BOOL instance method isEqualToString of NSString class.
[string1 isEqualToString:#"someString"]

Trouble comparing NSString to NSArray object

Ok, so I'm trying to check if an object from an NSArray equals something inputed by the User into a UITextField. It should work, but for some reason it dosn't. Here is my code:
if (theAnswer.text == [correctAnswers objectAtIndex:problemNumber]) {
NSLog(#"CORRECT");
}
else {
NSLog(#"wrong");
}
The console always give wrong.
I put this log in:
NSLog(#"%# %#", theAnswer.text, [correctAnswers objectAtIndex:problemNumber]);
And I get A A
wrong
printed everytime. Thanks for the help
Objective-C doesn't support the == operator for NSStrings. That will do a comparison of the pointers to the NSStrings and not the contents of the string itself.
Try
if([theAnswer.text isEqualToString:[correctAnswers objectAtIndex:problemNumber]]) {
}

curious what is wrong with this if statement?

Could someone explain what is wrong with this "if statement"? I get:
"lvalue required as left operand of assignment".
This does not work:
if ([[prepDataArray objectAtIndex:iNDEX] boolValue] = YES) {
NSLog(#"HARD");
}
While this works:
diffQ = [[prepDataArray objectAtIndex:iNDEX] boolValue];
if (diffQ = YES) {
NSLog(#"HARD");
}
I do realize where the problem are and that the 'lvalue' indicate that i need to have something different on the left side but i do not grasp why and how to do what i want inside the 'if' statement as tried in the first example.
Would appreciate if someone nice could give me a hint :-)
if ([[prepDataArray objectAtIndex:iNDEX] boolValue] == YES) {
NSLog(#"HARD");
}
it's == not =
The first one doesn't work because you try to assign a BOOL (YES) to a message. The second one works because you try to assign a BOOL to diffQ. This is correct, but not the result you expect (comparing diffQ to YES)
Common programming error ;) I've done this a millions times
I completely agree with what #thomas said above, but let me add.
Don't compare a bool to YES. It's not that the if construct requires
if( some comparison statement ) {
....
}
That's not the case. The if construct has the following form:
if( a boolean value) {
...
}
It just happens that a comparison statement yields a boolean, so you put that in the if statement.
In your case, boolValue already gives you a bool. You don't need to compare that against YES or NO. That's like doing YES==YES or YES==NO and it's completely redundant.
Just do
if ([[prepDataArray objectAtIndex:iNDEX] boolValue]) {
NSLog(#"HARD");
}