【非凡プログラマー】目覚まし時計
7014 ワード
今日は簡単な目覚まし時計の設定を学びました.ある時間に目覚まし時計が鳴り始め、自分から目覚まし時計を停止させます(目覚まし時計の鳴動回数はランダムです).実行コードは以下の通りです.
実行結果は次のとおりです.
//
// 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