IOS-OC-书签和字典

书签

#import 
#import "MCJBookmark.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MCJBookmark *bookMark = [[MCJBookmark alloc] init];
        [bookMark addItemToBookMarkWithUrl:@"www.kenan.com" title:@"柯南" stars:5];
        [bookMark removeItemWithTitle:@"柯南"];
    }
    return 0;
}

#import 

@interface MCJURLItem : NSObject
{
//    1. 网址url(字符串)
    NSString *_url;
//    2. 标题 title(字符串)
    NSString *_title;
//    3. 星级stars(整形)
    int _stars;
//    4. 访问次数 vistis(整型)
    int vistis;
}

//(1)修改标题
- (void)setTitle:(NSString *)title;
//(2)修改网址
- (void)setUrl:(NSString *)url;
//(3)设置星级
- (void)setStars:(int )stars;

- (NSString *)url;
- (NSString *)titile;

// 重写初始化方法
- (instancetype)initWithTitle:(NSString *)title url:(NSString *)url stars:(int)stars;

@end
#import "MCJURLItem.h"

@implementation MCJURLItem

- (instancetype)initWithTitle:(NSString *)title url:(NSString *)url stars:(int)stars
{
    if (self = [super init]) {
        _title = title;
        _url = url;
        _stars = stars;
    }
    return self;
}

- (void)setTitle:(NSString *)title
{
    _title = title;
}

- (void)setUrl:(NSString *)url
{
    _url = url;
}

- (void)setStars:(int)stars
{
    _stars = stars;
}

- (NSString *)url
{
    return _url;
}

- (NSString *)titile
{
    return _title;
}
@end
#import 

@interface MCJBookmark : NSObject
{
//    1.可变数组(用于排序,按不同顺序打印书签的标题)
    NSMutableArray *_arrayM;
    
//    2.可变字典(用于查找书签,键是书签的标题,值是书签对象)
    NSMutableDictionary *_dictM;
}

// 类的初始化方法
- (instancetype)init;


//(1)添加书签:参数有网址,标题,星级
- (void)addItemToBookMarkWithUrl:(NSString *)url title:(NSString *)title stars:(int )stars;

//(2)删除标签(可以用标题和网址两种方式来删除)
- (void)removeItemWithTitle:(NSString *)title;
- (void)removeItemWIthUrl:(NSString *)url;

//(3)查询书签(通过书签标题)

//(4)修改书签对应的网址(通过书签标题)

//(5)修改书签标题(注意字典类中key必须是唯一的)

//(6)根据星级显示所有的书签标题

//(7)根据访问次数来显示所有的书签标题

@end
#import "MCJBookmark.h"
#import "MCJURLItem.h"

@implementation MCJBookmark
- (instancetype)init
{
    if (self = [super init]) {
        // 初始化我们要用的资源
        _arrayM = [[NSMutableArray alloc] init];
        _dictM = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void)addItemToBookMarkWithUrl:(NSString *)url title:(NSString *)title stars:(int)stars
{
    // 书签对象
    MCJURLItem *item = [[MCJURLItem alloc] initWithTitle:title url:url stars:stars];
    // 判断书签是否已经存在
    if (_dictM[title]) {
        return;
    }
    
    // 存储书签
    [_arrayM addObject:item];
    [_dictM setObject:item forKey:title];
}

- (void)removeItemWithTitle:(NSString *)title
{
    MCJURLItem *deleteItem = _dictM[title];
    
    // 判断对象存不存在,不存在就return
    if (!deleteItem) {
        return;
    }
    
    // 删除书签
    [_arrayM removeObject:deleteItem];
    [_dictM removeObjectForKey:title];
}

- (void)removeItemWIthUrl:(NSString *)url
{
    for (MCJURLItem *item in _arrayM) {
        NSString *urlGet = [item url];
        
        // 字符串的对比使用isEqualToString
        if ([url isEqualToString:urlGet]) {
            NSString *titleGet = [item titile];
            
            // 删除
            [_arrayM removeObject:item];
            [_dictM removeObjectForKey:titleGet];
        }
    } 
}

@end

字典

#define PATH @"/Users/qianfeng/Desktop/OC课堂老师代码/oc上课代码/1217-2/资源/dict.txt"

#import "MCJDictionary.h"

@interface MCJDictionary ()
{
    NSMutableDictionary *_dictionaryM;
}
@end

static MCJDictionary *_dictM;

@implementation MCJDictionary
- (id)init
{
    if (self = [super init]) {
        _dictionaryM = [[NSMutableDictionary alloc] init];
    }
    
    // 读取文件
    [self readFile];
    
    return self;
}

#pragma mark - 读取文件
- (void)readFile
{
    NSString *str = [[NSString alloc] initWithContentsOfFile:PATH encoding:NSUTF8StringEncoding error:nil];
    NSArray *array = [str componentsSeparatedByString:@"\n"];
    
    for (int i = 0; i < [array count] - 1; i += 2) {
        // 英文
        NSString *englishWord = array[i];
        // 汉语翻译
        NSString *chineseWord = array[i + 1];
      
        [_dictionaryM setObject:[chineseWord substringFromIndex:6] forKey:[englishWord substringFromIndex:1]];
    }
    
}

#pragma mark - 获取翻译内容
#pragma mark  成员方法获取翻译内容
- (NSString *)chineseTransFromEnglish:(NSString *)englishWord
{
    // 将字母转成小写
    NSString *englishWordL = [englishWord lowercaseString];
    NSString *trans = [_dictionaryM objectForKey:englishWordL];
    
    // 输入的单词,这个词典没有
    if (trans == nil) {
        return @"没有这个单词";
    }
    
    NSArray *array = [trans componentsSeparatedByString:@"@"];
    NSString *newWord = [array componentsJoinedByString:@"\n"];
    return newWord;
}


#pragma mark  类方法获取翻译内容
+(NSString *)chineseTransFromEnglish:(NSString *)englishWord
{
    if (!_dictM) {
        _dictM = [[MCJDictionary alloc] init];
    }
    
    return [_dictM chineseTransFromEnglish:englishWord];
    
}

#pragma mark - 启动字典
+ (void)startDictionary
{
    printf("=======================\n");
    printf("   欢迎使用英汉词典\n");
    printf("=======================\n");
    
    char buf[64] = {};
    while (1) {
        printf("请输入英语单词:\n");
        scanf("%s", buf);
        //查询单词
        NSString * CH = [MCJDictionary chineseTransFromEnglish:[NSString stringWithUTF8String:buf]];
        //转成C字符串
        printf("%s\n", [CH UTF8String]);
    A:
        //清理缓冲区
        while (getchar() != '\n');
        printf("选择操作:1.继续查询 2.结束查询!\n");
        
        int c;
        int ret = scanf("%d", &c);
        if (ret <= 0) {
            goto B;
        }
        
        switch (c) {
            case 1:
                break;
            case 2:
                printf("感谢使用,再见!\n");
                exit(0);
                break;
                
            default:
            B:
                printf("操作错误,请重选择!\n");
                goto A;
                break;
        }
    }

    
}

@end

字典简单版

#import 
#import "Dictionary.h"

#define PATH @"/Users/qianfeng/Desktop/OC课堂老师代码/oc上课代码/1217-2/资源/dict.txt"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Dictionary *dict = [[Dictionary alloc] initWithPath:PATH];
        
        // 输入
        char eng[20];
        scanf("%s",eng);
        NSString *engStr = [NSString stringWithFormat:@"%s",eng];
        
        // 查询
        NSString *result = [dict translationFrom:engStr];
        NSLog(@"%@",result);
    }
    return 0;
}
#import 

@interface Dictionary : NSObject
- (instancetype)initWithPath:(NSString *)path;
- (NSString *)translationFrom:(NSString *)str;
@end
#import "Dictionary.h"

@interface Dictionary ()
{
    NSMutableDictionary *_dictM;
}
@end

@implementation Dictionary
- (instancetype)initWithPath:(NSString *)path
{
    if (self = [super init]) {
        _dictM = [[NSMutableDictionary alloc] init];
        
        // 读取文件
        [self readFileWithPath:path];
    }
    return self;
}

- (void)readFileWithPath:(NSString *)path
{
    // 1. 将文本信息转成字符串对象
    NSString *str = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
    // 2. 切割字符串
    NSArray *array = [str componentsSeparatedByString:@"\n"];
    
    for (int i = 0; i < array.count - 1; i += 2) {
        // 英文
        NSString *englishWord = array[i];
        // 中文
        NSString *chineseWord = array[i + 1];
        
        // 处理字符串
        NSString *engW = [englishWord substringFromIndex:1];
        NSString *chinW = [chineseWord substringFromIndex:6];
        
        // 存储数据
        [_dictM setObject:chinW forKey:engW];
    }
}

- (NSString *)translationFrom:(NSString *)str
{
    // 1. 把大写转成小写
    NSString *engW = [str lowercaseString];
    
    // 2. 小写转成大写
   // NSString *engW = [str uppercaseString];
    
    // 3.转成首字母大小
   // NSString *engW = [str capitalizedString];

    NSString *result = [_dictM objectForKey:engW];
    return result;
}

@end

你可能感兴趣的:(IOS-OC-书签和字典)