`

IOS开发重点与难点

 
阅读更多

1。给iPhone程序创建Splash欢迎界面
最简单的方法就是做一个全屏的欢迎页的图片,把它命名为Default.png,然后放在Xcode工程的Resource里面。 执行就可以看到你的这个默认图像在程序完全加载之前显示在屏幕上。
Default.png是一张480*320的png图片用于在程序启动时显示。启动时,系统会用这张图片作为临时背景,直到程序载入了他的窗口和用户界面。
Icon.png是一张57*57的png图片,用于在iPhone的主界面上作为程序图标代表你的程序。这张图片不需要有其他附加特效,系统会自动添加这些效果。
Icon-Setting.png是一张29*29的png图片,用于在设置程序中作为图标代表你的程序。如果你的程序有Settings.bundle,这个图标将会显示在程序名的旁边。如果你没有设置这张图片,系统会将Icon.png缩放来代替。
2。 怎样才能让程序在运行过程中不锁屏呢?
[UIApplication sharedApplication].idleTimerDisabled=YES;//not let iphone go to sleep
3。显示被view 或 control遮盖的背景内容
xx.backgroundColor=[UIColor clearColor];
4。url编码
NSString *strURL = @”http://www.google.com/search?hl=en&newwindow=1&q=如何对url编码&aq=f&oq=&aqi=”;
strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //使用utf8
strURL=[strURLstringByAddingPercentEscapesUsingEncoding: CFStringConvertEncodingToNSStringEncoding( kCFStringEncodingGB_18030_2000)] ;////使用 gb2312
5。转换网页编码gb2312 -> utf
1.        //编码转换 gb2313 to UTF
2.        NSData * myResponseData = [myRequest responseData];
3.        NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
4.        NSString * myResponseStr = [[NSString alloc] initWithData:myResponseData encoding:enc];
6。获取界面语言设置
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];
7.对于做为数据Model的类来说,让其实现NSCoding协议是个好习惯
it’s just good habit to conform data model classes to NSCoding.
8
It’s okay to redefine properties to be more permissive than the
same property as declared in a protocol to which you’ve conformed, or as declared in
your superclass. You can always redefine a readonly or writeonly property to be
readwrite, but you have to explicitly use the readwrite keyword. Most of the time, that
keyword isn’t used because it’s the default value and unnecessary.
NSFileManager中包含了用来查询单词库目录、创建、重命名、删除目录以及获取/设置文件属性的方法(可读性,可编写性等等)。

每个程序都会有它自己的沙盒,通过它你可以阅读/编写文件。写入沙盒的文件在程序的进程中将会保持稳定,即便实在程序更新的情况下。
如下所示,你可以在沙盒中定位文件目录:
//对于错误信息
NSError *error;
// 创建文件管理器
NSFileManager *fileMgr = [NSFileManagerdefaultManager];
//指向文件目录
NSString *documentsDirectory= [NSHomeDirectory() 
stringByAppendingPathComponent"Documents"];
//创建一个目录
[[NSFileManager defaultManager]   createDirectoryAtPath: [NSString stringWithFormat"%@/myFolder", NSHomeDirectory()] attributes:nil];

创建一个文件
现在我们已经有了文件目录,我们就能使用这个路径在沙盒中创建一个新文件并编写一段代码:
// File we want to create in the documents directory我们想要创建的文件将会出现在文件目录中
// Result is: /Documents/file1.txt结果为:/Documents/file1.txt
NSString *filePath= [documentsDirectory
stringByAppendingPathComponent"file1.txt"];
//需要写入的字符串
NSString *str= @"iPhoneDeveloper Tips\nhttp://iPhoneDevelopTips,com";
//写入文件
[str writeToFile:filePath atomically:YES 
encoding:NSUTF8StringEncoding error:&error];
//显示文件目录的内容
NSLog(@"Documentsdirectory: %@",
[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
我们为想要创建的文件构建一条路径(file1.txt),初始化一个字符串来写入文件,并列出目录。最后一行显示了在我们创建文件之后出现在文件目录下的一个目录列表:
对一个文件重命名
想要重命名一个文件,我们需要把文件移到一个新的路径下。下面的代码创建了我们所期望的目标文件的路径,然后请求移动文件以及在移动之后显示文件目录。
//通过移动该文件对文件重命名
NSString *filePath2= [documentsDirectory
stringByAppendingPathComponent"file2.txt"];
//判断是否移动
if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)
NSLog(@"Unable to move file: %@", [error localizedDescription]);
//显示文件目录的内容
NSLog(@"Documentsdirectory: %@", 
[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
在移动了文件之后,输出结果应该如下图所示:
删除一个文件
为了使这个技巧完整,让我们再一起看下如何删除一个文件:
//在filePath2中判断是否删除这个文件
if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)
NSLog(@"Unable to delete file: %@", [error localizedDescription]);
//显示文件目录的内容
NSLog(@"Documentsdirectory: %@",
[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
一旦文件被删除了,正如你所预料的那样,文件目录就会被自动清空:
这些示例能教你的,仅仅只是文件处理上的一些皮毛。想要获得更全面、详细的讲解,你就需要掌握NSFileManager文件的知识。
在开发iPhone程序时,有时候要对文件进行一些操作。而获取某一个目录中的所有文件列表,是基本操作之一。通过下面这段代码,就可以获取一个目录内的文件及文件夹列表。

1.        
NSFileManager *fileManager = [NSFileManager defaultManager];
//在这里获取应用程序Documents文件夹里的文件及文件夹列表
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDir = [documentPaths objectAtIndex:0];
        NSError *error = nil;
        NSArray *fileList = [[NSArray alloc] init];
//fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
        fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];

以下这段代码则可以列出给定一个文件夹里的所有子文件夹名
1.        
NSMutableArray *dirArray = [[NSMutableArray alloc] init];
        BOOL isDir = NO;
//在上面那段程序中获得的fileList中列出文件夹名
        for (NSString *file in fileList) {
                NSString *path = [documentDir stringByAppendingPathComponent:file];
                [fileManager fileExistsAtPath:path isDirectory&isDir)];
                if (isDir) {
                        [dirArray addObject:file];
                }
                isDir = NO;
        }
        NSLog(@"Every Thing in the dir:%@",fileList);
        NSLog(@"All folders:%@",dirArray);

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics