老谢博客

  • 首页
  • WordPress
  • 网络技术
  • 乱七八糟
  • 运维技术
  • 给我留言
  • 关于老谢

Foundation框架常用方法学习笔记

分类:程序设计日期:2015-10-29 - 15:58:45作者:老谢

  • NSString
  • NSMutableString
  • NSArray
  • NSMutableArray
  • NSDictionary
  • NSMutableDictionary

PS. 这两天代码敲的ctrl+s惯了,写这篇帖子的时候也下意识的ctrl+s,然后发现wp居然也支持这个快捷键来保存草稿!

NSString
//
//  main.m
//  Demo
//
//  Created by Jingjia Xie on 15/10/29.
//  Copyright © 2015年 Jingjia Xie. All rights reserved.
//
 
#import <Foundation/Foundation.h>
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        char *s = "Hello Objective-C";
        NSString *str = @"Hello";
        //C转OC字符串
        NSString *str1 = [NSString stringWithUTF8String:s];
        NSLog(@"str1 = %@",str1);
        //OC转C字符串
        NSLog(@"str2 = %s",[str UTF8String]);
 
        //创建字符串
        NSString *str3 = @"IOS";
        NSString *str4 = [[NSString alloc] init];
        str4 = @"IOS";
 
        //***格式化字符串***
        int a = 10;
        int b = 20;
 
        NSString *str5 = [NSString stringWithFormat:@"a=%d b=%d",a,b];
        NSLog(@"str5 = %@",str5);
 
        //拼接字符串
        NSString *str6 = [str5 stringByAppendingString:str3];
        NSLog(@"str6 = %@",str6);
 
        //转换小写
        NSString *str7 = @"AbCdEF";
        NSString *str8 = [str7 lowercaseString];
        NSLog(@"str8 = %@",str8);
 
        //转换大写
        NSString *str9 = [str7 uppercaseString];
        NSLog(@"str9 = %@",str9);
 
        //判断前缀
        NSString *str10 = @"www.xj123.info";
        BOOL hasPrefix = [str10 hasPrefix:@"www."];
        if(hasPrefix)
            NSLog(@"有对应前缀");
        else
            NSLog(@"没有对应前缀");
 
        //判断后缀
        BOOL hasSuffix = [str10 hasSuffix:@".info"];
        if(hasSuffix)
            NSLog(@"有对应后缀");
        else
            NSLog(@"没有对应后缀");
 
        //判断两个字符串是否相同
        NSString *str11 = @"hello";
        NSString *str12 = @"hello";
        if([str11 isEqualToString:str12])
            NSLog(@"两个字符串一致");
        else
            NSLog(@"两个字符串不一致");
 
        //按照制定字符分割字符串
        NSString *str13 = @"a,b,c,d,e,f,g";
        NSArray *strArray = [str13 componentsSeparatedByString:@","];
        for(NSArray *str in strArray){
            NSLog(@"str = %@",str);
        }
 
        //按照范围截取字符串
        NSRange range = NSMakeRange(1, 5);
        NSString *str14 = [str13 substringWithRange:range];
        NSLog(@"str14 = %@",str14);
 
        //从某一位开始截取后面的字符串
        NSString *str15 = [str13 substringFromIndex:2];
        NSLog(@"str15 = %@",str15);
 
        //从开头截取到某一位
        NSString *str16 = [str13 substringToIndex:7];
        NSLog(@"str16 = %@",str16);
 
        //将字符串拆分为每一个字符
        for(int i=0;i<[str13 length];i++){
            NSLog(@"%c",[str13 characterAtIndex:i]);
        }
 
        //查找
        NSString *str17 = @"ab cd ef gh ij ab";
        //正向查找指定字符串的位置
        NSRange range1 = [str17 rangeOfString:@"ab"];
        NSLog(@"range1.location:%ld range1.length:%ld",range1.location,range1.length);
 
        //替换
        NSString *str18 = @"hello IOS,hello Jason";
        //替换某一个范围的内容
        NSString *str19 = [str18 stringByReplacingCharactersInRange:NSMakeRange(0,5) withString:@"你好"];
        NSLog(@"str19 = %@",str19);
        //用指定字符串替换源字符串中的字符
        /*
        stringByReplacingOccurrencesOfString - 源字符串中要被替换的内容
        withString - 替换的字符串
         */
        NSString *str20 = [str18 stringByReplacingOccurrencesOfString:@"hello" withString:@"你好"];
        NSLog(@"str20 = %@",str20);
 
        //读取文件
        //文件来源:1.本地文件 2.网络文件
        //路径类
        NSString *str21 = @"www.baidu.com";
        //网络路径
        NSURL *httpURL = [NSURL URLWithString:str21];
        //本地路径
        NSURL *fileUrl = [NSURL fileURLWithPath:str21];
        //读取网络文件
        NSString *httpStr = [NSString stringWithContentsOfURL:httpURL encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"httpStr = %@",httpStr);
        //读取本地文件
        NSString *fileStr = [NSString stringWithContentsOfFile:@"/Users/Jason/Desktop/test.txt" encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"fileStr = %@",fileStr);
 
        //写入文件
        NSString *str22 = @"Hello Jason";
        BOOL isok = [str22 writeToFile:@"/Users/Jason/Desktop/demo.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
        if(isok)
            NSLog(@"文件写入成功");
        else
            NSLog(@"文件写入失败");
    }
    return 0;
}

// // main.m // Demo // // Created by Jingjia Xie on 15/10/29. // Copyright © 2015年 Jingjia Xie. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { char *s = "Hello Objective-C"; NSString *str = @"Hello"; //C转OC字符串 NSString *str1 = [NSString stringWithUTF8String:s]; NSLog(@"str1 = %@",str1); //OC转C字符串 NSLog(@"str2 = %s",[str UTF8String]); //创建字符串 NSString *str3 = @"IOS"; NSString *str4 = [[NSString alloc] init]; str4 = @"IOS"; //***格式化字符串*** int a = 10; int b = 20; NSString *str5 = [NSString stringWithFormat:@"a=%d b=%d",a,b]; NSLog(@"str5 = %@",str5); //拼接字符串 NSString *str6 = [str5 stringByAppendingString:str3]; NSLog(@"str6 = %@",str6); //转换小写 NSString *str7 = @"AbCdEF"; NSString *str8 = [str7 lowercaseString]; NSLog(@"str8 = %@",str8); //转换大写 NSString *str9 = [str7 uppercaseString]; NSLog(@"str9 = %@",str9); //判断前缀 NSString *str10 = @"www.xj123.info"; BOOL hasPrefix = [str10 hasPrefix:@"www."]; if(hasPrefix) NSLog(@"有对应前缀"); else NSLog(@"没有对应前缀"); //判断后缀 BOOL hasSuffix = [str10 hasSuffix:@".info"]; if(hasSuffix) NSLog(@"有对应后缀"); else NSLog(@"没有对应后缀"); //判断两个字符串是否相同 NSString *str11 = @"hello"; NSString *str12 = @"hello"; if([str11 isEqualToString:str12]) NSLog(@"两个字符串一致"); else NSLog(@"两个字符串不一致"); //按照制定字符分割字符串 NSString *str13 = @"a,b,c,d,e,f,g"; NSArray *strArray = [str13 componentsSeparatedByString:@","]; for(NSArray *str in strArray){ NSLog(@"str = %@",str); } //按照范围截取字符串 NSRange range = NSMakeRange(1, 5); NSString *str14 = [str13 substringWithRange:range]; NSLog(@"str14 = %@",str14); //从某一位开始截取后面的字符串 NSString *str15 = [str13 substringFromIndex:2]; NSLog(@"str15 = %@",str15); //从开头截取到某一位 NSString *str16 = [str13 substringToIndex:7]; NSLog(@"str16 = %@",str16); //将字符串拆分为每一个字符 for(int i=0;i<[str13 length];i++){ NSLog(@"%c",[str13 characterAtIndex:i]); } //查找 NSString *str17 = @"ab cd ef gh ij ab"; //正向查找指定字符串的位置 NSRange range1 = [str17 rangeOfString:@"ab"]; NSLog(@"range1.location:%ld range1.length:%ld",range1.location,range1.length); //替换 NSString *str18 = @"hello IOS,hello Jason"; //替换某一个范围的内容 NSString *str19 = [str18 stringByReplacingCharactersInRange:NSMakeRange(0,5) withString:@"你好"]; NSLog(@"str19 = %@",str19); //用指定字符串替换源字符串中的字符 /* stringByReplacingOccurrencesOfString - 源字符串中要被替换的内容 withString - 替换的字符串 */ NSString *str20 = [str18 stringByReplacingOccurrencesOfString:@"hello" withString:@"你好"]; NSLog(@"str20 = %@",str20); //读取文件 //文件来源:1.本地文件 2.网络文件 //路径类 NSString *str21 = @"www.baidu.com"; //网络路径 NSURL *httpURL = [NSURL URLWithString:str21]; //本地路径 NSURL *fileUrl = [NSURL fileURLWithPath:str21]; //读取网络文件 NSString *httpStr = [NSString stringWithContentsOfURL:httpURL encoding:NSUTF8StringEncoding error:nil]; NSLog(@"httpStr = %@",httpStr); //读取本地文件 NSString *fileStr = [NSString stringWithContentsOfFile:@"/Users/Jason/Desktop/test.txt" encoding:NSUTF8StringEncoding error:nil]; NSLog(@"fileStr = %@",fileStr); //写入文件 NSString *str22 = @"Hello Jason"; BOOL isok = [str22 writeToFile:@"/Users/Jason/Desktop/demo.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil]; if(isok) NSLog(@"文件写入成功"); else NSLog(@"文件写入失败"); } return 0; }

2015-10-29 15:56:52.285 Demo[1489:30978] str1 = Hello Objective-C
2015-10-29 15:56:52.286 Demo[1489:30978] str2 = Hello
2015-10-29 15:56:52.286 Demo[1489:30978] str5 = a=10 b=20
2015-10-29 15:56:52.286 Demo[1489:30978] str6 = a=10 b=20IOS
2015-10-29 15:56:52.287 Demo[1489:30978] str8 = abcdef
2015-10-29 15:56:52.287 Demo[1489:30978] str9 = ABCDEF
2015-10-29 15:56:52.287 Demo[1489:30978] 有对应前缀
2015-10-29 15:56:52.287 Demo[1489:30978] 有对应后缀
2015-10-29 15:56:52.287 Demo[1489:30978] 两个字符串一致
2015-10-29 15:56:52.287 Demo[1489:30978] str = a
2015-10-29 15:56:52.287 Demo[1489:30978] str = b
2015-10-29 15:56:52.287 Demo[1489:30978] str = c
2015-10-29 15:56:52.287 Demo[1489:30978] str = d
2015-10-29 15:56:52.287 Demo[1489:30978] str = e
2015-10-29 15:56:52.288 Demo[1489:30978] str = f
2015-10-29 15:56:52.288 Demo[1489:30978] str = g
2015-10-29 15:56:52.288 Demo[1489:30978] str14 = ,b,c,
2015-10-29 15:56:52.288 Demo[1489:30978] str15 = b,c,d,e,f,g
2015-10-29 15:56:52.288 Demo[1489:30978] str16 = a,b,c,d
2015-10-29 15:56:52.288 Demo[1489:30978] a
2015-10-29 15:56:52.288 Demo[1489:30978] ,
2015-10-29 15:56:52.288 Demo[1489:30978] b
2015-10-29 15:56:52.288 Demo[1489:30978] ,
2015-10-29 15:56:52.288 Demo[1489:30978] c
2015-10-29 15:56:52.288 Demo[1489:30978] ,
2015-10-29 15:56:52.288 Demo[1489:30978] d
2015-10-29 15:56:52.288 Demo[1489:30978] ,
2015-10-29 15:56:52.289 Demo[1489:30978] e
2015-10-29 15:56:52.289 Demo[1489:30978] ,
2015-10-29 15:56:52.289 Demo[1489:30978] f
2015-10-29 15:56:52.289 Demo[1489:30978] ,
2015-10-29 15:56:52.289 Demo[1489:30978] g
2015-10-29 15:56:52.289 Demo[1489:30978] range1.location:0 range1.length:2
2015-10-29 15:56:52.289 Demo[1489:30978] str19 = 你好 IOS,hello Jason
2015-10-29 15:56:52.289 Demo[1489:30978] str20 = 你好 IOS,你好 Jason
2015-10-29 15:56:52.307 Demo[1489:30978] httpStr = (null)
2015-10-29 15:56:52.308 Demo[1489:30978] fileStr = (null)
2015-10-29 15:56:52.313 Demo[1489:30978] 文件写入成功
Program ended with exit code: 0

2015-10-29 15:56:52.285 Demo[1489:30978] str1 = Hello Objective-C 2015-10-29 15:56:52.286 Demo[1489:30978] str2 = Hello 2015-10-29 15:56:52.286 Demo[1489:30978] str5 = a=10 b=20 2015-10-29 15:56:52.286 Demo[1489:30978] str6 = a=10 b=20IOS 2015-10-29 15:56:52.287 Demo[1489:30978] str8 = abcdef 2015-10-29 15:56:52.287 Demo[1489:30978] str9 = ABCDEF 2015-10-29 15:56:52.287 Demo[1489:30978] 有对应前缀 2015-10-29 15:56:52.287 Demo[1489:30978] 有对应后缀 2015-10-29 15:56:52.287 Demo[1489:30978] 两个字符串一致 2015-10-29 15:56:52.287 Demo[1489:30978] str = a 2015-10-29 15:56:52.287 Demo[1489:30978] str = b 2015-10-29 15:56:52.287 Demo[1489:30978] str = c 2015-10-29 15:56:52.287 Demo[1489:30978] str = d 2015-10-29 15:56:52.287 Demo[1489:30978] str = e 2015-10-29 15:56:52.288 Demo[1489:30978] str = f 2015-10-29 15:56:52.288 Demo[1489:30978] str = g 2015-10-29 15:56:52.288 Demo[1489:30978] str14 = ,b,c, 2015-10-29 15:56:52.288 Demo[1489:30978] str15 = b,c,d,e,f,g 2015-10-29 15:56:52.288 Demo[1489:30978] str16 = a,b,c,d 2015-10-29 15:56:52.288 Demo[1489:30978] a 2015-10-29 15:56:52.288 Demo[1489:30978] , 2015-10-29 15:56:52.288 Demo[1489:30978] b 2015-10-29 15:56:52.288 Demo[1489:30978] , 2015-10-29 15:56:52.288 Demo[1489:30978] c 2015-10-29 15:56:52.288 Demo[1489:30978] , 2015-10-29 15:56:52.288 Demo[1489:30978] d 2015-10-29 15:56:52.288 Demo[1489:30978] , 2015-10-29 15:56:52.289 Demo[1489:30978] e 2015-10-29 15:56:52.289 Demo[1489:30978] , 2015-10-29 15:56:52.289 Demo[1489:30978] f 2015-10-29 15:56:52.289 Demo[1489:30978] , 2015-10-29 15:56:52.289 Demo[1489:30978] g 2015-10-29 15:56:52.289 Demo[1489:30978] range1.location:0 range1.length:2 2015-10-29 15:56:52.289 Demo[1489:30978] str19 = 你好 IOS,hello Jason 2015-10-29 15:56:52.289 Demo[1489:30978] str20 = 你好 IOS,你好 Jason 2015-10-29 15:56:52.307 Demo[1489:30978] httpStr = (null) 2015-10-29 15:56:52.308 Demo[1489:30978] fileStr = (null) 2015-10-29 15:56:52.313 Demo[1489:30978] 文件写入成功 Program ended with exit code: 0

NSMutableString
//
//  main.m
//  Demo
//
//  Created by Jingjia Xie on 15/10/29.
//  Copyright © 2015年 Jingjia Xie. All rights reserved.
//
 
#import <Foundation/Foundation.h>
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
 
        //可变字符串是字符串的子类
        NSMutableString *str = [[NSMutableString alloc] initWithCapacity:10];
        [str setString:@"Hello"];
 
        //1.追加字符串
        [str appendString:@" world"];
        NSLog(@"str = %@",str);
        int a = 10;
        [str appendFormat:@" - %d",a];
        NSLog(@"str = %@",str);
 
        //2.替换字符串
        NSRange range = [str rangeOfString:@"world"];
        [str replaceCharactersInRange:range withString:@"IOS"];
        NSLog(@"str = %@",str);
 
        //3.插入字符串
        [str insertString:@"A" atIndex:6];
        NSLog(@"str = %@",str);
 
        //4.删除字符串
        NSRange range1 = [str rangeOfString:@"AIOS"];
        [str deleteCharactersInRange:range1];
        NSLog(@"str = %@",str);
 
    }
    return 0;
}

// // main.m // Demo // // Created by Jingjia Xie on 15/10/29. // Copyright © 2015年 Jingjia Xie. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //可变字符串是字符串的子类 NSMutableString *str = [[NSMutableString alloc] initWithCapacity:10]; [str setString:@"Hello"]; //1.追加字符串 [str appendString:@" world"]; NSLog(@"str = %@",str); int a = 10; [str appendFormat:@" - %d",a]; NSLog(@"str = %@",str); //2.替换字符串 NSRange range = [str rangeOfString:@"world"]; [str replaceCharactersInRange:range withString:@"IOS"]; NSLog(@"str = %@",str); //3.插入字符串 [str insertString:@"A" atIndex:6]; NSLog(@"str = %@",str); //4.删除字符串 NSRange range1 = [str rangeOfString:@"AIOS"]; [str deleteCharactersInRange:range1]; NSLog(@"str = %@",str); } return 0; }

2015-10-30 01:23:30.268 Demo[1926:51623] str = Hello world
2015-10-30 01:23:30.270 Demo[1926:51623] str = Hello world - 10
2015-10-30 01:23:30.270 Demo[1926:51623] str = Hello IOS - 10
2015-10-30 01:23:30.270 Demo[1926:51623] str = Hello AIOS - 10
2015-10-30 01:23:30.270 Demo[1926:51623] str = Hello  - 10
Program ended with exit code: 0

2015-10-30 01:23:30.268 Demo[1926:51623] str = Hello world 2015-10-30 01:23:30.270 Demo[1926:51623] str = Hello world - 10 2015-10-30 01:23:30.270 Demo[1926:51623] str = Hello IOS - 10 2015-10-30 01:23:30.270 Demo[1926:51623] str = Hello AIOS - 10 2015-10-30 01:23:30.270 Demo[1926:51623] str = Hello - 10 Program ended with exit code: 0

NSArray
//
//  main.m
//  Demo
//
//  Created by Jingjia Xie on 15/10/29.
//  Copyright © 2015年 Jingjia Xie. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import "Person.h"
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
 
        //OC的数组可以存储不同类型的对象,OC的数组只能存储对象
        //实例化的时候必须对数组进行赋值
        NSArray *array1 = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
        //数组的长度
        int count = (int)array1.count;
        NSLog(@"count = %d",count);
 
        //判断数组中是否包含对应的对象
        BOOL isHave = [array1 containsObject:@"2"];
        if(isHave)
            NSLog(@"存在");
        else
            NSLog(@"不存在");
 
        //取得数组中最后一个元素
        NSString *str = [array1 lastObject];
        NSLog(@"str = %@",str);
 
        //取得数组中第一个元素
        str = [array1 firstObject];
        NSLog(@"str = %@",str);
 
        //取出我们所需的元素
        //取出数组中下标为1的元素**
        str = [array1 objectAtIndex:3];
        NSLog(@"str = %@",str);
 
        //打印元素对应的下标(如果元素不存在则打印-1值)
        int index = (int)[array1 indexOfObject:@"3"];
        NSLog(@"index = %d",index);
 
        //数组的遍历(1.基本的for循环通过下标逐一取出查看。2.for in快速枚举。3.枚举器(迭代器))
        Person *p = [[Person alloc] init];
        p.personName = @"张三";
        NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"b",p,@"c", nil];
 
        // 1
        for(int i=0;i<array1.count;i++){
            NSString *str1 = [array1 objectAtIndex:i];
            NSLog(@"str1 = %@",str1);
        }
 
        // 2(如果使用快速枚举,我们需要让数据中的元素类型保持一致)
        for(NSString *str2 in array1){
            NSLog(@"str2 = %@",str2);
        }
 
    }
    return 0;
}

// // main.m // Demo // // Created by Jingjia Xie on 15/10/29. // Copyright © 2015年 Jingjia Xie. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { //OC的数组可以存储不同类型的对象,OC的数组只能存储对象 //实例化的时候必须对数组进行赋值 NSArray *array1 = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil]; //数组的长度 int count = (int)array1.count; NSLog(@"count = %d",count); //判断数组中是否包含对应的对象 BOOL isHave = [array1 containsObject:@"2"]; if(isHave) NSLog(@"存在"); else NSLog(@"不存在"); //取得数组中最后一个元素 NSString *str = [array1 lastObject]; NSLog(@"str = %@",str); //取得数组中第一个元素 str = [array1 firstObject]; NSLog(@"str = %@",str); //取出我们所需的元素 //取出数组中下标为1的元素** str = [array1 objectAtIndex:3]; NSLog(@"str = %@",str); //打印元素对应的下标(如果元素不存在则打印-1值) int index = (int)[array1 indexOfObject:@"3"]; NSLog(@"index = %d",index); //数组的遍历(1.基本的for循环通过下标逐一取出查看。2.for in快速枚举。3.枚举器(迭代器)) Person *p = [[Person alloc] init]; p.personName = @"张三"; NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"b",p,@"c", nil]; // 1 for(int i=0;i<array1.count;i++){ NSString *str1 = [array1 objectAtIndex:i]; NSLog(@"str1 = %@",str1); } // 2(如果使用快速枚举,我们需要让数据中的元素类型保持一致) for(NSString *str2 in array1){ NSLog(@"str2 = %@",str2); } } return 0; }

2015-10-30 01:53:33.616 Demo[2409:65254] count = 5
2015-10-30 01:53:33.620 Demo[2409:65254] 存在
2015-10-30 01:53:33.621 Demo[2409:65254] str = 5
2015-10-30 01:53:33.622 Demo[2409:65254] str = 1
2015-10-30 01:53:33.622 Demo[2409:65254] str = 4
2015-10-30 01:53:33.622 Demo[2409:65254] index = 2
2015-10-30 01:53:33.623 Demo[2409:65254] str1 = 1
2015-10-30 01:53:33.623 Demo[2409:65254] str1 = 2
2015-10-30 01:53:33.624 Demo[2409:65254] str1 = 3
2015-10-30 01:53:33.624 Demo[2409:65254] str1 = 4
2015-10-30 01:53:33.625 Demo[2409:65254] str1 = 5
2015-10-30 01:53:33.625 Demo[2409:65254] str2 = a
2015-10-30 01:53:33.625 Demo[2409:65254] str2 = b
2015-10-30 01:53:33.626 Demo[2409:65254] str2 = <Person: 0x100203f20>
2015-10-30 01:53:33.626 Demo[2409:65254] str2 = c
Program ended with exit code: 0

2015-10-30 01:53:33.616 Demo[2409:65254] count = 5 2015-10-30 01:53:33.620 Demo[2409:65254] 存在 2015-10-30 01:53:33.621 Demo[2409:65254] str = 5 2015-10-30 01:53:33.622 Demo[2409:65254] str = 1 2015-10-30 01:53:33.622 Demo[2409:65254] str = 4 2015-10-30 01:53:33.622 Demo[2409:65254] index = 2 2015-10-30 01:53:33.623 Demo[2409:65254] str1 = 1 2015-10-30 01:53:33.623 Demo[2409:65254] str1 = 2 2015-10-30 01:53:33.624 Demo[2409:65254] str1 = 3 2015-10-30 01:53:33.624 Demo[2409:65254] str1 = 4 2015-10-30 01:53:33.625 Demo[2409:65254] str1 = 5 2015-10-30 01:53:33.625 Demo[2409:65254] str2 = a 2015-10-30 01:53:33.625 Demo[2409:65254] str2 = b 2015-10-30 01:53:33.626 Demo[2409:65254] str2 = <Person: 0x100203f20> 2015-10-30 01:53:33.626 Demo[2409:65254] str2 = c Program ended with exit code: 0

NSMutableArray
//
//  main.m
//  Demo
//
//  Created by Jingjia Xie on 15/10/29.
//  Copyright © 2015年 Jingjia Xie. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import "Person.h"
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
 
        Person *p1 = [[Person alloc] initWithName:@"张三"];
        Person *p2 = [[Person alloc] initWithName:@"李四"];
        Person *p3 = [[Person alloc] initWithName:@"王五"];
        NSArray *personArray = [[NSArray alloc] initWithObjects:p2,p3, nil];
 
        NSMutableArray *array = [[NSMutableArray alloc] init];
        //添加元素
        [array addObject:p1];
        [array addObjectsFromArray:personArray];
        NSLog(@"%@",array);
 
        //删除元素
        //删除数组内所有的元素
        //[array removeAllObjects];
 
        //删除最有一个元素
        //[array removeLastObject];
 
        //删除指定元素
        [array removeObject:p2];
 
        //删除制定下标的元素(逐一数组内元素的个数 下标问题会导致程序崩溃)
        //[array removeObjectAtIndex:2];
        NSLog(@"2.%@",array);
 
        //交换元素的位置
        [array exchangeObjectAtIndex:0 withObjectAtIndex:1];
        NSLog(@"3.%@",array);
 
    }
    return 0;
}

// // main.m // Demo // // Created by Jingjia Xie on 15/10/29. // Copyright © 2015年 Jingjia Xie. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { Person *p1 = [[Person alloc] initWithName:@"张三"]; Person *p2 = [[Person alloc] initWithName:@"李四"]; Person *p3 = [[Person alloc] initWithName:@"王五"]; NSArray *personArray = [[NSArray alloc] initWithObjects:p2,p3, nil]; NSMutableArray *array = [[NSMutableArray alloc] init]; //添加元素 [array addObject:p1]; [array addObjectsFromArray:personArray]; NSLog(@"%@",array); //删除元素 //删除数组内所有的元素 //[array removeAllObjects]; //删除最有一个元素 //[array removeLastObject]; //删除指定元素 [array removeObject:p2]; //删除制定下标的元素(逐一数组内元素的个数 下标问题会导致程序崩溃) //[array removeObjectAtIndex:2]; NSLog(@"2.%@",array); //交换元素的位置 [array exchangeObjectAtIndex:0 withObjectAtIndex:1]; NSLog(@"3.%@",array); } return 0; }

2015-10-30 02:10:21.988 Demo[2728:71954] (
    "<Person: 0x100102f40>",
    "<Person: 0x100100eb0>",
    "<Person: 0x100100310>"
)
2015-10-30 02:10:21.989 Demo[2728:71954] 2.(
    "<Person: 0x100102f40>",
    "<Person: 0x100100310>"
)
2015-10-30 02:10:21.989 Demo[2728:71954] 3.(
    "<Person: 0x100100310>",
    "<Person: 0x100102f40>"
)
Program ended with exit code: 0

2015-10-30 02:10:21.988 Demo[2728:71954] ( "<Person: 0x100102f40>", "<Person: 0x100100eb0>", "<Person: 0x100100310>" ) 2015-10-30 02:10:21.989 Demo[2728:71954] 2.( "<Person: 0x100102f40>", "<Person: 0x100100310>" ) 2015-10-30 02:10:21.989 Demo[2728:71954] 3.( "<Person: 0x100100310>", "<Person: 0x100102f40>" ) Program ended with exit code: 0

NSDictionary
//
//  main.m
//  Demo
//
//  Created by Jingjia Xie on 15/10/29.
//  Copyright © 2015年 Jingjia Xie. All rights reserved.
//
 
#import <Foundation/Foundation.h>
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
 
        /*
         字典
         存储的内存不是连续的
         用key和value进行对应(键值)
         kvc 键值编码
         */
 
        NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"1" forKey:@"a"];
        NSLog(@"dict1 = %@",dict1);
 
        NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"2",@"3", nil] forKeys:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
        NSLog(@"dict2 = %@",dict2);
 
        NSDictionary *dict3 = @{@"1":@"a",@"2":@"b"};
        NSLog(@"dict3 = %@",dict3);
 
        int count = (int)[dict2 count];
        NSLog(@"count = %d",count);
 
        NSString *value = [dict2 valueForKey:@"b"];
        NSLog(@"value = %@",value);
 
        NSString *value2 = [dict2 objectForKey:@"b"];
        NSLog(@"value2 = %@",value2);
 
        NSArray *allValues = [dict2 allValues];
        NSLog(@"allValus = %@",allValues);
 
        NSArray *allKeys = [dict2 allKeys];
        NSLog(@"allKeys = %@",allKeys);
 
        NSArray *array = [dict2 objectsForKeys:[NSArray arrayWithObjects: @"a",@"b",@"d", nil] notFoundMarker:@"not found"];
        NSLog(@"array = %@",array);
 
        //遍历字典
        for(NSString *key in dict2){
            NSLog(@"%@ = %@",key,[dict2 objectForKey:key]);
        }
 
    }
    return 0;
}

// // main.m // Demo // // Created by Jingjia Xie on 15/10/29. // Copyright © 2015年 Jingjia Xie. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { /* 字典 存储的内存不是连续的 用key和value进行对应(键值) kvc 键值编码 */ NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"1" forKey:@"a"]; NSLog(@"dict1 = %@",dict1); NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"2",@"3", nil] forKeys:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]]; NSLog(@"dict2 = %@",dict2); NSDictionary *dict3 = @{@"1":@"a",@"2":@"b"}; NSLog(@"dict3 = %@",dict3); int count = (int)[dict2 count]; NSLog(@"count = %d",count); NSString *value = [dict2 valueForKey:@"b"]; NSLog(@"value = %@",value); NSString *value2 = [dict2 objectForKey:@"b"]; NSLog(@"value2 = %@",value2); NSArray *allValues = [dict2 allValues]; NSLog(@"allValus = %@",allValues); NSArray *allKeys = [dict2 allKeys]; NSLog(@"allKeys = %@",allKeys); NSArray *array = [dict2 objectsForKeys:[NSArray arrayWithObjects: @"a",@"b",@"d", nil] notFoundMarker:@"not found"]; NSLog(@"array = %@",array); //遍历字典 for(NSString *key in dict2){ NSLog(@"%@ = %@",key,[dict2 objectForKey:key]); } } return 0; }

2015-10-30 12:14:05.074 Demo[3076:81279] dict1 = {
    a = 1;
}
2015-10-30 12:14:05.076 Demo[3076:81279] dict2 = {
    a = 1;
    b = 2;
    c = 3;
}
2015-10-30 12:14:05.076 Demo[3076:81279] dict3 = {
    1 = a;
    2 = b;
}
2015-10-30 12:14:05.076 Demo[3076:81279] count = 3
2015-10-30 12:14:05.076 Demo[3076:81279] value = 2
2015-10-30 12:14:05.076 Demo[3076:81279] value2 = 2
2015-10-30 12:14:05.076 Demo[3076:81279] allValus = (
    1,
    2,
    3
)
2015-10-30 12:14:05.076 Demo[3076:81279] allKeys = (
    a,
    b,
    c
)
2015-10-30 12:14:05.076 Demo[3076:81279] array = (
    1,
    2,
    "not found"
)
2015-10-30 12:14:05.077 Demo[3076:81279] a = 1
2015-10-30 12:14:05.077 Demo[3076:81279] b = 2
2015-10-30 12:14:05.077 Demo[3076:81279] c = 3
Program ended with exit code: 0

2015-10-30 12:14:05.074 Demo[3076:81279] dict1 = { a = 1; } 2015-10-30 12:14:05.076 Demo[3076:81279] dict2 = { a = 1; b = 2; c = 3; } 2015-10-30 12:14:05.076 Demo[3076:81279] dict3 = { 1 = a; 2 = b; } 2015-10-30 12:14:05.076 Demo[3076:81279] count = 3 2015-10-30 12:14:05.076 Demo[3076:81279] value = 2 2015-10-30 12:14:05.076 Demo[3076:81279] value2 = 2 2015-10-30 12:14:05.076 Demo[3076:81279] allValus = ( 1, 2, 3 ) 2015-10-30 12:14:05.076 Demo[3076:81279] allKeys = ( a, b, c ) 2015-10-30 12:14:05.076 Demo[3076:81279] array = ( 1, 2, "not found" ) 2015-10-30 12:14:05.077 Demo[3076:81279] a = 1 2015-10-30 12:14:05.077 Demo[3076:81279] b = 2 2015-10-30 12:14:05.077 Demo[3076:81279] c = 3 Program ended with exit code: 0

NSMutableDictionary
//
//  main.m
//  Demo
//
//  Created by Jingjia Xie on 15/10/29.
//  Copyright © 2015年 Jingjia Xie. All rights reserved.
//
 
#import <Foundation/Foundation.h>
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
 
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        //添加键值对
        [dict setObject:@"1" forKey:@"a"];
        [dict setObject:@"2" forKey:@"b"];
 
        //删除键值对
        //删除所有
        [dict removeAllObjects];
 
        //删除指定的键值对
        [dict removeObjectForKey:@"b"];
 
        //删除多个键值对
        [dict removeObjectsForKeys:[NSArray arrayWithObjects:@"a",@"b", nil]];
 
    }
    return 0;
}

// // main.m // Demo // // Created by Jingjia Xie on 15/10/29. // Copyright © 2015年 Jingjia Xie. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; //添加键值对 [dict setObject:@"1" forKey:@"a"]; [dict setObject:@"2" forKey:@"b"]; //删除键值对 //删除所有 [dict removeAllObjects]; //删除指定的键值对 [dict removeObjectForKey:@"b"]; //删除多个键值对 [dict removeObjectsForKeys:[NSArray arrayWithObjects:@"a",@"b", nil]]; } return 0; }

原文地址 : https://www.xj123.info/6332.html

本站遵循 : 署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5)

版权声明 : 原创文章转载时,请务必以超链接形式标明文章原始出处

Tags: IOS , Objective-C
  • 上一篇:Objective-C 中面向对象基础笔记
  • 下一篇:MySQL替换指定字段中的字符串
2条评论
  1. adamfei 说:

    赞一个,老谢开始学OC了,为什么不直接用swift呢?虽然我没写过swift……

    POST:2015-11-18 00:17 回复
  2. 子痕 说:

    我纯粹是路过打酱油的。。。

    POST:2015-11-22 21:43 回复
发表评论 点击取消评论.

*必填

*必填

  • 文章归档
  • 子网计算
  • 我的共享
  • 锻炼计划
  • 给我留言
  • 关于老谢
2025 年 6 月
一 二 三 四 五 六 日
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« 5 月    

最新文章

  • 认知,是否是一座大山?当架构决策变成配置清单比价
  • 重装博客服务器环境
  • 特斯拉24款标续 Model Y 2万公里使用体验
  • 接盘的傻子
  • 小牛us电瓶指示灯闪三次不上电
  • 一次还不错的小米售后体验
  • 装台1600元办公主机
  • 2021好久没更新博客
  • Zabbix监控oxidized备份状态
  • Zabbix 5.0 LTS版本MySQL表分区及编译安装随记

最新评论

  • zwwooooo:类似以前做网站开发时,一开始有自...
  • 老陳网志:有点高端,像我们整点nas玩玩就够...
  • springwood:自从 CentOS 不维护之后,我换 U...
  • 大D:难都搞下来了,那就更得YM了
  • 大D:只能是YM了,谢总牛啊
  • 灰常记忆:经济不好 今年我也换了机器 一...
  • 大峰:这是海外服务器嘛?速度挺快的。
  • 大D:只能单走一个6了哈哈哈
  • zwwooooo:买特斯拉和买iPhone的人群其实相似...
  • 平安家属子痕:一直坚持油车,看你写的心里有...

日志存档

  • 2025 年 5 月
  • 2025 年 4 月
  • 2025 年 3 月
  • 2024 年 9 月
  • 2024 年 5 月
  • 2024 年 1 月
  • 2023 年 4 月
  • 2021 年 10 月
  • 2021 年 4 月
  • 2021 年 3 月
  • 2021 年 2 月
  • 2020 年 11 月
  • 2020 年 9 月
  • 2020 年 5 月
  • 2020 年 4 月
  • 2020 年 3 月
  • 2020 年 1 月
  • 2019 年 12 月
  • 2019 年 10 月
  • 2019 年 7 月
  • 2019 年 6 月
  • 2019 年 5 月
  • 2019 年 3 月
  • 2019 年 1 月
  • 2018 年 12 月
  • 2018 年 11 月
  • 2018 年 10 月
  • 2018 年 7 月
  • 2018 年 6 月
  • 2018 年 5 月
  • 2018 年 4 月
  • 2018 年 3 月
  • 2018 年 1 月
  • 2017 年 10 月
  • 2017 年 9 月
  • 2017 年 8 月
  • 2017 年 7 月
  • 2017 年 2 月
  • 2017 年 1 月
  • 2016 年 12 月
  • 2016 年 11 月
  • 2016 年 10 月
  • 2016 年 7 月
  • 2016 年 6 月
  • 2016 年 4 月
  • 2016 年 2 月
  • 2016 年 1 月
  • 2015 年 12 月
  • 2015 年 10 月
  • 2015 年 9 月
  • 2015 年 7 月
  • 2015 年 5 月
  • 2015 年 4 月
  • 2015 年 3 月
  • 2015 年 2 月
  • 2015 年 1 月
  • 2014 年 12 月
  • 2014 年 10 月
  • 2014 年 9 月
  • 2014 年 8 月
  • 2014 年 7 月
  • 2014 年 6 月
  • 2014 年 5 月
  • 2014 年 4 月
  • 2014 年 3 月
  • 2014 年 2 月
  • 2014 年 1 月
  • 2013 年 12 月
  • 2013 年 11 月
  • 2013 年 10 月
  • 2013 年 9 月
  • 2013 年 8 月
  • 2013 年 7 月
  • 2013 年 6 月
  • 2013 年 5 月
  • 2013 年 4 月
  • 2013 年 3 月
  • 2013 年 2 月
  • 2013 年 1 月
  • 2012 年 12 月
  • 2012 年 11 月
  • 2012 年 9 月
  • 2012 年 8 月
  • 2012 年 7 月
  • 2012 年 6 月
  • 2012 年 5 月
  • 2012 年 4 月
  • 2012 年 3 月
  • 2012 年 2 月
  • 2012 年 1 月
  • 2011 年 12 月
  • 2011 年 11 月
  • 2011 年 10 月
  • 2011 年 9 月
  • 2011 年 8 月
  • 2011 年 7 月
  • 2011 年 6 月
  • 2011 年 5 月
  • 2011 年 4 月
  • 2011 年 3 月
  • 2011 年 2 月
  • 2011 年 1 月
  • 2010 年 12 月
  • 2010 年 11 月
  • 2010 年 10 月
  • 2010 年 9 月
  • 2010 年 8 月
  • 2010 年 7 月

W3C

  • XHTML 1.0 Transitional
  • CSS level 3
  • Google+
Copyright © 2010-2025 老谢博客 All rights reserved.
Gzipped 76.5% | Optimized loading 47 queries in 0.367 seconds | Memory 39.11 MB | 尼玛的备案
Powered by WordPress. | Hosted By LAOXUEHOST | Theme by WordPress主题巴士 | 站点地图 | SiteMap | uptime查询