当前位置 : 主页 > 编程语言 > 其它开发 >

Objective-C 基础教程第八章,FoundationKit介绍

来源:互联网 收集:自由互联 发布时间:2022-05-30
目录 Object-C 基础教程第八章,FoundationKit介绍 0x00 前言 0x01 Foundation Kit介绍 NSRange(范围类型) CGRect(几何数据类型) NSString (字符串类型) ☆ NSMutableString (可变字符串类型) ☆ NSArray (集合类型

目录
  • Object-C 基础教程第八章,FoundationKit介绍
    • 0x00 前言
    • 0x01 Foundation Kit介绍
    • NSRange(范围类型)
    • CGRect(几何数据类型)
    • NSString (字符串类型) ☆
    • NSMutableString (可变字符串类型) ☆
    • NSArray (集合类型) ☆
    • NSEnumerator (枚举类型)
    • NSDictionary(字典类型)☆
    • NSNumber(值类型)☆
    • NSValue (值类型)
    • NSNull (空类型)
    • 代码输出回显
    • 小项目:查找文件
      • 改进程序
    • Pwn菜鸡学习小分队

Object-C 基础教程第八章,FoundationKit介绍 0x00 前言

大家好!我是卷王VxerLee,难得清闲的大周末我又来卷了。

我找了个附近的StarBucks,戴上耳机、掏出我的mbp、摆出我的洛斐鼠标和键盘、摊开我的《Objective-C基础教程 (第2版)》书本,开始沉浸式学习。

image-20220326160701037

0x01 Foundation Kit介绍

今天要做的笔记内容是第8章Foundation Kit介绍,Foundation框架是Cocoa的很重要的一部分,内置了大量有用的类。

其中Cocoa中还包含了很重要的Application Kit(AppKit)User Interface Kit(UIKit)框架,其中前者多用于macOS桌面端的开发中,而UIKit多用于iOS的开发。

Foundation框架中的类或者函数基本上都以NS开头,他是基于另外一个框架CoreFoundation为基础创建的。

CoreFoundation框架是用纯C语言写的,所以他框架中的类和函数都是以CF开头的,以后遇到的话可以注意下。

OK话不多说,直接上代码,整活。

NSRange(范围类型)
//NSRange
NSRange nsRange = NSMakeRange(5, 6);
NSLog(@"location:%d",nsRange.location);
NSLog(@"length:%d",nsRange.length);
CGRect(几何数据类型)
//几何数据类型
//创建一个矩形
CGPoint point = CGPointMake(1, 2);
CGSize  size  = CGSizeMake(100, 200);
CGRect rect = CGRectMake(point.x, point.y, size.width, size.height);
NSString (字符串类型) ☆
//字符串
NSString *msg = [NSString stringWithFormat:@"Hello,%@,%d加油!",@"OC",2022];
NSLog(msg);
NSLog(@"字符串长度:%d",[msg length]);
//比较
if([msg isEqualToString:[NSString stringWithFormat:@"Hello,OC,%d加油!",2022]] == YES)
{
  NSLog(@"字符串相等");
}else
{
  NSLog(@"字符串不相等");
}
if([@"Hello" compare:@"hello" options:NSCaseInsensitiveSearch] == 0)
{
  NSLog(@"内容相同(不区分大小写)");
}else{
  NSLog(@"内容不同");
}
//字符串包含
//判断字符串是否以.skg结尾 是否以/var/movile开头,是否包含mobile 或者test.skg
NSString *filepath = @"/var/mobile/tset.skg";
if ([filepath hasSuffix:@".skg"]) {
  NSLog(@"文件是以.skg结尾的.");
}else
{
  NSLog(@"文件不是以.skg结尾的.");
}
//判断前缀 后缀
if([filepath hasPrefix:@"/var/mobile"])
{
  NSLog(@"文件是以/var/mobile开头的");
}else{
  NSLog(@"文件不是以/var/mobile开头的");
}
if([filepath rangeOfString:@"tset.skg"].location != NSNotFound)
{
  NSLog(@"该文件是test.skg文件");
}else{
  NSLog(@"该文件不是test.skg文件");
}
if ([filepath rangeOfString:@"mobile"].location != NSNotFound ) {
  NSLog(@"存在mobile目录");
}else
{
  NSLog(@"不存在mobile目录");
}
NSMutableString (可变字符串类型) ☆
//可变字符串
NSMutableString *mutablestring = [NSMutableString stringWithCapacity:66];
[mutablestring appendFormat:@"%d+%d=%d",1,2,3];
[mutablestring appendString:@"不错不错"];
[mutablestring deleteCharactersInRange:NSMakeRange(0, 2)];
NSArray (集合类型) ☆
//集合 ()
NSArray *array = [NSArray arrayWithObjects:@"One",@"tow",@"trhee",@"For",@"five", nil];

//遍历集合
for (NSInteger i=0; i<[array count]; i++) {
  NSLog(@"index %d has %@",i,[array objectAtIndex:i]);
}
//可变数组
NSMutableArray *mutarray =  [NSMutableArray arrayWithCapacity:18];
[mutarray addObject:@"1"];
[mutarray addObject:@"2"];
[mutarray addObject:@"3"];
[mutarray addObject:@"4"];

//NSInteger testN = 6;
//[mutarray addObject:testN];

for (NSInteger i=0; i<[mutarray count]; i++) {
  id test = [mutarray objectAtIndex:i];
  NSLog(@"%@",test);
}
NSEnumerator (枚举类型)
//枚举
NSEnumerator *enumerator = [mutarray objectEnumerator];
for (id value in mutarray) {
  NSLog(@"%@",value);
}
[array enumerateObjectsUsingBlock:^(id   obj, NSUInteger idx, BOOL * _Nonnull stop) {
  NSLog(@"using block enumerator %@",obj);
}];
NSLog(@"6");

NSString *str1 = @"hello";
NSString *str2 = @"World!";
NSString *str3 = @"i";
NSString *str4 = @"love";
NSString *str5 = @"oc";
NSDictionary(字典类型)☆
//字典类型
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:str1,@"1",str2,@"2",str3,@"3",str4,@"4",str5,@"5",nil];
NSDictionary *dic2 = @{@"1":str1,@"2":str2,@"3":str3,@"4":str4,@"5":str5};
NSLog(@"%@",[dic objectForKey:@"2"]);
NSLog(@"%@",dic[@"5"]);

//可变字典
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionary];
[mutableDic setObject:str1 forKey:@"1"];
[mutableDic setObject:str2 forKey:@"2"];
[mutableDic setObject:str3 forKey:@"3"];
[mutableDic setObject:str4 forKey:@"4"];
[mutableDic setObject:str5 forKey:@"5"];
NSLog(@"%@",mutableDic[@"1"]);

NSString *newstr = @"66666666666";
//替换key中的value
[mutableDic setObject:newstr forKey:@"1"];
NSLog(@"%@",mutableDic[@"1"]);

//删除值,通过key
[mutableDic removeObjectForKey:@"1"];
NSLog(@"%@",[mutableDic objectForKey:@"1"]);
NSNumber(值类型)☆
//NSNumber OC中用对象形式来显示的基本数据类型
//+(NSNumber *) numberWithChar: (char) value;
//+(NSNumber *) numberWithInt: (int) value;
//+(NSNumber *) numberWithFloat (float) value;
//+(NSNumber *) numberWithBool: (BOOL) value;

//返回值
//-(char)       charValue;
//-(int)        intValue;
//-(float)      floatValue;
//-(BOOL)       boolValue;
//-(NSString *) stringValue;
//字面值方式直接创建
NSNumber *number;
number = @"X";//字符类型
number = @12345;  //int
number = @12345u; //uint
number = @12345ul;//ulong
number = @12345ll;//long long
number = @123.45f;//float
number = @123.45; //double
number = @YES;    //BOOL

//将对象放入Dictionary ,否则不能直接将基本类型放入Dictionary
[mutableDic setObject:number forKey:@"1"];
NSValue (值类型)
//还有比NSNumber 更加强大的NSValue ,这个其实是NSNumber的父类,可以封装任意值
NSRect rect1 = NSMakeRect(1, 2, 30, 40);
NSValue *value = [NSValue valueWithBytes:&rect1 objCType:@encode(NSRect)];
[mutarray addObject:value];

//获取数值
//NSRect rect2 = [value getValue:&rect];
NSNull (空类型)
//NSNull 之前我们说过,不能在集合中放入nil值,因为在NSArray和NSDirectionary中nil有特殊的含义,
//但是有时候需要明确的存储空值

[NSNull null];
代码输出回显
2022-03-26 15:50:39.679650+0800 Foundation_Demo[2339:137850] location:5
2022-03-26 15:50:39.680168+0800 Foundation_Demo[2339:137850] length:6
2022-03-26 15:50:39.680286+0800 Foundation_Demo[2339:137850] Hello,OC,2022加油!
2022-03-26 15:50:39.680344+0800 Foundation_Demo[2339:137850] 字符串长度:16
2022-03-26 15:50:39.680394+0800 Foundation_Demo[2339:137850] 字符串相等
2022-03-26 15:50:39.680451+0800 Foundation_Demo[2339:137850] 内容相同(不区分大小写)
2022-03-26 15:50:39.680516+0800 Foundation_Demo[2339:137850] 文件是以.skg结尾的.
2022-03-26 15:50:39.680555+0800 Foundation_Demo[2339:137850] 文件是以/var/mobile开头的
2022-03-26 15:50:39.680617+0800 Foundation_Demo[2339:137850] 该文件是test.skg文件
2022-03-26 15:50:39.680658+0800 Foundation_Demo[2339:137850] 存在mobile目录
2022-03-26 15:50:39.680732+0800 Foundation_Demo[2339:137850] index 0 has One
2022-03-26 15:50:39.680841+0800 Foundation_Demo[2339:137850] index 1 has tow
2022-03-26 15:50:39.680883+0800 Foundation_Demo[2339:137850] index 2 has trhee
2022-03-26 15:50:39.680907+0800 Foundation_Demo[2339:137850] index 3 has For
2022-03-26 15:50:39.680928+0800 Foundation_Demo[2339:137850] index 4 has five
2022-03-26 15:50:39.685202+0800 Foundation_Demo[2339:137850] 1
2022-03-26 15:50:39.685263+0800 Foundation_Demo[2339:137850] 2
2022-03-26 15:50:39.685302+0800 Foundation_Demo[2339:137850] 3
2022-03-26 15:50:39.685336+0800 Foundation_Demo[2339:137850] 4
2022-03-26 15:50:39.685455+0800 Foundation_Demo[2339:137850] 1
2022-03-26 15:50:39.685521+0800 Foundation_Demo[2339:137850] 2
2022-03-26 15:50:39.685552+0800 Foundation_Demo[2339:137850] 3
2022-03-26 15:50:39.685574+0800 Foundation_Demo[2339:137850] 4
2022-03-26 15:50:39.685598+0800 Foundation_Demo[2339:137850] using block enumerator One
2022-03-26 15:50:39.685624+0800 Foundation_Demo[2339:137850] using block enumerator tow
2022-03-26 15:50:39.685643+0800 Foundation_Demo[2339:137850] using block enumerator trhee
2022-03-26 15:50:39.685662+0800 Foundation_Demo[2339:137850] using block enumerator For
2022-03-26 15:50:39.685680+0800 Foundation_Demo[2339:137850] using block enumerator five
2022-03-26 15:50:39.685698+0800 Foundation_Demo[2339:137850] 6
2022-03-26 15:50:39.685821+0800 Foundation_Demo[2339:137850] World!
2022-03-26 15:50:39.685900+0800 Foundation_Demo[2339:137850] oc
2022-03-26 15:50:39.685991+0800 Foundation_Demo[2339:137850] hello
2022-03-26 15:50:39.686086+0800 Foundation_Demo[2339:137850] 66666666666
2022-03-26 15:50:39.686189+0800 Foundation_Demo[2339:137850] (null)
Program ended with exit code: 0
小项目:查找文件

OK写了那么多片段代码,光学理论知识很容易就会忘记,我们需要来写个小项目动动手,加深肌肉记忆。

程序功能如下:

  • 可以查找你在Mac电脑上的主目录以查找.jpg文件并打印到列表中。
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //用来与文件系统交互的类
        NSFileManager *manager;
        manager = [NSFileManager defaultManager];
        
        //主目录 /Users/用户名
        NSString *home;
        home = [@"~" stringByExpandingTildeInPath];
        
        //目录 枚举,枚举home目录
        NSDirectoryEnumerator *direnmu;
        direnmu = [manager enumeratorAtPath:home];
        
        //可变集合,用来存储文件列表
        NSMutableArray *files;
        files = [NSMutableArray arrayWithCapacity:42];
        
        //遍历home 添加只属于jpg后缀的文件名
        NSString *filename;
        while (filename = [direnmu nextObject])
        {
            
            if( [filename hasSuffix:@".jpg"])
            {
                [files addObject:filename];
            }
        }
        
        //利用NSEnumerator类型 遍历输出集合中保存的文件名.
        NSEnumerator *fileenum;
        fileenum = [files objectEnumerator];
        while (filename = [fileenum nextObject]) {
            NSLog(@"%@",filename);
        }
        
    }
    return 0;
}

image-20220326165538933

改进程序

改进程序,使其可以使用命令行,并且能搜索自定义目录下自定义的后缀名,并且用快速枚举方法

#import <Foundation/Foundation.h>
#define NSLog(FORMAT, ...) printf("%s\n",[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

void usage()
{

    NSLog(@"    _______ __        _       __      ____             ______");
    NSLog(@"   / ____(_) /__     | |     / /___ _/ / /_____  _____/ ____/  __");
    NSLog(@"  / /_  / / / _ \\    | | /| / / __ `/ / //_/ _ \\/ ___/ __/ | |/_/");
    NSLog(@" / __/ / / /  __/    | |/ |/ / /_/ / / ,< /  __/ /  / /____>  <");
    NSLog(@"/_/   /_/_/\___/      |__/|__/\\__,_/_/_/|_|\\___/_/  /_____/_/|_|");
    NSLog(@"");
    NSLog(@"                             From 三进制安全 & PTU Team VxerLee");
    NSLog(@"");
    NSLog(@"例子:");
    NSLog(@"    ./FileWalkerEx /Users/VxerLee jpg");
    NSLog(@"");
}
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        if(argc < 3)
        {
            usage();
            return 0;
        }
        usage();
        //
        NSString *strDir    = [NSString stringWithUTF8String:argv[1]];
        NSString *strSuffic = [NSString stringWithFormat:@".%s",argv[2]];
        NSLog(@"开始搜寻目录:%@",strDir);
        NSLog(@"搜索后缀名为:%@的文件",strSuffic);
        
        //用来与文件系统交互的类
        NSFileManager *manager;
        manager = [NSFileManager defaultManager];
        
        NSMutableArray *files;
        files = [NSMutableArray arrayWithCapacity:69];
        
        //遍历
        for (NSString *filename in [manager enumeratorAtPath:strDir]) {
            if([filename hasSuffix:strSuffic])
            {
                NSLog(@"---------------------------------------------------------------------------------------------------------");
                NSLog(@"%@",filename);
                [files addObject:filename];
            }
        }
        //遍历完在输出太慢了,这里注释了
        /*
         for (NSString *filename in files) {
             NSLog(@"---------------------------------------------------------------------");
             NSLog(@"%@",filename);
         }
         */

    }
    return 0;
}

成品图:

image-20220326174611060

image-20220326174629926

对应的源码可在 https://github.com/Vxer-Lee/FileWalkerEx 中找到。

OK,在星巴克白嫖了不少白开水,也得收敛点了,不学了,出去Happy!

上一篇:Python 中的 retrying 库
下一篇:没有了
网友评论