iphone开发前传


                                                          Objective -C语言基础学习日志(五)

今天,沈老师带领我们把昨天学的一些知识点重新简单复习了一下,然后又给我们总结了一些Objective-C中以后会经常用到的一些操作及相关代码实例,下面是我们今天所要学习的知识点:

1Objective中属性定义

@interface 类名

{

 成员

}

@property 成员;

@end

@implementation类名

@synthesize 成员;

 

@end

 

类名 * 对象 =[[类名 alloc]init];

对象.属性=xxx;

 

2、静态变量static修饰的成员

@interface 类名

{

 成员

}

@property 成员;

+(void)func;

@end

以+开头的方法 类方法可以通过

[类名 方法名]调用

 

3、协议

@protocol 协议名

   

   -(void)play;

 

@end

 

@interfaceApplication:NSApplication <ApplicationDelegate>

 

 

@end

4Objective中常用的一些事例及相关代码

#import <Foundation/Foundation.h>

 

int main(int argc, const char * argv[])

{

    @autoreleasepool {

       //-------------------NSNumber------------------

        /*

       A//基本类型====>>对象

        int num =1;

        float num2 = 4.5;

        BOOL isBool = NO;

        char c = 'c';

        B//封装成对象  ---------------类方法实现

        NSNumber *IntNumber =[NSNumber numberWithInt:num];

        NSNumber *FloatNumber =[NSNumber numberWithFloat:num2];

       

        C//          ---------------实例方法实现

       NSNumber *isBoolNumber =[[NSNumber alloc] initWithBool:isBool];

       

        NSNumber *CNumber =[[NSNumber alloc] initWithChar:c];

       

       D//打印

       

        NSLog(@"IntNumber :%@",IntNumber);

       NSLog(@"isBoolNumber : %@",isBoolNumber);

       

       

        E//对象======>>基本类型

        int d = [IntNumberintValue];

        float f1 = [FloatNumberfloatValue];

        char c1 = [CNumbercharValue];

       

        NSLog(@"");

       

         */

       

        //------------------------NSString-------------------

        /*

       F //字符串常量

        NSString *str =@"good!";

       

        G//字符串的变量

       

           //1)空字符串创建

        NSString *str2 =[[NSString alloc] init];

        str2 =@"test";

       

        NSString *str3 =[NSString string];

        NSLog(@"str =%@,str2=%@,str3=%@",str,str2,str3);

       

           //2)快速创建字符串

        NSString *str4 =[[NSString alloc] initWithString:@"pk"];

       

        NSString *str5 =[NSString stringWithString:@"pk"];

        NSLog(@"str4 =%@,str5=%@",str4,str5);

       

           //3)格式化创建字符串

  

        NSString *str6 =[[NSString alloc]initWithFormat:@"%d_%d_%d_%d_%d_%@",1,2,3,4,5,str4];

       NSLog(@"str6=%@",str6);

       

        //判断字符串是否相等

        if([str4isEqualToString:str5]){

           NSLog(@"字符串相等");

        }else{

       

           NSLog(@"字符串不相等");

        }

       

        NSLog(@"%p,%p",str4,str5);

        //判断字符串

        if(str4 == str5){

           NSLog(@"是同一个对象!");

        }

       

       

       

       H //基本数据类型 ===> 字符串

        //str = [[NSStringalloc] initWithFormat:@"%d",5];

        //字符串 ===> 基本数据类型

        NSString *str7 =[NSString stringWithFormat:@"%d",56];

        int num7 = [str7intValue];  //字符串转整型

        NSLog(@"num7+1=%d",num7+1);

       

        //字符串转换 ======>> 数组

        NSArray *array = [str6componentsSeparatedByString:@"_"];

       NSLog(@"%@",array);

       

        //字符串截取 abcdefg

        NSLog(@"substringto 2 :%@",[str6 substringToIndex:5]);

        NSLog(@"substringto 2 :%@",[str6 substringFromIndex:5]);

       

        //截取某一个范围的字符串

        NSRange rang;

        rang.length = 4;

        rang.location = 2;          //截取的时候,包含起始位置

        NSLog(@"substringto 2 :%@",[str6 substringWithRange:rang]);

       

        //字符串查找(查找子串)

        NSString*str8=@"hello01.txt";

        //查找 ,返回范围

        NSRange rang2 = [str8rangeOfString:@"."];

       

        if(rang2.location !=NSNotFound){

           NSLog(@"sub str location =%ld ,length=%ld",rang2.location,rang2.length);

       

        }else{

       

           NSLog(@"NSNotFound!!");

        }

       

        M//可变长度的字符串

        NSMutableString*mutableStr = [NSMutableString stringWithString:@"爱大米"];

       

       N //动态的插入内容

        [mutableStr insertString:@"老鼠" atIndex:0];

       

       NSLog(@"mubableStr:%@",mutableStr);

        */

       

       

       //--------------------------------NSArray---------------------------

        /*

      I //定义数组并且初始化

        NSArray *array1 =[NSArray arrayWithObject:@"one"];

       

        NSArray *array2 =[NSArrayarrayWithObjects:@"one",@"two",@"three",@"four",nil];

       

        NSArray *array3 =[NSArray arrayWithArray:array2];

       

        NSLog(@"array1 =%@, array2 = %@, array3 = %@",array1,array2,array3);

       

        K//数组的访问

        //求长度

        int len  = [array2 count];

        //访问元素

        NSString *arrayObject =[array3 objectAtIndex:3];

        L//将数组元素连接成一个字符串

        NSString *newStr =[array2 componentsJoinedByString:@"_"];

        NSLog(@"array2length:%d,index 3=%@,joinStr = %@",len,arrayObject,newStr);

       

       

        P//可变数组的使用

        NSMutableArray*mutableArray = [NSMutableArray arrayWithObjects:@"one", nil];

       O //----添加元素

        [mutableArrayaddObject:@"two"];

        [mutableArray addObject:@"three"];

        [mutableArrayaddObject:@"four"];

        //-------------添加一个数组

        [mutableArrayaddObjectsFromArray:array2];

       

       U //----计算长度

        int length =[mutableArray count];

       NSLog(@"mutableArray length=%d,countent:%@",length,mutableArray);

       

        //---- 移除最后一个

        [mutableArrayremoveLastObject];

       

        //---- 移除指定的数据

        [mutableArrayremoveObjectAtIndex:0];

       

        length = [mutableArraycount];

        NSLog(@"***mutableArraylength=%d,countent:%@",length,mutableArray);

       

       

       Y //数组的遍历方式:传统方式 高效方式

        //----- 传统方式

       

        for (int i=length-1;i>=0; i--) {

           

           NSLog(@"%d = %@",i,[mutableArray objectAtIndex:i]);

        }

        

       

       T //----- 高效方式

        for(NSString *str inmutableArray){

       

           NSLog(@"obj =%@",str);

           

        }

       

        */

       

       

       

       //--------------------------------NSDictionary-----------------------

        //----- 初始化

        /*

        NSNumber *numObj =[NSNumber numberWithInt:100];

           //初始化一组数组

          //                                                    值           key

        NSDictionary *dic1 =[NSDictionary dictionaryWithObject:numObj forKey:@"key1"];

       

         R //初始化多组数据

        NSDictionary *dic2 =[NSDictionarydictionaryWithObjectsAndKeys:@"hello",@"key2",@"world",@"key3",@"csdn",@"key4",nil];

           //用一个字典初始化另外一个字典

        NSDictionary *dic3 = [NSDictionarydictionaryWithDictionary:dic2];

        //打印输出

        NSLog(@"dic1 :  %@,dic2  :  %@,  dic3   :  %@",dic1,dic2,dic3);

         

       

       W //------ 获取值

       

        //获取长度

        int len = [dic2 count];

        NSLog(@"dic2 length= %d",len);

        //根据key获取key所对应的value

        NSLog(@"key3 value= %@",[dic2 objectForKey:@"key3"]);

        //可以获取所有的keys

        NSArray *allkeys =  [dic3 allKeys];

        NSLog(@"NSarrayallkey = %@",allkeys);

 

        //可以获取所有的values

        NSArray *allvalues=  [dic3 allValues];

        NSLog(@"NSarrayallvalues = %@",allvalues);

       

       Q //--- 可变字典

        //----- 初始化

        NSMutableDictionary*dic4 = [NSMutableDictionarydictionaryWithObjectsAndKeys:@"one",@"key4",@"two",@"key5",nil];

        NSLog(@"dic4  :   %@",dic4);

        //定义成空字典

        NSMutableDictionary*dic5 = [NSMutableDictionary dictionary];

       

        //讲字典dic2整体添加到dic4钟

        [dic4addEntriesFromDictionary:dic2];

       NSLog(@"addEntriesFromDictionary dic2  :  %@",dic4);

       

        //添加一个元素

        [dic4setValue:@"three" forKey:@"key6"];

        NSLog(@"dic4setValue :   %@",dic4);

        //根据key获取value

        NSLog(@"key6 =  %@",[dic4objectForKey:@"key6"]);

       

       

       

        //------ 字典的遍历

        //1)一般的遍历

        NSArray *keys4 = [dic4allKeys];

       

        for(int i=0;i<[dic4count];i++){

       

           NSLog(@"dic4 key = %@,value=%@",[keys4 objectAtIndex:i],[dic4objectForKey:[keys4 objectAtIndex:i]]);

       

       }

       NSLog(@"-----------------------");

       

        //2)高效的for

        for (NSString *key indic4){

           NSLog(@"dic4 key = %@ ,value = %@",key,[dic4objectForKey:key]);

        }

       

       NSLog(@"-----------------------");

        //3)使用枚举进行遍历

        NSEnumerator *enum1 =[dic4 keyEnumerator];

        //获取key,如果不为空,则进行偏移

        id key = [enum1nextObject];

       

        while (key) {

           

           NSLog(@"key = %@ ,value = %@ ",key,[dic4 objectForKey:key]);

           

           key = [enum1 nextObject];

        }

       

        */

       

       //----------------------------------NSSet-------------------

      S //----- 定义、初始化

        NSSet *set = [[NSSetalloc] initWithObjects:@"one",@"one",@"two",nil];

       

        //用数组定义NSSet;

        NSArray *arrayset =[NSArray arrayWithObjects:@"1",@"2",@"3",nil];

       

        NSSet *set2 = [NSSetsetWithArray:arrayset];

       

        NSLog(@"set1 =%@,set2 = %@",set,set2);

        //访问方式

        //----- 获取长度

        int len = [set2 count];

       

        NSString *s = [set2anyObject];

       

        NSLog(@"set2 length= %d,obj = %@",len,s);

       

    }

    return 0;

}

5、对文件进行的操作相关代码

       //----------------------获取沙盒信-----------------

       //获取应用程序根目录

       NSString *path1 = NSHomeDirectory();

       

       //获取docment的目录

       NSArray *patharr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

       //取出数组内容

       NSString *path2 = [patharr objectAtIndex:0];

       

       NSLog(@"path1:%@\n,patharr = %@\n,path2=%@\n",path1,patharr,path2);

       

       */

       

       /*

       NSArray *patharr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

       //取出数组内容

        NSString *path2 = [patharrobjectAtIndex:0];

 

       NSString *filePath = [path2stringByAppendingPathComponent:@"helloword.txt"];

 

       NSLog(@"filePath=%@",filePath);

       */

 

       

       

       

       

       

       //-------------------文件操作 --------------------

       //通过NSFileManager 创建文件

       //---- 初始化

       NSFileManager *fm  =[NSFileManager defaultManager];

       

 

       /*

 

       

       //---- 创建保存的路径

       NSArray *patharr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

       //取出数组内容

       NSString *path = [patharr objectAtIndex:0];

       //---- path :/Users/liwei/Documents

       //---- 创建文件保存的路径

       NSString *filePath = [path stringByAppendingPathComponent:@"helloword.txt"];

       

       //---- filepath :/Users/liwei/Documents/helloword.txt

       

       

       NSString *text = @"我喜欢凤姐5.6,121212";

       //---- 定义data

       NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];

       //---- 写入文件

       BOOL isOK=[fm createFileAtPath:filePath contents:dataattributes:nil];   

       

       if (isOK) {

           NSLog(@"文件创建成功!");

       }else{

       

           NSLog(@"失败了!");

       }

       

       //-------------创建文件方法2:简单版

       

       NSString *str = @"缺课的人喜欢凤姐!";

       [str writeToFile:@"/Users/liwei/Desktop/fengjie.txt"atomically:YES encoding:NSUTF8StringEncoding error:nil];

       

       

       */

       //--------------------------创建目录----------------

       /*

        //withIntermediateDirectories

       // YES 如果文件夹不存在,则创建, 如果存在表示可以覆盖

       // NO  如果文件夹不存在,则创建, 如果存在不可以覆盖

       NSString *dirPath = @"/Users/liwei/Desktop/test";

       BOOL isOK = [fm createDirectoryAtPath:dirPathwithIntermediateDirectories:NO attributes:nil error:nil];

       

       if (isOK) {

           NSLog(@"创建成功!");

       }else{

           NSLog(@"创建失败!");

       

       }

       

       //------------ 读取文件 -------------

       //三种方法读取文件,假设读取文件内容为NSString

       //1、通过NSData 读取文件

       //  1)读取文件到NSData

       NSString *filePath=@"/Users/liwei/Desktop/fengjie.txt";

       NSData *data2 = [NSData dataWithContentsOfFile:filePath];

       //  2)将NSData转换为NSString

       NSString *contentStr1 = [[NSString alloc] initWithData:data2encoding:NSUTF8StringEncoding];

       NSLog(@"fileContent------:%@",contentStr1);

       

       

       

       

       

       //2、根据系统路径,指定文件名的文件读取

       //  1)产生文件路径

       NSArray *patharr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

       //取出数组内容

       NSString *path = [patharr objectAtIndex:0];

       //---- path :/Users/liwei/Documents

       //---- 创建文件保存的路径

       NSString *filePath2 = [path stringByAppendingPathComponent:@"helloword.txt"];

       NSLog(@"filePath2 : %@",filePath2);

       //  2)读取到NSData

       NSData *data3 = [NSData dataWithContentsOfFile:filePath2];

       //  3)转换到NSString

       NSString *contentStr3 = [[NSString alloc] initWithData:data3encoding:NSUTF8StringEncoding];

       

       NSLog(@"contentStr3 : %@",contentStr3);

       

       //3、字符串使用文件进行初始化 stringWithContentsOfFile:path

       //定义字符串的同时就用文件初始化

       

       NSString *contentStr4 = [NSStringstringWithContentsOfFile:@"/Users/liwei/Desktop/test.txt"encoding:NSUTF8StringEncoding error:nil];

       

       NSLog(@"contentStr4 :  %@",contentStr4);

       

       */

       

       

       // ----------------- 移动文件 -------------

       /*

       NSString *formPath = @"/Users/liwei/Desktop/test2.txt";

       NSString *moveToPath = @"/Users/liwei/Desktop/test3/test.txt";

       //创建目录 /Users/liwei/Desktop/test3

       [fm createDirectoryAtPath:[moveToPath stringByDeletingLastPathComponent]withIntermediateDirectories:YES attributes:nil error:nil];

       

       NSError *err;

       //开始移动文件,并且返回移动结果 YES or NO

       BOOL isOK = [fm moveItemAtPath:formPath toPath:moveToPatherror:&err];

       if(isOK){

       

           NSLog(@"移动文件成功!");

       }else{

       

            NSLog(@"移动失败!");

       

       }

       */

       

       

       //---------------文件复制------------------

       /*

       NSString *formPath = @"/Users/liwei/Desktop/test3/test.txt";

       NSString *copyToPath = @"/Users/liwei/Desktop/备份/test3.txt";

       //创建目录 /Users/liwei/Desktop/备份

       [fm createDirectoryAtPath:[copyToPath stringByDeletingLastPathComponent]withIntermediateDirectories:YES attributes:nil error:nil];

       

       NSError *err;

       //开始复制文件,并且返回移动结果 YES or NO

       BOOL isOK = [fm copyItemAtPath:formPath toPath:copyToPatherror:&err];

       if(isOK){

           

           NSLog(@"复制文件成功!");

       }else{

           

           NSLog(@"复制文件失败!");

           

       }

       

       */

       //-----------------删除文件--------------

       /*

       NSString *deletePath = @"/Users/liwei/Desktop/备份/test3.txt";

       //判断要删除的文件是否存在

       if ([fm fileExistsAtPath:deletePath]) {

           

           NSLog(@"文件存在!");

           //如果存在,则删除文件

            if([fm removeItemAtPath:deletePath error:nil]) {

               NSLog(@"文件删除成功!");

           }else{

               NSLog(@"文件删除失败!");

           }

           

       }else{

           //提示要删除的文件不存在

           NSLog(@"您要删除的文件不存在!");

        }

       */

       

       

       

       //----------------目录列表--------------

       //设定要读取的目录

       NSString *userDirPath =@"/Users/liwei/Desktop";//NSHomeDirectory();

       

       //定义枚举对象

       NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:userDirPath];

       //定义变量存储路径

       NSString *dirPath =nil;

       

       //遍历目录,打印所有的文件或者子目录名称

       while ((dirPath = [dirEnum nextObject])!=nil) {

           NSLog(@"list dir: --- %@",dirPath);

       }

       

       

    }

    return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值