This question already has answers here:
Quicker if-statement: if `variable` is "value" or "value"
(7 answers)
Closed 7 years ago.
I have a iOS app with a few simple and logical if-statements. However they wont run and I can't understnad why. Here is my simple code:
-(void)viewDidLoad {
[super viewDidLoad];
// Run setup code.
int day = [self currentDay];
if ((day == (1 || 3 || 7) && (day != (2 || 4 || 5 || 6))) {
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
}
else if ((day == (2 || 4 || 5 || 6)) && (day != (1 || 3 || 7))) {
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
}
}
-(int)currentDay {
NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [component weekday];
}
What am I doing wrong here?
This compiles, but it does something completely different from what you are trying to achieve:
day == (1 || 3 || 7) && (day != (2 || 4 || 5 || 6)
The statement ORs 1, 3, and 7, and then ORs 2, 4, 5, and 6, before performing comparisons.
A proper way to do it is to compare day to 1, 3, and 7 separately. If any of the comparisons is successful, it's also guaranteed that the day is not 2, 4, 5, or 6:
if (day == 1 || day == 3 || day == 7)
...
if (day == 2 || day == 4 || day == 5 || day == 6)
...
You could also rewrite this with a switch:
switch(day) {
case 1:
case 3:
case 7:
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
break;
case 2:
case 4:
case 5:
case 6:
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
break;
}
You're overcomplicating it a bit.
if(day==1 || day==2 || day==3){
[self runSetupVX_4];
}else{
[self runSetupVX_2];
}
Should do the trick
If the first part of each if statement is true, then the second part will be false every time. So you don't have to check the second part.
You can modify your code like this:
-(void)viewDidLoad {
[super viewDidLoad];
// Run setup code.
int day = [self currentDay];
if (day ==1 || day==3 || day==7) {
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
}
else{
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
}}
-(int)currentDay {
NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [component weekday];
}
Hope this works for you:
if (day == 1 || day == 2 || day == 3) {
// Run the calendar setup code for
// Sunday/Tuesday OR Saturday.
[self runSetupVX_4];
}
else if (day == 2 || day == 4 || day == 5 || day == 6) {
// Run setup code for Monday
// Wednesday, Thursday and Friday.
[self runSetupVX_2];
}
You can put break point at if statement and then check that if statement is get called or not.
-(void)viewDidLoad {
[super viewDidLoad];
// Run setup code.
int day = [self currentDay];
//if ((day == (1 || 3 || 7) && (day != (2 || 4 || 5 || 6))) {
//here u will get 0 or 1 from this (1 || 3 || 7) && (day != (2 || 4 || 5 || 6)))
//And u r comparing with 0 or 1 with day so please check below code how to compare
if (((day == 1 || 3 || 7) && (day != 2 || 4 || 5 || 6)) {
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
}
else if ((day == 2 || 4 || 5 || 6)&& (day != 1 || 3 || 7)) {
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
}
}
-(int)currentDay {
NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [component weekday];
} //you have problems with comparing variable please check it
Related
In viewDidLoad I'm programmatically creating 13 blocks using the code:
for (int X = 1; X <= 13; X++) {
UIImageView *iceBlockX = [[UIImageView alloc] initWithFrame:CGRectMake((28 * X - 4),52,28,28)];
iceBlockX.image = [UIImage imageNamed:#"iceBlock.png"];
iceBlockX.tag = X;
[self.view insertSubview:iceBlockX belowSubview:_topPenguinCollisionTarget];
}
I need to detect a collision between an imageView created on the storyboard (penguin) and any one programmatically-created imageView block. If this is possible, what is the code used for the detection? I've tried the following code, but it's not working:
if (((CGRectIntersectsRect(_penguin.frame, _iceBlock1.frame)) && (_iceBlock1.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock2.frame)) &&(_iceBlock2.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock3.frame)) && (_iceBlock3.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock4.frame)) && (_iceBlock4.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock5.frame)) &&(_iceBlock5.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock6.frame)) && (_iceBlock6.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock7.frame)) && (_iceBlock7.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock8.frame)) && (_iceBlock8.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock9.frame)) && (_iceBlock9.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock10.frame)) && (_iceBlock10.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock11.frame)) && (_iceBlock11.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock12.frame)) && (_iceBlock12.hidden == NO)) ||
((CGRectIntersectsRect(_penguin.frame, _iceBlock13.frame)) &&(_iceBlock13.hidden == NO)))
{
NSLog(#"A collision was detected");
}
CGRectIntersectsRect return true if "rect1" intersects "rect2", else false.
- (BOOL)didUIImageCollided{
for (int X = 1; X <= 13; X++)
{
UIImageView *iceBlockX = (UIImageView*)[self.view viewWithTag:X];
if(CGRectIntersectsRect(_penguin.frame, iceBlockX.frame))
{
return YES;
}
}
return NO;
}
I have two dates (Date object). I need to check if the difference between is exactly of multiples of X months (considering days and last days of the months).
For simplicity, let's assume that X will be 4 for this question.
The simplest logic that I want to achieve are the following points (Only except that FOUR_MONTHS doesn't work, because modulo seems to only work with integers). So, I'd like to know if there's any way (that I might have simply missed) to mimic the following:
(Date.parse('2017-01-15') - Date.parse('2016-09-15')) % (FOUR_MONTHS) == 0
(Date.parse('2017-01-15') - Date.parse('2016-09-14')) % (FOUR_MONTHS) != 0
(Date.parse('2017-01-15') - Date.parse('2016-05-15')) % (FOUR_MONTHS) == 0
(Date.parse('2017-01-31') - Date.parse('2016-09-30')) % (FOUR_MONTHS) == 0
(Date.parse('2016-11-30') - Date.parse('2016-07-31')) % (FOUR_MONTHS) == 0
(Date.parse('2016-11-30') - Date.parse('2016-07-30')) % (FOUR_MONTHS) == 0
At the moment, I can think of looping through each FOUR_MONTHS, but I am thinking that it won't be efficient, and that there might be a simpler way or that I might have just missed a very simple Ruby method.
P.S. I'm tagging this question with ruby-on-rails because ActiveSupport::TimeZone possibly has differences with implementation.
This should work fine with Rails (end_of_month is needed) :
def month_id(date)
date.month + date.year * 12
end
def same_day?(date1, date2)
(last_day_of_month?(date1) && date2.day >= date1.day) ||
(last_day_of_month?(date2) && date1.day >= date2.day) ||
date1.day == date2.day
end
def last_day_of_month?(date)
date == date.end_of_month
end
def separated_by_multiple_of_x_months?(date1, date2, x = 4)
same_day?(date1, date2) && (month_id(date1) - month_id(date2)) % x == 0
end
separated_by_multiple_of_x_months?(Date.parse('2017-01-15'), Date.parse('2016-09-15'))
#=> true
separated_by_multiple_of_x_months?(Date.parse('2017-01-15'), Date.parse('2016-09-14'))
#=> false
separated_by_multiple_of_x_months?(Date.parse('2017-01-15'), Date.parse('2016-05-15'))
#=> true
separated_by_multiple_of_x_months?(Date.parse('2017-01-31'), Date.parse('2016-09-30'))
#=> true
separated_by_multiple_of_x_months?(Date.parse('2016-11-30'), Date.parse('2016-07-31'))
#=> true
separated_by_multiple_of_x_months?(Date.parse('2016-11-30'), Date.parse('2016-07-30'))
#=> true
You could try something like (end_date - start_date % x * (30.days / 1.day)).to_i.zero?
If you do a to_i you'll get the difference in days:
(Date.parse('2017-01-15') - Date.parse('2016-09-15')).to_i
OK 2nd try. You could use the months_ago or months_since method of ActiveSupport:
Assuming date1 is smaller than date2:
def months_apart?(date1, date2, num_of_months)
date2.months_ago(num_of_months) == date1
end
I had requirement to display the data of this month (between month starting to ending data )
I know how to do in MySQL below query
enter code here select #MonthAmount := IFNULL(sum(AmountReceived), 0.0) as TotoalAmountperMonth
from collection
where date_time between DATE_FORMAT(NOW() ,'%Y-%m-01')
and LAST_DAY(now() - interval 0 month ) and AgentID=v_agent) as monthamount
but how to do using entity (lambda expression) I am new to entity when I google I got to get the data of today but in month?
below query got the result of today data
enter code here var newAuctionsResults = repo.FindAllAuctions()
.Where(a => a.IsActive == true
|| (a.StartTime.Value.Year == todayYear
&& a.StartTime.Value.Month == todayMonth
&& a.StartTime.Value.Day == todayDay))
.ToList();
Try
DateTime date = DateTime.Today;
var newAuctionsResults = repo.FindAllAuctions()
.Where(a => a.StartTime.Value.Year == date.Year
&& a.StartTime.Value.Month == date.Month)
I have a Grails domain object with a startDate and endDate property.
What's the best way to find all those objects where the range [startDate, endDate] overlaps with a specified date range? I know how to do this in SQL but wonder if there's any Grails/GORM magic to do it more succinctly.
Also, the endDate is an optional property.
The SQL / JPQL query would be something like
from MyObject obj where obj.startDate <= ?1 and (obj.endDate is null OR obj.endDate >= ?2)
Two Ways to embrace Grails/GORM in this case:
Lazy:-
def today = new Date()
def query = MyObject.where {
startDate <= (today - 10) && (endDate == null || endDate >= today + 10)
}
def listOfMyObjects = query.list()
Eager:-
def today = new Date()
def listOfMyObjects = MyObject.findAll {//or find{} if you need the first occurance
startDate <= (today - 10) && (endDate == null || endDate >= today + 10)
}
I have list of days (from monday till sunday).
When a user selects monday till friday or saturday and sunday I want my app to show it's hyponym (weekdays/weekends).
Does iOS provide some default functionality for this (If i'm not mistaken the iOS Alarm app does the same) or do I have to write this functionality myself?
Provided that you have a NSArray with the selected days as numbers, named selectedDays, and that 0 and 6 are weekend days:
NSString *name = nil; // Fill this with the default values (concatenate all names of selected days)
bool days[] = {0,0,0,0,0,0,0};
for (NSNumber *day in selectedDays)
{
int d = [day intValue];
if (d >= 0 && d <= 6) days[d] = true;
}
if ( days[0] == true
&& days[1] == false
&& days[2] == false
&& days[3] == false
&& days[4] == false
&& days[5] == false
&& days[6] == true)
{
name = #"Weekend";
}
if ( days[0] == false
&& days[1] == true
&& days[2] == true
&& days[3] == true
&& days[4] == true
&& days[5] == true
&& days[6] == false)
{
name = #"Weekdays";
}