//
// main.m
// ArrayAndNSString
//
// Created by on 12-12-24.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// ************* 字符串 NSString and NSMutableStirng**********************************************************
// 使用 NString 类的 实例方法
NSString * str = [[NSString alloc]initWithString:@"iPhone"];
NSLog(@"%@",str);
NSString * str1= [[NSString alloc]initWithFormat:@"%@",@"IPhone"];
NSLog(@"%@",str1);
// 使用 NString 类的 类方法
NSString * str2 = [NSString stringWithString:str];
NSString * str3= [NSString stringWithFormat:@"%@",str1];
NSLog(@"str2 = %@,str3 = %@",str2,str3);
//从文件 或者 URL 创建 字符串
//从文件 创建 字符串 读出 文件内容 赋给字符串
NSString * str4 = [NSString stringWithContentsOfFile:@"/Users/rjxy/Desktop/text.txt" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str4 = %@",str4);
// 写 方法 往 文件里写内容
[str writeToFile:@"/Users/rjxy/Desktop/text.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString * str5 = [[NSString alloc]initWithContentsOfFile:@"/Users/rjxy/Desktop/text.txt" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str5 = %@",str5);
// 字符串的 常用 函数
// 字符串 比较 函数 isEqualToString , compare
if ([str1 isEqualToString:str2]) {
NSLog(@"相等");
}else {
NSLog(@"不相等");
}
NSLog(@"%ld",[str1 compare:str2]);
// NSMutableString 可变字符串 更 高效 是 NSString 类的 子类
NSMutableString * str6 = [[NSMutableString alloc]init];
// ********** 数组 Array and MutableArray ******************************************************
// 数组 可以保存同类型的多个数据对象,操作不方便,大小固定 ,集合分为 set Array 和 Dictionary 三类 集合类似数组,可以保存多个数据,提供了方便的添加删除 查找等方法,每类 又有 可变 (Mutable)和不可变两种
// NSArray, 是不可变集合 1 可以存放多个对象 2 每个对象使用下标 表示 3 最后以 nil 结束 4 可以看作有方法的数组
// 不可变 数组 无法
NSArray * array = [[NSArray alloc]initWithObjects:@"android",@"iphone",@"windows phone", nil];
[array arrayByAddingObject:@"nokia"];
NSLog(@"array = %@",array);
NSString * android = [NSString stringWithString:@"android"];
NSString * iphone = [NSString stringWithString:@"iphone"];
NSString * windowPhone = [NSString stringWithString:@"windowPhone"];
NSArray * phoneArray = [[NSArray alloc]initWithObjects:android,iphone,windowPhone, nil];
// 遍历 NSArray 一般的 for 循环
for (int i= 0; i<phoneArray.count; i++) {
NSLog(@"object %d == %@",i,[phoneArray objectAtIndex:i]);
}
// 使用 for ... in 遍历 集合 更加 简单
for (NSString * object in phoneArray) {
NSLog(@" === %@",object);
}
// 获取 某个下标的 集合 元素
NSLog(@"%@" ,[phoneArray objectAtIndex:0]);
// 可变 数组
NSMutableArray * array1=[[NSMutableArray alloc]initWithObjects:@"iphone", nil];
NSLog(@"%ld",[array1 retainCount]);
NSLog(@"array1 = %@",array1);
[array1 addObject:@"amdroid"]; // 添加 对象 android
NSLog(@"%ld",[array1 retainCount]);
NSLog(@"array1 = %@",array1);
[array1 insertObject:@"windowsPhone" atIndex:0]; // 在下标 为0处 插入 元素 windowsPhone
[array1 insertObject:@"nokia" atIndex:2]; // 在下标 为2处 插入 元素 nokia
NSLog(@"%ld",[array1 retainCount]);
NSLog(@"array1 = %@",array1);
[array1 removeObjectAtIndex:2]; // 删除 下标 为 2 的 元素
NSLog(@"%ld",[array1 retainCount]);
NSLog(@"array1 == %@",array1);
[array1 addObject:phoneArray]; // 数组 会对加入 其中的 对象保留 计数 一次 , 这里 phoneArray 被 add 了 所以 phoneArray 的 retainCount 加 1
NSLog(@"%ld",[phoneArray retainCount]);
NSLog(@"array1 == %@",array1);
// ***** 字典 *********************************************************
// 1 字典 使用 key - value 形式存储数据 2 字典中的对象存储没有 顺序,使用key 来表示每个对象 3 cocoa 框架中的字典,NSDictionary 和 NSMutableDictionary 4 字典中 key 一般用 NSString 或者 NSNumber ,而值 可以是 任一类型 5 值对象不能为 nil
// 创建 Dictionary 对象、
NSDictionary * dictionary = [NSDictionary dictionaryWithObject:str forKey:@"str"];
NSLog(@"dictionary = %@",dictionary);
// 初始化 字典
NSDictionary * dictionary1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2", nil];
// 获取 key - value 的 个数
int count = [dictionary1 count];
NSLog(@"字典key - value 个数 %d",count); //字典key - value 个数 2
// 获取 所有 的 key、
NSArray * allarray = [dictionary1 allKeys];
NSLog(@"all keys = %@",allarray); //all keys = (
// key1,
// key2
// )
// ******** 集合 NSSet and NSMutableSet *****************************************
// NSSet 和 NSMutableSet
// Set 是 无序集合 , 元素 无序 ,不能 存放 相同 的对象,是一组 单值 对象的集合
// 集合 和 内存管理 :当向一个集合对象中 添加一个 对象元素时,集合会主动持有 对它 的 所有权
// 1 、初始化
NSSet * set = [[NSSet alloc]initWithObjects:@"object1",@"object2",@"object3", nil];
// 2 、set 集合的 元素 个数
int setCount = [set count];
NSLog(@"setCount = %d",setCount); //setCount = 3
// 3 、获取 集合 中 的元素
NSLog(@" set = %@",set);
NSLog(@"anyObject = %@",[set anyObject]);
// 4 、比较两个 集合 -(BOOL)isEqualToSet: (NSSet *)otherSet
// 5 、 确定 集合是否 包含 对象 object1
NSLog(@"blog = %@",[set member:@"object1"]);
//NSMutableSet
NSMutableSet * mutableSet = [[NSMutableSet alloc]initWithObjects:@"obj1",@"obj2",@"obj3", nil];
// 添加对象
[mutableSet addObject:@"obj4"];
NSLog(@"mutableSet = %@",mutableSet);
// 删除 对象 obj4
[mutableSet removeObject:@"obj4"];
NSLog(@"mutableSet = %@",mutableSet);
// 删除所有的对象
[mutableSet removeAllObjects];
NSLog(@"mutableSet = %@",mutableSet);
// **********************************************作业
// 作业:1.现有如下定义的字符串: NSMutableString * str=@“iphoneAndroid”,能不能对该字符串进行修改,如果能,请输出删除Android后的新字符串。
//
// 2求字符串“158”和“39”按十进制数值做差后的结果以字符串形式输出
//
// 3取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出,(提示:可变字符串;返回数组)
//
// 4放四个十进制三位数到一个数组中,然后按从小到大排序后组成一个新的数组
}
return 0;
}