设为首页 加入收藏

TOP

ObjectC----几个常用的类(二)
2015-11-19 23:07:25 来源: 作者: 【 】 浏览:7
Tags:ObjectC---- 常用
@"%@",[arr objectAtIndex:i]);

// }

//

// 添加一个对象元素

[mulArray addObject:@"guoguo"];

?

// 交换下标对应的元素对象

[mulArray exchangeObjectAtIndex:0 withObjectAtIndex:[mulArray count]-1]; //交换第一个元素和最后一个元素

===================================================================================== 下面通过一个实例来形象的了解: 使用可变数组管理BOOk类,实现 图书的增删查改 BOOK有两个成员变量:_name,_price;

Book * book1 = [[Book alloc] initWithName:@"guozai1" andPrice:10];

Book * book2 = [[Book alloc] initWithName:@"guozai2" andPrice:15];

Book * book3 = [[Book alloc] initWithName:@"guozai3" andPrice:13];

?

//数组赋值

NSMutableArray *books = [NSMutableArray arrayWithObjects:book1,book2,book3, nil];

?

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];

?

//将vPoint转化成结构体

NSPoint point2 = [vPoint pointValue];

// NSLog(@"%.2f,%.2f",point2.x,point2.y);

?

// NSStringFromPoint可以将点转化成字符串

NSLog(@"%@",NSStringFromPoint(point2));


其他的例子类比就行:例如

?

//将NSsize结构体转化成NSValue对象

NSSize size = {22,44};

NSValue * sValue = [NSValue valueWithSize:size];

?

//将NSValue对象转化成NSSize结构体;

NSSize size2 = [sValue sizeva lue];

NSLog(@"%@", NSStringFromSize(size2));

========================================================================================
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言结构体初始化的三种方法 下一篇ObjectC----字典类和集合类以及快..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: