Constantly Returning True - objective-c

For some reason my boolean seems to be returning true, even when the else should be getting called during testing:
if([photoId isEqualToString:object]) {
liked = TRUE;
cell.likedButton.imageView.image = [UIImage imageNamed:#"likedBtn.png"];
break;
} else {
liked = FALSE;
cell.likedButton.imageView.image = [UIImage imageNamed:#"likeBtn.png"];
}
To clarify a little more, photoId is sometimes equal to the object. But in one case, it isn't equal to it. The images in my cell are changing to the right files contingent on the equal method however, the boolean seems to only return yes.
Any ideas why this is happening? Does it have something to do with my break?

You should only put a break statement when you want to terminate a loop. Perhaps you're running this conditional inside a loop so once the statement is TRUE, your entire loop stops (break).

Since it is in a loop it is breaking after the first time it is true. You should put the break in the else statement if you want it to break if it is false.

Related

Required fields in React-Native

I am checking some user input values if they are empty and if yes then I looping through their titles (textinput title) and checking if they contain '*'. If they do, I display a message to the user.
However, my approach isnt really working for some reasons. It just continues further even if the user doesnt fill out the required fields.
What am I doing wrong here?
for (i = 0; i < this.myInputFields.myTextFields.length; i++) {
data[this.myInputFields.myTextFields[i].key] = this.myInputFields.myTextFields[i].inputValues; //there is a bug here, if user doesnt fill the first textinput it will fail to send
if (this.myInputFields.myTextFields[i].inputValues === '') {
if (this.myInputFields.myTextFields[i].title.every('*')) {
return Alert.alert('Please fill out the required fields!')
}
}
}
If the user doesnt give an input in the first TextInput, that fails the operation and displays an error which says that 'this.myInputFields.myTextFields[i].key' is not defined. It is obviously failing because it is finding the array empty and in fact it expects there to a be a value. But how can I make it continue even that one is empty?
You are comparing the value of this.myInputFields.myTextFields[i].inputValues with '' explicitly. This will fail if inputValues is anything other than ''. (undefined for example)
...
if (this.myInputFields.myTextFields[i].inputValues === '')
...
instead, just check for falsiness:
...
if (!this.myInputFields.myTextFields[i].inputValues)
...

Function Node-red Raspberry

I am testing with my new Raspberry Pi and starting with the Node-red and I have encountered an obstacle.
I try to make a loop which ends when a switch is triggered, but when it enters the while loop it does not re-read the "alarm" variable and loop infinitely.
context.data = context.data || {};
switch (msg.topic){
case "alarma":
context.data.alarma = msg.payload;
msg = null;
break;
case "pi/13":
context.data.contacto = msg.payload;
msg = null;
break;
}
if(context.data.alarma === true && context.data.contacto == 1)
{
while(context.data.alarma === true)
{
context.data.alarma = msg.payload;
node.send({payload:true});
}
}
return {payload:"stop"};
I have tried putting all the code in a "do - while" loop but it fails, I have also tried to put the whole switch block back into the while and the same thing. Any ideas?
I'm making an alarm system, for this I have a switch (to activate and deactivate the alarm) and a contact in a door. If the alarm is activated and the contact opens, the alarm sounds (I return true), the alarm will be disconnected if I close the contact again (that is not what I pretend) I want that only disconnect if the alarm deactivation switch is activated. If I do not turn off the switch I want it to return true so the alarm will continue to ring. I have also tried to introduce a delay so the while loop is not so fast, but still not working. What I want is to know how to actually the variable inside the loop.
Thank you!!
That's because the variable will probably never actually change, that loop will just run so tight and never return so Node-RED will not get a chance to process any messages that would change the variable.
It will also be sending 1000's of message with payload: true
You should not need the loop at all, just return {payload: true} in the if else return {payload: 'stop'} (should that be false rather than stop?)
if(context.data.alarma === true && context.data.contacto == 1)
{
return {payload:true});
} else {
return {payload:"stop"};
}

Durandal - Correct way to disable .canDeactivate for 'Success' operations?

I have an edit page (in a DurandalJS single page app), where I use the .canDeactivate lifecycle method to check if there are any changes to the record, and optionally prompt them for confirmation before leaving the page.
I also have a 'Save' and 'View History' button. Is the correct thing to do to override the .canDeactivate method before calling router.navigate, to stop the modal popup invoking?
E.g.: As here:
self.onSave = function() {
self.repository.updateItem(self.model).done(function() {
self.canDeactivate = null; // Is this the correct way to do this?
router.navigate("#/home");
}
}
As this .canDeactivate will otherwise get called:
self.canDeactivate = function() {
if (!self.model.hasChanges()) {
return true;
}
return app.ShowMessage("Unsaved data will be lost", "Are you sure you wish to exit?", ["Yes", "No"]).done(function(result) {
return result !== "No";
}
};
Why dont you just set
self.model.hasChanges(false)
in your updateItem callback?
Then when your canDeactivate is called, it will return true.
Also you seem to have an error in your ShowMessage callback. I think you mean to do:
return result != "No";
I don't think the way Durandal decides whether to attempt to call a canDeactivate function is fully defined, other than the fact that if it's not in the view model, it won't try. Hence, even if it works as is, a future version of the framework could change its check to something like if (canDeactivate in viewModel) viewModel.canDeactivate(...); without further tests, and your code would break.
This is unlikely, but if you want to worry about it, you should thus delete self.canDeactivate instead of assigning it the null value.
Quote from the documentation:
To participate in the lifecycle, implement any (or none) of the
functions below on the object that you set the activator to (...)
Current implementation (activator.js, L126, 1eecbc2d3f84dc42eb7304bde761d88f300d8951):
if (item && item.canDeactivate) {
So it only checks if it's truthy (which would indicate using null works fine currently, too).
If you want to discuss the pattern, I don't see anything wrong with it, as long as it makes sense to you and everyone who should read the code.
You're not supposed to be activating and deactivating views programmatically in any critical path, so performance should be irrelevant either way (flag on view model or deletion of canDeactivate).

pausing objective-c runtime loop for user input

I have this code:
while (!found) {
char letterGuess=(char)([[[wordList letterRank:lengthOfWord]objectAtIndex:yescount]intValue]+97);
NSString *stringLetter=[NSString stringWithFormat:#"%c",letterGuess];
if ([prevGuess count]<=0) {
alreadyGuessed=NO;
}else {
if ([prevGuess containsObject:stringLetter]) {
alreadyGuessed=YES;
yescount++;
}else{
alreadyGuessed=NO;
}
}
if (!alreadyGuessed) {
[prevGuess addObject:stringLetter];
[self drawYesNo];
NSLog(#"%c",letterGuess);
}
if (userAnswer) {
}else {
[self noGuess:letterGuess];
}
if ([wordList count]<=1) {
NSLog(#"word found");
found=YES;
}
}
basically, it takes a letter from a sorted array, checks it against another array containing all the previous letters that were entered, and if it is not in that array, it will call [self drawYesNo], which basically sets up and draws a yes and a no UIButton, and based on what the user presses, changes the 'userAnswer' variable.
My problem is that this loop executes so quickly that the objectAtIndex quickly exceeds the bounds of the array, and throws an error. I need some way to pause during the [drawYesNo] method, and allow the user to actually make a decision.
I know there are answers to similar questions on StackOverflow, but I just can't make heads or tails of them. Can someone please explain this for a very new OO programmer?
I can't really figure out all the logic of your code, but it sounds like you need to refactor it into different methods. Instead of having the code in a while loop, you should have one method that does most (all? I can't tell) of what you posted (without the while loop). You call this once when your app starts, then after the user clicks a button -- in the button's action method you change the value of userAnswer, and then call that method again. This keeps going until some condition is met in your button method that would cause it to stop.

Checkbox State Problem

I'm new to Cocoa, and working my way through Hillegass's book. I'm having trouble with chapter 20's challenge 2.
The challenge is to add checkbox's that toggle if the letter being drawn is italic, bold, or both.
-(IBAction)toggleItalic:(id)sender
{
int state = [italicBox state];
NSLog(#"state %d", state);
if (state = 1) {
italic = YES;
NSLog(#"italic is yes");
}
else {
italic = NO;
NSLog(#"italic is no");
}
}
Right now, this snippet of code is returning yes when the box is checked, and when the box is unchecked. What am I doing wrong?
Thanks,
Justin.
Your problem lies in your if statement:
if (state = 1) {
you are assigning state to the value 1: state = 1, while what you need to test is if state is currently 1: state ==1
This is a fairly common mistake (especially in languages that allow assignment in if statements). One trick to learn to get around this is to make your comparison checks like so:
if (1 == state)
You cannot assign 1 to another value. Therefore, if you mistakenly use = instead of == you will get a compiler error and it's an easy fix.
Use comparison instead of assignment and use proper enums instead of hardcoded values that could change:
if (state == NSOnState)
else if (state == NSOffState)