objective-c随机数+日期格式显示一例

objective-c随机数+日期格式显示一例,第1张

概述在原来的代码上有修改,主要为: 将准备随机数方法放到了init中,这样不用手动调用了 setWeek方法已经过时,使用的是setWeekOfYear方法 在此放一份以备以后查找: le.h //// LotteryEntry.h// lottery//// Created by kinds on 15/6/25.// Copyright (c) 2015年 hopy. All right

在原来的代码上有修改,主要为:

将准备随机数方法放到了init中,这样不用手动调用了 setWeek方法已经过时,使用的是setWeekOfYear方法

在此放一份以备以后查找:

le.h

//// LotteryEntry.h// lottery//// Created by kinds on 15/6/25.// copyright (c) 2015年 hopy. All rights reserved.//#import <Foundation/Foundation.h>@interface LotteryEntry : NSObject{    NSDate *entry_date;    int first_number;    int second_number;}-(voID)prepareRandomNumbers;-(voID)setEntryDate:(NSDate*)date;-(NSDate *)entryDate;-(int)firstNumber;-(int)secondNumber;@end

le.m

//// LotteryEntry.m// lottery//// Created by kinds on 15/6/25.// copyright (c) 2015年 hopy. All rights reserved.//#import "LotteryEntry.h"@implementation LotteryEntry-(ID)init{    self = [super init];    if(self){        [self prepareRandomNumbers];    }    return self;}-(voID)prepareRandomNumbers{    first_number = ((int)random()%100) + 1;    second_number = ((int)random()%100) + 1;}-(voID)setEntryDate:(NSDate *)date{    entry_date = date;}-(NSDate*)entryDate{    return entry_date;}-(int)firstNumber{    return first_number;}-(int)secondNumber{    return second_number;}-(Nsstring *)description{    NSDateFormatter *df = [NSDateFormatter new];    [df setTimeStyle:NSDateFormatterShortStyle];    [df setDateStyle:NSDateFormatterMediumStyle];    return [[Nsstring alloc]initWithFormat:@"%@ = %d and %d",[df stringFromDate:entry_date],first_number,second_number];}@end

main.m

//// main.m// lottery//// Created by kinds on 15/6/24.// copyright (c) 2015年 hopy. All rights reserved.//#import <Foundation/Foundation.h>#import "LotteryEntry.h"int main(int argc,const char * argv[]) {    @autoreleasepool {        NSDate *Now = [NSDate new];        NSCalendar *cal = [NSCalendar currentCalendar];        NSDateComponents *to_week = [NSDateComponents new];        srandom((unsigned)time(NulL));        NSMutableArray *mary = [NSMutableArray new];        for(int i = 0;i<10;i++){            [to_week setWeekOfYear:i];            NSDate *from_Now = [cal dateByAddingComponents:to_week toDate:Now                                                   options:0];            LotteryEntry *le = [LotteryEntry new];            //[le prepareRandomNumbers];            [le setEntryDate:from_Now];            [mary addobject:le];        }        for(LotteryEntry *le in mary)            NSLog(@"%@",le);    }    return 0;}
总结

以上是内存溢出为你收集整理的objective-c随机数+日期格式显示一例全部内容,希望文章能够帮你解决objective-c随机数+日期格式显示一例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1051683.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存