OC4_电子词典

//
// MyDictionary.h
// OC4_电子词典
//
// Created by zhangxueming on 15/6/15.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h> #define FilePath @"/Users/zhangxueming/Desktop/ios1509/Day16_类的复合设计/dict.txt" @interface MyDictionary : NSObject
{
NSMutableDictionary *_mulDict;
} - (id)initWithFile:(NSString *)path; + (void)userInterface; @end
//
// MyDictionary.m
// OC4_电子词典
//
// Created by zhangxueming on 15/6/15.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "MyDictionary.h" @interface MyDictionary () - (BOOL)parseFileContent:(NSString *)path; @end @implementation MyDictionary - (id)initWithFile:(NSString *)path
{
self = [super init];
if (self) {
_mulDict = [NSMutableDictionary dictionary];
[self parseFileContent:path];
}
return self;
} - (BOOL)parseFileContent:(NSString *)path
{
//读取字典文件
NSString *fileContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//判断是否读取成功
if(!fileContent)
{
return NO;
}
//解析
NSArray *contentItems = [fileContent componentsSeparatedByString:@"\n"];
NSInteger len = [contentItems count];
for (NSInteger i=; i<len; i+=) {
if ([[contentItems objectAtIndex:i] isEqualToString:@""]) {
continue;
}
NSString *key = [[contentItems objectAtIndex:i] substringFromIndex:];
NSString *value = [[[contentItems objectAtIndex:i+] substringFromIndex:] stringByReplacingOccurrencesOfString:@"@" withString:@"\n"];
[_mulDict setObject:value forKey:key];
}
return YES;
} + (void)userInterface
{
MyDictionary *dict = [[MyDictionary alloc] initWithFile:FilePath];
NSLog(@"欢迎使用电子词典");
char str[]={};
while (YES) {
NSLog(@"请输入要查找的单词:");
scanf("%s", str);
NSLog(@"翻译:%@",[dict->_mulDict objectForKey:[NSString stringWithUTF8String:str]]);
}
} @end
//
// main.m
// OC4_电子词典
//
// Created by zhangxueming on 15/6/15.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h>
#import "MyDictionary.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
[MyDictionary userInterface];
}
return ;
}
上一篇:CheckStyle, 强制你遵循编码规范


下一篇:【Linux】多睡/少睡一小时!冬夏令时全解析