【非凡程序员】闹钟

今天学了简单的闹钟设置,人设置某个时间闹铃开始响,人主动让闹钟停止(闹钟响铃次数随机),运行代码如下:

//
//  Person.h
//  闹钟
//
//  Created by 非凡程序员 on 15/6/3.
//  Copyright (c) 2015年 wzhen. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Agreement.h"

@interface Person : NSObject<getUp>

@property(nonatomic,strong)id<getUp> delegate;

- (void) settime;
- (void) up:(int) ringtimes;
@property(nonatomic,assign)int stoptime;

@end
//
//  Person.m
//  闹钟
//
//  Created by 非凡程序员 on 15/6/3.
//  Copyright (c) 2015年 wzhen. All rights reserved.
//

#import "Person.h"
#import "Clock.h"
#import "Agreement.h"
@implementation Person
- (id) init
{
    self=[super init];
    if (self) {
        _stoptime=arc4random()%7;
        _stoptime+=3;
    }
    return self;
}

- (void) settime
{
    NSLog(@"人定了闹钟,闹钟响了%i次,人才起床",_stoptime);
    [_delegate reveille];
}
- (void) up:(int) ringtimes
{
    if (ringtimes==_stoptime) {
        NSLog(@"人想:怎么响个不停,关了你,看你还响不响!");
        [_delegate stop];
    }
    else {
        NSLog(@"人想:我要再睡一会,不想起床!");
    }
}

- (void) dealloc
{
    NSLog(@"人对象被释放");
}


@end
//
//  Person.m
//  闹钟
//
//  Created by 非凡程序员 on 15/6/3.
//  Copyright (c) 2015年 wzhen. All rights reserved.
//

#import "Person.h"
#import "Clock.h"
#import "Agreement.h"
@implementation Person
- (id) init
{
    self=[super init];
    if (self) {
        _stoptime=arc4random()%7;
        _stoptime+=3;
    }
    return self;
}

- (void) settime
{
    NSLog(@"人定了闹钟,闹钟响了%i次,人才起床",_stoptime);
    [_delegate reveille];
}
- (void) up:(int) ringtimes
{
    if (ringtimes==_stoptime) {
        NSLog(@"人想:怎么响个不停,关了你,看你还响不响!");
        [_delegate stop];
    }
    else {
        NSLog(@"人想:我要再睡一会,不想起床!");
    }
}

- (void) dealloc
{
    NSLog(@"人对象被释放");
}


@end
//
//  Person.m
//  闹钟
//
//  Created by 非凡程序员 on 15/6/3.
//  Copyright (c) 2015年 wzhen. All rights reserved.
//

#import "Person.h"
#import "Clock.h"
#import "Agreement.h"
@implementation Person
- (id) init
{
    self=[super init];
    if (self) {
        _stoptime=arc4random()%7;
        _stoptime+=3;
    }
    return self;
}

- (void) settime
{
    NSLog(@"人定了闹钟,闹钟响了%i次,人才起床",_stoptime);
    [_delegate reveille];
}
- (void) up:(int) ringtimes
{
    if (ringtimes==_stoptime) {
        NSLog(@"人想:怎么响个不停,关了你,看你还响不响!");
        [_delegate stop];
    }
    else {
        NSLog(@"人想:我要再睡一会,不想起床!");
    }
}

- (void) dealloc
{
    NSLog(@"人对象被释放");
}


@end
//
//  Clock.h
//  闹钟
//
//  Created by 非凡程序员 on 15/6/3.
//  Copyright (c) 2015年 wzhen. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Agreement.h"
@class Person;
@interface Clock : NSObject <getUp>

@property(nonatomic,retain)NSTimer * timer;
@property(nonatomic,assign)int timerCount;
@property(nonatomic,weak)Person *person;


@end
//
//  Clock.m
//  闹钟
//
//  Created by 非凡程序员 on 15/6/3.
//  Copyright (c) 2015年 wzhen. All rights reserved.
//

#import "Clock.h"
#import "Agreement.h"
#import "Person.h"

@implementation Clock

- (id) init
{
    self=[super init];
    if (self) {
        _timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(reveille) userInfo:nil repeats:YES];
    }
    return self;
}
- (void) reveille
{
    for (int i=1; i<=100; i++) {
        NSDate *today=[NSDate date];
        NSDateFormatter *format=[NSDateFormatter new];
        [format setDateFormat:@"当前时间为:yyyy年MM月dd日 HH时mm分ss秒"];
        NSString *now=[format stringFromDate:today];
        if (i<=1) {
            if ( [now compare:@"当前时间为:2015年06月03日 19时24分"]==NSOrderedDescending) {
                _timerCount++;
                [_person up:_timerCount];
                NSLog(@"闹钟响了第%i次:'啦啦啦,啦啦啦,我是卖报的小画家'%@",_timerCount,now);
            }
            else{
                NSLog(@"闹钟%@",now);
            }
            
        }
    }
}
- (void) stop
{
    [_timer invalidate];
}
- (void) dealloc
{
    NSLog(@"闹钟对象被释放");
}

@end
//
//  Agreement.h
//  闹钟
//
//  Created by 非凡程序员 on 15/6/3.
//  Copyright (c) 2015年 wzhen. All rights reserved.
//

#import <Foundation/Foundation.h>



@protocol getUp <NSObject>

@required



@optional
- (void) reveille;
- (void) stop;
@property(nonatomic,assign)int stoptime;


@end
//
//  main.m
//  闹钟
//
//  Created by 非凡程序员 on 15/6/3.
//  Copyright (c) 2015年 wzhen. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Clock.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        
        
        Person *person=[[Person alloc]init];
        Clock *clock=[[Clock alloc]init];
        
        person.delegate=clock;
        clock.person=person;
        [person settime];
        
        [[NSRunLoop currentRunLoop]run];
        
        NSLog(@"Hello, World!");
        
        
        
    }
    return 0;
}

运行结果如下:

2015-06-03 19:23:49.885 闹钟[3126:1614512] 人定了闹钟,闹钟响了6次,人才起床
2015-06-03 19:23:49.891 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分49秒
2015-06-03 19:23:50.886 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分50秒
2015-06-03 19:23:51.890 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分51秒
2015-06-03 19:23:52.890 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分52秒
2015-06-03 19:23:53.887 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分53秒
2015-06-03 19:23:54.890 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分54秒
2015-06-03 19:23:55.886 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分55秒
2015-06-03 19:23:56.886 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分56秒
2015-06-03 19:23:57.887 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分57秒
2015-06-03 19:23:58.885 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分58秒
2015-06-03 19:23:59.887 闹钟[3126:1614512] 闹钟当前时间为:2015年06月03日 19时23分59秒
2015-06-03 19:24:00.886 闹钟[3126:1614512] 人想:我要再睡一会,不想起床!
2015-06-03 19:24:00.887 闹钟[3126:1614512] 闹钟响了第1次:'啦啦啦,啦啦啦,我是卖报的小画家'当前时间为:2015年06月03日 19时24分00秒
2015-06-03 19:24:01.891 闹钟[3126:1614512] 人想:我要再睡一会,不想起床!
2015-06-03 19:24:01.891 闹钟[3126:1614512] 闹钟响了第2次:'啦啦啦,啦啦啦,我是卖报的小画家'当前时间为:2015年06月03日 19时24分01秒
2015-06-03 19:24:02.891 闹钟[3126:1614512] 人想:我要再睡一会,不想起床!
2015-06-03 19:24:02.891 闹钟[3126:1614512] 闹钟响了第3次:'啦啦啦,啦啦啦,我是卖报的小画家'当前时间为:2015年06月03日 19时24分02秒
2015-06-03 19:24:03.891 闹钟[3126:1614512] 人想:我要再睡一会,不想起床!
2015-06-03 19:24:03.891 闹钟[3126:1614512] 闹钟响了第4次:'啦啦啦,啦啦啦,我是卖报的小画家'当前时间为:2015年06月03日 19时24分03秒
2015-06-03 19:24:04.886 闹钟[3126:1614512] 人想:我要再睡一会,不想起床!
2015-06-03 19:24:04.886 闹钟[3126:1614512] 闹钟响了第5次:'啦啦啦,啦啦啦,我是卖报的小画家'当前时间为:2015年06月03日 19时24分04秒
2015-06-03 19:24:05.891 闹钟[3126:1614512] 人想:怎么响个不停,关了你,看你还响不响!
2015-06-03 19:24:05.891 闹钟[3126:1614512] 闹钟响了第6次:'啦啦啦,啦啦啦,我是卖报的小画家'当前时间为:2015年06月03日 19时24分05秒


你可能感兴趣的:(闹铃)