7_1_NSStringTest.m
#import <Foundation/Foundation.h>
int main(int argc, char const *argv[])
{
@autoreleasepool
{
unichar data[6] = {97, 98, 99, 100, 101, 102};
// 使用unicode数值数组初始化字符串
NSString* str1 = [[NSString alloc] initWithCharacters: data length:6];
NSLog(@"str1=%@", str1);
// c风格的字符串
char* cstr1 = "hello NSString";
NSString* str2 = [NSString stringWithUTF8String: cstr1];
NSLog(@"str2=%@", str2);
// 将字符串写入文件
[str2 writeToFile: @"myFile.txt"
atomically: YES
encoding: NSUTF8StringEncoding
error: nil];
// 读取文件内容
NSString* str3 = [NSString stringWithContentsOfFile: @"myFile.txt"
encoding: NSUTF8StringEncoding
error: nil];
NSLog(@"str3=%@", str3);
// 以下是常用方法
NSString* str = @"hello ios,";
NSString* book = @"《疯狂IOS讲义》";
str = [str stringByAppendingFormat: @"%@是一本非常不错的图书", book];
NSLog(@"afterstr1=%@", str);
NSLog(@"str length1=%lu", [str length]);
NSLog(@"str length2=%lu", [str lengthOfBytesUsingEncoding: NSUTF8StringEncoding]);
NSLog(@"substr1=%@", [str substringToIndex: 10]);
NSLog(@"substr2=%@", [str substringFromIndex: 2]);
NSLog(@"substr3=%@", [str substringWithRange: NSMakeRange(1, 4)]);
NSRange pos = [str rangeOfString: @"o"];
NSLog(@"substr4=%ld", pos.location);
NSLog(@"substr5=%@", [str uppercaseString]);
NSLog(@"substr6=%@", [str lowercaseString]);
}
}
7_2_NSDateTest.m
#import <Foundation/Foundation.h>
int main(int argc, char const *argv[])
{
@autoreleasepool
{
// 当前时间, 标准暁
NSDate* date1 = [NSDate date];
NSLog(@"date1=%@", date1);
// 1天之后
NSDate* date2 = [[NSDate alloc] initWithTimeIntervalSinceNow: 60 * 60* 24];
NSLog(@"date2=%@", date2);
// 1天前
NSDate* date3 = [[NSDate alloc] initWithTimeIntervalSinceNow: -60 * 60* 24];
NSLog(@"date3=%@", date3);
// 1970之后
NSDate* date4 = [NSDate dateWithTimeIntervalSince1970: 60 * 60* 24 * 365 * 20];
NSLog(@"date4=%@", date4);
// 当前时区
NSLocale* cn = [NSLocale currentLocale];
NSLog(@"当前时间的当前时区=%@", [date1 descriptionWithLocale: cn]);
// 取小/大的时间
NSDate* smallDate = [date1 earlierDate: date2];
NSLog(@"smallDate=%@", smallDate);
NSDate* bigDate = [date1 laterDate: date2];
NSLog(@"bigDate=%@", bigDate);
// 比较时间
NSComparisonResult rslt = [date1 compare: date2];
switch (rslt)
{
case NSOrderedAscending:
NSLog(@"date1小于data2");
break;
case NSOrderedSame:
NSLog(@"date1等于data2");
break;
case NSOrderedDescending:
NSLog(@"date1小于data2");
break;
}
// 时间差, 秒
NSLog(@"date1与date2的时间差 = %g", [date1 timeIntervalSinceDate: date2]);
}
}
7_3_NSObjCopy.m
#import <Foundation/Foundation.h>
int main(int argc, char const *argv[])
{
@autoreleasepool
{
NSMutableString* book = [NSMutableString stringWithString: @"我是一本书"];
NSMutableString* bookCopy = [book mutableCopy];
[bookCopy replaceCharactersInRange:
NSMakeRange(2,3)
withString: @"Android"];
NSLog(@"book的值为: %@", book);
NSLog(@"bookCopy的值为: %@", bookCopy);
}
}
7_5_NSArray.m
#import <Foundation/Foundation.h>
NSComparisonResult intSort(id num1, id num2, void *comtext)
{
int v1 = [num1 intValue];
int v2 = [num2 intValue];
if (v1 < v2)
{
return NSOrderedAscending;
}
else if (v1 > v2)
{
return NSOrderedDescending;
}
else
{
return NSOrderedSame;
}
}
int main(int argc, char const *argv[])
{
@autoreleasepool
{
NSArray* array = [NSArray arrayWithObjects:
@"一a",
@"二b",
@"三c",
nil];
NSLog(@"first item: %@", [array objectAtIndex: 0]);
NSLog(@"secod item: %@", array[1]);
NSLog(@"last item: %@", [array lastObject]);
// 获取从[1,2]的集合
NSArray* arr1 = [array objectsAtIndexes:
[NSIndexSet
indexSetWithIndexesInRange:
NSMakeRange(1,2)]];
NSLog(@"arr1 = %@", arr1);
NSArray* arr2 = [array subarrayWithRange:
NSMakeRange(1,2)];
NSLog(@"arr2 = %@", arr2);
NSLog(@"b在数组中的位置 %ld", [array indexOfObject: @"二b"]);
// 追加元素
array = [array arrayByAddingObject: @"四d"];
NSLog(@"array = %@", array);
array = [array arrayByAddingObjectsFromArray: arr1];
NSLog(@"array = %@", array);
// 排序
array = [array sortedArrayUsingSelector:
@selector(compare:)];
NSLog(@"array排序 = %@", array);
// 数字排序
NSArray* numArr1 = @[
[NSNumber numberWithInt: 20],
[NSNumber numberWithInt: 1],
[NSNumber numberWithInt: -8],
[NSNumber numberWithInt: 9],
];
numArr1 = [numArr1 sortedArrayUsingFunction: intSort context: nil];
NSLog(@"numArr1 = %@", numArr1);
numArr1 = [numArr1 sortedArrayUsingComparator:
^(id obj1, id obj2)
{
int v1 = [obj1 intValue];
int v2 = [obj2 intValue];
if (v1 < v2)
{
return NSOrderedDescending;
}
else if (v1 > v2)
{
return NSOrderedAscending;
}
else
{
return NSOrderedSame;
}
}];
NSLog(@"numArr1 = %@", numArr1);
// 遍历
NSEnumerator* enum1 = [numArr1 objectEnumerator];
id object1;
while(object1 = [enum1 nextObject])
{
NSLog(@"%d", [object1 intValue]);
}
for(id object2 in numArr1)
{
NSLog(@"%d", [object2 intValue]);
}
/* 可变的数组 */
NSMutableArray* marray1 = [NSMutableArray arrayWithObjects:
@"A", nil];
[marray1 addObject: @"C"];
NSLog(@"marray1 = %@", marray1);
}
}
myFile.txt
hello NSString
网友评论