Book * book4 = [[Book alloc] initWithName:@"guozai4" andPrice:12];
//添加一本书
[books addObject:book4];
//删除一本书
[books removeObjectAtIndex:2];
for (int i = 0; i < [books count]; i ++) {
NSLog(@"%@,%.2f",[[books objectAtIndex:i] name],[[books objectAtIndex:i] price]);
}
//查找名字是guozai3的书,打印价格
for (int i = 0; i < [books count]; i ++) {
if ([[[books objectAtIndex:i] name] isEqualToString:@"guozai3"]) {
NSLog(@"%f",[[books objectAtIndex:i] price]);
}
}
// 对数组进行排序,按价格从高到低
for (int i = 0; i < [books count] - 1; i ++) {
for (int j = 0; j < [books count] - i - 1; j ++) {
if ([books[j] price] < [books[j+1] price]) {
[books exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
for (int i = 0; i < [books count]; i ++) {
NSLog(@"%@,%.2f",[books[i] name],[books[i] price]);
}
========================================================================================
5.NSNumber:将基本数据类型转化成对象类型
//将基本数据类型int转化为对象类型
NSNumber * intNum = [NSNumber numberWithInt:100];//便利构造器
NSMutableArray * ar = [NSMutableArray arrayWithObjects:intNum, nil];
NSNumber * tem = [ar objectAtIndex:0];
//将对象类型转化成基本数据类型
int result = [tem intValue];
NSLog(@"%d",result);
========================================================================================
6.NSValue:将结构体转化成对象
//将一个点转化成NSValue对象
NSPoint point = {1,2};
//将一个结构体转化成NSValue对象
NSValue *vPoint = [NSValue valueWithPoint:point];