一个简单的新闻管理系统

首先是主函数,其次是各个类函数,有三个类:News、Channel、ChannelManagment

=========================================================

主函数:

#import "News.h"

#import "Channel.h"

#import "ChannelManagement.h"

int main(int argc, const char * argv[]) {

@autoreleasepool {

//新闻对象

News

*ne1 = [[News alloc]initWithTitle:@"kill" link:[NSURL

URLWithString:@"www.baidu.com"] description:@"a people killed another

one!"];

News *ne2 = [[News alloc]initWithTitle:@"number" link:[NSURL URLWithString:@"www.number.com"] description:@"this is a number!"];

News *ne3 = [[News alloc]initWithTitle:@"hello" link:[NSURL URLWithString:@"www.hello.com"] description:@"helloworld!"];

NSLog(@"%@",ne1);

//对新闻进行输出

NSLog(@"%@",ne2);

//频道对象

Channel *ch1 = [[Channel alloc]initWithSubject:@"NEWS" link:[NSURL URLWithString:@"www.imau.edu.cn"]];

//添加新闻到频道里

[ch1 addNews:ne1];

[ch1 addNews:ne2];

[ch1 addNews:ne3];

Channel *ch2 = [[Channel alloc]initWithSubject:@"READ" link:[NSURL URLWithString:@"www.read.cn"]];

[ch2 addNews:ne1];

[ch2 addNews:ne2];

Channel *ch3 = [[Channel alloc]initWithSubject:@"MUSIC" link:[NSURL URLWithString:@"www.music.cn"]];

[ch3 addNews:ne1];

[ch3 addNews:ne2];

//NSLog(@"%@",ch1);

//频道管理

ChannelManagement * chma = [[ChannelManagement alloc]initWithFileName:@"News.txt"];

//添加函数 字典中添加新闻和频道

[chma addNews:ne1 andChannel:ch1];

[chma addNews:ne2 andChannel:ch2];

[chma addNews:ne2 andChannel:ch3];

NSLog(@"%@",chma);

NSLog(@"channelCount : %lu",[chma channelCount]);

NSInteger i = 1;

//查找字典中指定元素

NSLog(@"channelAtIndex %ld : %@",i,[chma channelAtIndex:i]);

//移除字典中指定元素

[chma removeChannels:i];

NSLog(@"%@",chma);

//移除频道下面的指定元素

[chma removeNews:i channel:ch2];

NSLog(@"%@",chma);

//查找频道下面指定的元素

NSLog(@"newsAtIndex %ld in channel %@ is: %@",i,@"ch2",[chma newsAtIndex:i channel:ch2]);

//计算字典中频道下指定的元素

NSLog(@"channel %@ count is : %ld",@"cha2",[chma newCountAtChannel:ch2]);

//保存函数 把字典中的成员写入文件

[chma write];

//还原函数 把字典中的成员读取出来

[chma read];

}

return 0;

}

=========================================================

News.h文件:

#import@interface News : NSObject<NSCopying,NSCoding>

{

NSString * title;  //新闻标题

NSURL * link;    //新闻链接

NSString * description; //新闻内容

}

@property(nonatomic,copy) NSString * title;

@property(nonatomic,retain) NSURL * link;

@property(nonatomic,copy)NSString * description;

-(id)copyWithZone:(NSZone *)zone;

//对数据成员进行初始化

-(id)initWithTitle:(NSString *)_title link:(NSURL *)_link description:(NSString *)_description;

//打印函数,输出新闻的头、链接、内容

-(NSString *)description;

@end

=========================================================

News.m 文件:

#import "News.h"

@implementation News

@synthesize title,link,description;

-(id)copyWithZone:(NSZone *)zone

{

News * n = [[[self class]allocWithZone:zone]init];

[n setTitle:title];

[n setLink:link];

[n setDescription:description];

return n;

}

//对数据成员进行初始化

-(id)initWithTitle:(NSString *)_title link:(NSURL *)_link description:(NSString *)_description

{

if (self = [super init]) {

title = _title;

link = _link;

description = _description;

}

return self;

}

//归档

-(void)encodeWithCoder:(NSCoder *)aCoder

{

[aCoder encodeObject:title forKey:@"TITLE"];

[aCoder encodeObject:link forKey:@"LINK"];

[aCoder encodeObject:description forKey:@"DESCRIPTION"];

}

//解档

-(id)initWithCoder:(NSCoder *)aDecoder

{

if (self = [super init]) {

title = [aDecoder decodeObjectForKey:@"TITLE"];

link = [aDecoder decodeObjectForKey:@"LINK"];

description = [aDecoder decodeObjectForKey:@"DESCRIPTION"];

}

return self;

}

//打印函数,输出新闻的头、链接、内容

-(NSString *)description

{

NSString * str = [NSString stringWithFormat:@"News==> title:%@,link:%@,description:%@",title,link,description];

return str;

}

@end

=========================================================

Chann.h 文件:

#import#import "News.h"

@interface Channel : NSObject<NSCopying,NSCoding>

{

NSString  *subject;        //频道标题

NSURL *link;                //频道链接

NSMutableArray * arraylist;  //频道中新闻列表

}

@property(nonatomic,copy) NSString * subject;

@property(nonatomic,retain) NSURL * link;

@property(nonatomic,retain) NSMutableArray * arraylist;

-(id)copyWithZone:(NSZone *)zone;

//初始化数据成员

-(id)initWithSubject:(NSString *)_subject link:(NSURL *)_link;

//添加新闻

-(void)addNews:(News *)_news;

//移除指定的下标元素

-(void)removeNews:(NSInteger)index;

//计算数组元素个数

-(NSInteger)newsCount;

//打印函数 输出标题、链接、数组

-(NSString *)description;

@end

=========================================================

Channel.m文件:

#import "Channel.h"

@implementation Channel

@synthesize subject,link,arraylist;

-(id)copyWithZone:(NSZone *)zone

{

Channel *ch = [[[self class]allocWithZone:zone]init];

[ch setSubject:subject];

[ch setLink:link];

[ch setArraylist:arraylist];

return ch;

}

//归档

-(void)encodeWithCoder:(NSCoder *)aCoder

{

[aCoder encodeObject:subject forKey:@"SUBJECT"];

[aCoder encodeObject:link forKey:@"LINK"];

[aCoder encodeObject:arraylist forKey:@"ARRAYLIST"];

}

//解档

-(id)initWithCoder:(NSCoder *)aDecoder

{

if (self = [super init]) {

subject = [aDecoder decodeObjectForKey:@"SUBJECT"];

link = [aDecoder decodeObjectForKey:@"LINK"];

arraylist = [aDecoder decodeObjectForKey:@"ARRAYLIST"];

}

return self;

}

//初始化数据成员

-(id)initWithSubject:(NSString *)_subject link:(NSURL *) _link

{

if (self = [super init]) {

subject = _subject;

link = _link;

arraylist = [[NSMutableArray alloc] initWithCapacity:1];

}

return self;

}

//添加新闻

-(void)addNews:(News *)_news

{

BOOL flag = YES;

for (id objc in arraylist)

{

if (_news == objc)

{

NSLog(@"News exist!!");

flag = NO;

}

}

if (flag)

{

[arraylist addObject:_news];

}

}

//移除指定的下标元素

-(void)removeNews:(NSInteger)index

{

[arraylist removeObjectAtIndex:index];

}

//计算数组元素个数

-(NSInteger)newsCount

{

return [arraylist count];

}

//打印函数 输出标题、链接、数组

-(NSString *)description

{

NSString *str = [NSString stringWithFormat:@"Subject:%@,Link:%@,Arraylist:%@",subject,link,arraylist];

return  str;

}

@end

=========================================================

ChannelManagement.h 文件:

#import#import "Channel.h"

@interface ChannelManagement : NSObject

{

NSMutableDictionary *channels; //频道管理字典

NSString *filename;            //文件名

}

@property(nonatomic,retain) NSMutableDictionary *channels;

@property(nonatomic,retain) NSString *filename;

//始化数据成员

-(id)initWithFileName:(NSString *)_filename;

//添加一个字典对象

-(void)addChannels:(NSDictionary *)_channel;

//移除字典中指定下标的元素

-(void)removeChannels:(NSInteger)index;

//查找字典中指定下标的元素

-(Channel*)channelAtIndex:(NSInteger)index;

//计算字典里元素的个数

-(NSInteger)channelCount;

//添加函数 字典中添加新闻和频道

-(void)addNews:(News *)_news andChannel:(Channel *)_channel;

//移除频道下面的指定元素

-(void)removeNews:(NSInteger)index channel:(Channel *)_channel;

//查找频道下面指定的元素

-(News*)newsAtIndex:(NSInteger)index channel:(Channel *)_channel;

//计算字典中频道下指定的元素

-(NSInteger)newCountAtChannel: (Channel*)_channel;

//保存函数 把字典中的成员写入文件

-(void) write;

//还原函数 把字典中的成员读取出来

-(void) read;

//打印函数 输出频道、存放数据的文件名

-(NSString *)description;

@end

=========================================================

ChannelManagement.m 文件:

#import "ChannelManagement.h"

@implementation ChannelManagement

@synthesize channels,filename;

//初始化数据成员

-(id)initWithFileName:(NSString *)_filename

{

if(self = [super init])

{

filename = _filename;

channels = [[NSMutableDictionary alloc]initWithCapacity:2];

}

return self;

}

//添加一个字典对象

-(void)addChannels:(NSDictionary *)_channel

{

//    [channels setObject:_channel.subject forKey:@"channel.subject"];

//    [channels setObject:_channel.link forKey:@"channel.link"];

//    [channels setObject:_channel.arraylist forKey:@"channel.arraylist"];

[channels addEntriesFromDictionary:_channel];

}

//移除字典中指定下标的元素

-(void)removeChannels:(NSInteger)index

{

NSArray *array = [channels allKeys];

NSString *str = [array objectAtIndex:index];

[channels removeObjectForKey:str];

}

//查找字典中指定下标的元素

-(Channel*)channelAtIndex:(NSInteger)index

{

NSArray *array = [channels allKeys];

NSString *str = [array objectAtIndex:index];

Channel * ch = [channels objectForKey:str];

return ch;

}

//计算字典里元素的个数

-(NSInteger)channelCount

{

return [channels count];

}

//添加函数 字典中添加新闻和频道

-(void)addNews:(News *)_news andChannel:(Channel *)_channel

{

//    for (id objc in _channel.arraylist)

//    {

//        if (_news == objc)

//        {

//            NSLog(@"%@ have exist this news",_channel.subject);

//        }

//    }

[_channel addNews:_news];

NSString *sub = _channel.subject;

[channels setObject:_channel forKey:sub];

}

//移除频道下面的指定元素

-(void)removeNews:(NSInteger)index channel:(Channel *)_channel

{

[_channel.arraylist removeObjectAtIndex:index];

}

//查找频道下面指定的元素

-(News*)newsAtIndex:(NSInteger)index channel:(Channel *)_channel

{

News *ne = [_channel.arraylist objectAtIndex:index];

return ne;

}

//计算字典中频道下指定的元素

-(NSInteger)newCountAtChannel: (Channel*)_channel

{

NSInteger count = [_channel.arraylist count];

return count;

}

//保存函数 把字典中的成员写入文件

-(void)write

{

BOOL result = [NSKeyedArchiver archiveRootObject:channels toFile:filename];

if (result)

{

NSLog(@"write success!");

}

else

{

NSLog(@"write fail!");

}

}

//还原函数 把字典中的成员读取出来

-(void)read

{

NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];

NSString *str = [NSString stringWithFormat:@"%@",array];

//NSLog(@"Content of %@ is : %@",filename,array);

NSLog(@"%@",str);

}

//打印函数 输出频道、存放数据的文件名

-(NSString *)description

{

NSLog(@"**************************频道管理***************************");

NSString *str = [NSString stringWithFormat:@"filename:%@",filename];

NSLog(@"***********************************************************");

NSArray *array = [channels allKeys];

for (id element in array ) {

NSLog(@"CHANNEL:%@ = %@",element,[channels objectForKey:element]);

}

return str;

}

@end

======================================

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,888评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,677评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,386评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,726评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,729评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,337评论 1 310
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,902评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,807评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,349评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,439评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,567评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,242评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,933评论 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,420评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,531评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,995评论 3 377
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,585评论 2 359

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,702评论 18 139
  • 在项目之前,最好下载该App或者GitHub源码跑一下看一下效果,该项目旨在练习UI及网络数据的处理,推荐初学者边...
    si1ence阅读 3,336评论 13 35
  • 这些天总是有一个画面不停地在头脑里播放,像是自己手摇着电影放映机一遍遍的在回忆,没有特别清晰的声音却能够感...
    波王爷阅读 505评论 0 0
  • 01. 小表弟是2007年秋天入伍的。 都说秋高气爽,天凉好个秋。可这完全不适用与广州的秋。广州的秋天更像是夏天,...
    C姑娘的糖果屋阅读 424评论 1 3
  • 有人问想要剃毛,但是不知道剃了会不会有什么感染,或者是有人疑惑到底yin毛有什么用处等等的问题。似乎这样的困扰让女...
    冷喵阅读 2,184评论 0 1