从另一种方法中获取价值

时间:2014-04-19 02:13:41

标签: objective-c

我正在尝试使用我放在同一个类但使用不同方法的数据(我相信这是术语),但由于某些奇怪的原因,我似乎无法访问这些数据。当我将它们放在一起(在计时器中)时,我可以这样做,但是这会消耗内存的TONNE,因为它会一遍又一遍地设置这些数据。无论如何,到目前为止这是我的代码:

 /////////////////////////////////SET TIME/DAYS///////////////////
    currentTime = [NSDate date];
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
    [timeFormatter setTimeStyle:NSDateFormatterShortStyle];
    self.timeNow.text = [timeFormatter stringFromDate:currentTime];

    NSDate *lesson1Start = [timeFormatter dateFromString:@"8:40"];
    NSDate *lesson2Start = [timeFormatter dateFromString:@"9:35"];
    NSDate *recessStart = [timeFormatter dateFromString:@"10:30"];
    NSDate *caregroupStart = [timeFormatter dateFromString:@"10:50"];
    NSDate *lesson3Start = [timeFormatter dateFromString:@"11:00"];
    NSDate *lesson4Start = [timeFormatter dateFromString:@"11:55"];
    NSDate *lunchStart = [timeFormatter dateFromString:@"12:50"];
    NSDate *lesson5Start = [timeFormatter dateFromString:@"1:30"];
    NSDate *lesson6Start = [timeFormatter dateFromString:@"2:20"];
    NSDate *endDay = [timeFormatter dateFromString:@"3:10"];

    }
    //////////////////////////END SET TIMES/////////////////////////


- (void)updateTime {

    NSString *mondayDay = (@"Monday");
    NSString *tuesdayDay = (@"Tuesday");
    NSString *wednesdayDay = (@"Wednesday");
    NSString *thursdayDay = (@"Thursday");
    NSString *fridayDay = (@"Friday");

    [updateTimer invalidate];
    updateTimer = nil;

    currentTime = [NSDate date];
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
    [timeFormatter setTimeStyle:NSDateFormatterShortStyle];
    self.timeNow.text = [timeFormatter stringFromDate:currentTime];

    updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01
                            target:self
                            selector:@selector(updateTime)
                            userInfo:Nil repeats:YES];

   ///////Check day/time & update lesson/////

    if (self.dayNow.text == mondayDay && self.timeNow.text == lesson1Start) {
        NSLog(@"");
    }

正如你所看到的,我不能使用" lesson1Start"因为它不在- (void)updateTime

如果我把所有这些都捆绑在一起就会没问题,因为它会将这些数据反复设置,因为它被放置在计时器中。

PS:我正在尝试检查课程何时开始以及课程开始的时间是否会出现课程。在这种情况下,我只有NSLog(@"")

1 个答案:

答案 0 :(得分:0)

你可以这样做...... 让我们考虑一下你的代码是ClassRoom;

@interface ClassRoom 
{
NSArray *scheduleArray; //instance variable
}

你可以创建一个NSArray *scheduleArray;来保存//你必须在实现文件

中分配和初始化它
NSDate *lesson1Start = [timeFormatter dateFromString:@"8:40"];
NSDate *lesson2Start = [timeFormatter dateFromString:@"9:35"];
NSDate *recessStart = [timeFormatter dateFromString:@"10:30"];
NSDate *caregroupStart = [timeFormatter dateFromString:@"10:50"];
NSDate *lesson3Start = [timeFormatter dateFromString:@"11:00"];
NSDate *lesson4Start = [timeFormatter dateFromString:@"11:55"];
NSDate *lunchStart = [timeFormatter dateFromString:@"12:50"];
NSDate *lesson5Start = [timeFormatter dateFromString:@"1:30"];
NSDate *lesson6Start = [timeFormatter dateFromString:@"2:20"];
NSDate *endDay = [timeFormatter dateFromString:@"3:10"];

在scheduleArray中添加以上所有日期。(希望您知道如何在NSArray中添加项目。)

现在您可以将updateTime写为

    - (void)updateTime {

        NSString *mondayDay = (@"Monday");
        NSString *tuesdayDay = (@"Tuesday");
        NSString *wednesdayDay = (@"Wednesday");
        NSString *thursdayDay = (@"Thursday");
        NSString *fridayDay = (@"Friday");

        [updateTimer invalidate];
        updateTimer = nil;

        currentTime = [NSDate date];
        NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
        [timeFormatter setTimeStyle:NSDateFormatterShortStyle];
        self.timeNow.text = [timeFormatter stringFromDate:currentTime];

        updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01
                                target:self
                                selector:@selector(updateTime)
                                userInfo:Nil repeats:YES];

       ///////Check day/time & update lesson/////

for(NSDate * date in scheduleArray){

  here you will get above saved dates use them to your need.
}

}

希望它有所帮助!