首先是主函數(shù)迄埃,其次是各個類函數(shù),有三個類:News、Channel畔规、ChannelManagment
=========================================================
主函數(shù):
#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);
//對新聞進(jìn)行輸出
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"];
//添加函數(shù) 字典中添加新聞和頻道
[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]);
//計(jì)算字典中頻道下指定的元素
NSLog(@"channel %@ count is : %ld",@"cha2",[chma newCountAtChannel:ch2]);
//保存函數(shù) 把字典中的成員寫入文件
[chma write];
//還原函數(shù) 把字典中的成員讀取出來
[chma read];
}
return 0;
}
=========================================================
News.h文件:
#import@interface News : NSObject<NSCopying,NSCoding>
{
NSString * title;? //新聞標(biāo)題
NSURL * link;? ? //新聞鏈接
NSString * description; //新聞內(nèi)容
}
@property(nonatomic,copy) NSString * title;
@property(nonatomic,retain) NSURL * link;
@property(nonatomic,copy)NSString * description;
-(id)copyWithZone:(NSZone *)zone;
//對數(shù)據(jù)成員進(jìn)行初始化
-(id)initWithTitle:(NSString *)_title link:(NSURL *)_link description:(NSString *)_description;
//打印函數(shù)伏穆,輸出新聞的頭拘泞、鏈接、內(nèi)容
-(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;
}
//對數(shù)據(jù)成員進(jì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;
}
//打印函數(shù)枕扫,輸出新聞的頭陪腌、鏈接、內(nèi)容
-(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;? ? ? ? //頻道標(biāo)題
NSURL *link;? ? ? ? ? ? ? ? //頻道鏈接
NSMutableArray * arraylist;? //頻道中新聞列表
}
@property(nonatomic,copy) NSString * subject;
@property(nonatomic,retain) NSURL * link;
@property(nonatomic,retain) NSMutableArray * arraylist;
-(id)copyWithZone:(NSZone *)zone;
//初始化數(shù)據(jù)成員
-(id)initWithSubject:(NSString *)_subject link:(NSURL *)_link;
//添加新聞
-(void)addNews:(News *)_news;
//移除指定的下標(biāo)元素
-(void)removeNews:(NSInteger)index;
//計(jì)算數(shù)組元素個數(shù)
-(NSInteger)newsCount;
//打印函數(shù) 輸出標(biāo)題烟瞧、鏈接诗鸭、數(shù)組
-(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;
}
//初始化數(shù)據(jù)成員
-(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];
}
}
//移除指定的下標(biāo)元素
-(void)removeNews:(NSInteger)index
{
[arraylist removeObjectAtIndex:index];
}
//計(jì)算數(shù)組元素個數(shù)
-(NSInteger)newsCount
{
return [arraylist count];
}
//打印函數(shù) 輸出標(biāo)題强岸、鏈接、數(shù)組
-(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;
//始化數(shù)據(jù)成員
-(id)initWithFileName:(NSString *)_filename;
//添加一個字典對象
-(void)addChannels:(NSDictionary *)_channel;
//移除字典中指定下標(biāo)的元素
-(void)removeChannels:(NSInteger)index;
//查找字典中指定下標(biāo)的元素
-(Channel*)channelAtIndex:(NSInteger)index;
//計(jì)算字典里元素的個數(shù)
-(NSInteger)channelCount;
//添加函數(shù) 字典中添加新聞和頻道
-(void)addNews:(News *)_news andChannel:(Channel *)_channel;
//移除頻道下面的指定元素
-(void)removeNews:(NSInteger)index channel:(Channel *)_channel;
//查找頻道下面指定的元素
-(News*)newsAtIndex:(NSInteger)index channel:(Channel *)_channel;
//計(jì)算字典中頻道下指定的元素
-(NSInteger)newCountAtChannel: (Channel*)_channel;
//保存函數(shù) 把字典中的成員寫入文件
-(void) write;
//還原函數(shù) 把字典中的成員讀取出來
-(void) read;
//打印函數(shù) 輸出頻道砾赔、存放數(shù)據(jù)的文件名
-(NSString *)description;
@end
=========================================================
ChannelManagement.m 文件:
#import "ChannelManagement.h"
@implementation ChannelManagement
@synthesize channels,filename;
//初始化數(shù)據(jù)成員
-(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];
}
//移除字典中指定下標(biāo)的元素
-(void)removeChannels:(NSInteger)index
{
NSArray *array = [channels allKeys];
NSString *str = [array objectAtIndex:index];
[channels removeObjectForKey:str];
}
//查找字典中指定下標(biāo)的元素
-(Channel*)channelAtIndex:(NSInteger)index
{
NSArray *array = [channels allKeys];
NSString *str = [array objectAtIndex:index];
Channel * ch = [channels objectForKey:str];
return ch;
}
//計(jì)算字典里元素的個數(shù)
-(NSInteger)channelCount
{
return [channels count];
}
//添加函數(shù) 字典中添加新聞和頻道
-(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;
}
//計(jì)算字典中頻道下指定的元素
-(NSInteger)newCountAtChannel: (Channel*)_channel
{
NSInteger count = [_channel.arraylist count];
return count;
}
//保存函數(shù) 把字典中的成員寫入文件
-(void)write
{
BOOL result = [NSKeyedArchiver archiveRootObject:channels toFile:filename];
if (result)
{
NSLog(@"write success!");
}
else
{
NSLog(@"write fail!");
}
}
//還原函數(shù) 把字典中的成員讀取出來
-(void)read
{
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];
NSString *str = [NSString stringWithFormat:@"%@",array];
//NSLog(@"Content of %@ is : %@",filename,array);
NSLog(@"%@",str);
}
//打印函數(shù) 輸出頻道蝌箍、存放數(shù)據(jù)的文件名
-(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
======================================