美文网首页IT经验交流iOS学习笔记
一个简单的新闻管理系统

一个简单的新闻管理系统

作者: Snail_cz | 来源:发表于2016-03-23 19:59 被阅读496次

首先是主函数,其次是各个类函数,有三个类: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

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

相关文章

网友评论

    本文标题:一个简单的新闻管理系统

    本文链接:https://www.haomeiwen.com/subject/cttrlttx.html