Objective-C:NSValue类的常见用法
2015-08-17 19:20
363 查看
特殊类型的包装类:数组、结构体(OC内部的、自定义的)、指针
// // main.m // 05-NSValue // // Created by ma c on 15/8/17. // Copyright (c) 2015年. All rights reserved. // #import <Foundation/Foundation.h> typedef struct { char *name; int age; char gender; }Student; int main(int argc, const char * argv[]) { @autoreleasepool { //NSValue测试 //1.指针 int num = 10; int *p = # NSValue *value1 = [NSValue valueWithPointer:p]; int *p2 = (int*)[value1 pointerValue]; NSLog(@"num = %d",*(p2)); //2.数组 int arr[5] = {1,2,3,4,5}; NSValue *value2 = [NSValue valueWithPointer:arr]; int *p3 = (int*)[value2 pointerValue]; for(int i=0; i<5; i++) { NSLog(@"%d",p3[i]); } //3.Foundation中常用的结构体:NSRange\NSRect\NSSize\NSPoint NSRange range = NSMakeRange(0, 5); NSValue *value3 = [NSValue valueWithRange:range]; NSRange range2 = [value3 rangeValue]; NSLog(@"%@",NSStringFromRange(range2)); NSRect rect = NSMakeRect(0, 0, 100, 100); NSValue *value4 = [NSValue valueWithRect:rect]; NSRect rect2 = [value4 rectValue]; NSLog(@"%@",NSStringFromRect(rect2)); //4.自定义的结构体 Student stu = {"Tom",20,'M'}; NSValue *value5 = [NSValue valueWithBytes:&stu objCType:@encode(Student)]; NSLog(@"%s",@encode(Student));//输出结构体类型 Student stu2; [value5 getValue:&stu2]; NSLog(@"name = %s,age = %i,gender = %c",stu2.name,stu2.age,stu2.gender); } return 0; }
相关文章推荐
- Objective-C:NSNumber类的常见用法
- Objective-C:NSMutableString类的常见操作
- QObject 出现undefined vtable的解决方法
- Objective-C:NSString类的常见用法
- Objective-C:Foundation框架
- ASP入门(十四)-FileSystemObject 对象
- [caffe]深度学习之CNN检测object detection方法摘要介绍
- Object C学习笔记6-如何在Windows环境搭建Object C开发环境
- 插件开发之无法解释的外部符号 DllCanUnloadNow和DllGetClassObject
- OBjective-C:atomic和nonatomic的区别
- post a json object with an array using curl
- mysql_fetch_row,mysql_fetch_array,mysql_fetch_object,mysql_fetch_assoc
- Objective-C 数据类型定义的标示符(一)
- 给Objective-C的“悼词”
- javascript object 相等判断逻辑
- flex 客户端缓存SharedObject
- org.hibernate.NonUniqueObjectException
- ****Objective-C 中的方法的调用
- android Parcelable 带有数组的对象如何序列化 String[] Long[] int[] Object[]
- 【OC语言】第一篇·Objective-C概述和面向对象