//
// main.m
// FoundationDemo1
//
// Created by 周泉 on 12-9-28.
// Copyright (c) 2012年 www.iphonetrain.com 无限互联ios开发培训中心. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
/*___________________ NSNumber ___________________*/
// 数字对象的创建
/*
NSNumber *monthNumber = [NSNumber numberWithInt:10];
NSNumber *lengthNumber = [[NSNumber alloc] initWithFloat:10.8];
NSLog(@"monthNumber : %@", monthNumber);
NSLog(@"lengthNumber : %@", lengthNumber);
// 还原成基本数据类型
int month = [monthNumber intValue];
float length = [lengthNumber floatValue];
NSLog(@"month : %d", month);
NSLog(@"length : %.1f", length);
*/
/*___________________ NSString ___________________*/
// 字符串对象的创建
/*
// 创建一个字符串常量
NSString *string1 = @"这是一个字符串常量";
NSLog(@"string1 : %@", string1);
// 创建一个空的字符串
NSString *string2 = [[NSString alloc] init];
NSString *string3 = [NSString string];
NSLog(@"string2 : %@", string2);
NSLog(@"string3 : %@", string3);
// 快速创建一个字符串 常量区(NSString)
NSString *string4 = [[NSString alloc] initWithString:@"abc"];
NSString *string5 = [NSString stringWithString:@"abc"];
NSLog(@"string4 : %@", string4);
NSLog(@"string5 : %@", string5);
// 创建一个格式化的字符串 堆区
NSString *string6 = [[NSString alloc] initWithFormat:@"整数:%d", 10];
NSString *string7 = [NSString stringWithFormat:@"浮点数:%f", 10.8];
NSLog(@"string6 : %@", string6);
NSLog(@"string7 : %@", string7);
// 字符串的比较
NSString *string8 = [NSString stringWithFormat:@"123"];
NSString *string9 = [NSString stringWithFormat:@"123"];
if ([string8 isEqualToString:string9]) {
NSLog(@"same content");
}else {
NSLog(@"different content");
} // 比较字符串的内容
if (string8 == string9) {
NSLog(@"same pointer");
}else {
NSLog(@"different pointer");
} // 比较是否为同一个对象
// 比较字符串的大小
NSString *string10 = [[NSString alloc] initWithFormat:@"z"];
NSString *string11 = [[NSString alloc] initWithFormat:@"abcd"];
NSComparisonResult result = [string10 caseInsensitiveCompare:string11];
NSLog(@"result : %d", result);
// 求字符串的长度
NSLog(@"%d", [string11 length]);
// 字符串的转换
NSString *string12 = @"Hello wOrld";
NSLog(@"upper : %@", [string12 uppercaseString]);
NSLog(@"lower : %@", [string12 lowercaseString]);
NSLog(@"capital : %@", [string12 capitalizedString]);
// 转换成基本数据类型
NSString *string13 = @"3.14";
float pi = [string13 floatValue];
NSLog(@"pi : %.2f", pi);
// 字符串转换成数组
NSString *string14 = @"abc bcd xyz";
NSArray *array = [string14 componentsSeparatedByString:@" "];
NSLog(@"array : %@", array);
// 截取字符串
NSString *string15 = @"abcdefg";
NSString *subString1 = [string15 substringToIndex:2];
NSString *subString2 = [string15 substringFromIndex:2];
NSLog(@"subString1 : %@", subString1);
NSLog(@"subString2 : %@", subString2);
NSRange range = {4, 2};
NSString *subString3 = [string15 substringWithRange:range];
NSLog(@"subString3 : %@", subString3);
// 字符串的拼接
NSString *string16 = @"abc";
NSString *string17 = @"xyz";
NSString *appString1 = [[NSString alloc] initWithFormat:@"%@%@", string16, string17];
NSString *appString2 = [string16 stringByAppendingFormat:@"%@", string17];
NSString *appString3 = [string16 stringByAppendingString:string17];
NSLog(@"appString 1 : %@", appString1);
NSLog(@"appString 2 : %@", appString2);
NSLog(@"appString 3 : %@", appString3);
// 查找字符串
NSString *link = @"abcdeftarget=_blankxyz";
NSRange range1 = [link rangeOfString:@"target=_blank"];
if (range1.location != NSNotFound) {
NSLog(@"founded");
}
////////////////////////////////////////////////////////////
NSMutableString *mutableString1 = [[NSMutableString alloc] initWithFormat:@"abc"];
// 插入一个字符串
[mutableString1 insertString:@"...xyz" atIndex:3];
NSLog(@"mutalbeString1 : %@", mutableString1);
// 替换
[mutableString1 replaceCharactersInRange:NSMakeRange(0, 3) withString:@"efg"];
NSLog(@"mutalbeString1 : %@", mutableString1);
// 删除
[mutableString1 deleteCharactersInRange:NSMakeRange(0, 3)];
NSLog(@"mutalbeString1 : %@", mutableString1);
*/
/*___________________ NSArray ___________________*/
// 数组对象的创建
/*
NSArray *array1 = [NSArray arrayWithObject:@"One"];
NSArray *array2 = [NSArray arrayWithObjects:@"One", @"Two", nil];
NSArray *array3 = [NSArray arrayWithArray:array2];
NSLog(@"array1 : %@", array1);
NSLog(@"array2 : %@", array2);
NSLog(@"array3 : %@", array3);
NSArray *array4 = [[NSArray alloc] initWithArray:array1];
NSLog(@"array4 : %@", array4);
// 获取数组中的元素个数和访问
int count = [array2 count];
NSLog(@"array2 all element :%d", count);
NSString *string1 = [array2 objectAtIndex:1];
NSLog(@"element of array: %@", string1);
// 追加数组中的内容
NSArray *array5 = [array2 arrayByAddingObject:@"end"];
NSLog(@"array5 : %@", array5);
// 数组->字符串
NSString *string2 = [array5 componentsJoinedByString:@","];
NSLog(@"stirng2 : %@", string2);
// 判断数组中是否存在一个指定的对象
BOOL isContain = [array5 containsObject:@"end1"];
NSLog(@"isContain : %d", isContain);
// 根据指定的对象返回索引下标、返回数组中最后一个元素
NSInteger index = [array5 indexOfObject:@"end"];
NSLog(@"index : %d", index);
NSString *lastObject = [array5 lastObject];
NSLog(@"last Object : %@", lastObject);
/////////////////////////////////////////////////////////
// 创建了一个空的数组
NSMutableArray *mutableArray1 = [NSMutableArray arrayWithObjects:@"One", @"Two", @"end", @"Five", nil];
// 新增、插入、删除、替换
NSLog(@"mutableArray1 : %@", mutableArray1);
[mutableArray1 addObject:@"Four"];
NSLog(@"mutableArray1 : %@", mutableArray1);
[mutableArray1 insertObject:@"Three" atIndex:2];
NSLog(@"mutableArray1 : %@", mutableArray1);
[mutableArray1 removeLastObject];
NSLog(@"removeLastObject : %@", mutableArray1);
[mutableArray1 removeObject:@"end"];
NSLog(@"removeObject : %@", mutableArray1);
[mutableArray1 removeObjectAtIndex:3];
NSLog(@"removeObjectAtIndex : %@", mutableArray1);
[mutableArray1 addObject:@"Four"];
NSLog(@"add four: %@", mutableArray1);
NSArray *deleteArray = [NSArray arrayWithObjects:@"One", @"Two", nil];
[mutableArray1 removeObjectsInArray:deleteArray];
NSLog(@"removeObjectsInArray: %@", mutableArray1);
[mutableArray1 replaceObjectAtIndex:0 withObject:@"3"];