zoukankan      html  css  js  c++  java
  • [好程序员训练营]Objective-C学习笔记---基本数据结构NSValue

      <A href="http://www.gooodprogrammer.org/" target="blank">ios培训</A>------我的OC语言笔记,期待与您交流! 

      NSValue是NSNumber的父类,NSNumber用于将基本数据类型int、char、float等存储成对象,NSValue则可以用于将装指针和结构体等更加复杂的类型存储为对象。因此他们的使用上还是有一定的区别。

     1    struct sct{                   //声明一个结构体
     2         int a;
     3         float b;
     4     };
     5     struct sct sctt={2,6.55f};      //定义一个结构体变量sctt并赋值
     6     
     7     
     8     NSValue * value = [[NSValue alloc] initWithBytes:&sctt objCType:@encode(struct sct)];
     9     //将结构体sctt存储为对象,&sctt是取结构体的地址,@encode(struct sct)是将结构体类型转为字符串
    10     
    11     struct sct newsctt;    //新定义一个结构体变量
    12     
    13     [value getValue:&newsctt];     //对象value执行方法将sctt结构体的值赋给newsctt
    14     
    15     NSLog(@"%d,%f",newsctt.a,newsctt.b);  //将结构体newsctt中的值打印出来

    在上面的代码中,@encode(struct sct)是将数据的类型转换成字符串,比如说int型就转为i,float转为f。上面只是给出了NSValue的一种使用方法,下面看看其他的

     1   
     2   NSValue * value1 = [NSValue valueWithBytes:&sctt objCType:@encode(struct sct)];  //上面的可以用这种方式替代
     3     
     4     NSValue * value2 = [NSValue valueWithPoint:<#(NSPoint)#>];
     5     
     6     NSValue * value3 = [NSValue valueWithPointer:<#(const void *)#>];
     7     
     8     NSValue * value4 = [NSValue valueWithRange:<#(NSRange)#>];
     9     
    10     NSValue * value5 = [NSValue valueWithRect:<#(NSRect)#>];
    11     
    12      NSValue * value6 = [NSValue valueWithPointer:<#(const void *)#>];

    这里也只是列举了一部分的常用存储类型。

  • 相关阅读:
    安装Linux应用软件的五种基本方法
    gprof使用介绍
    Linux内核对象模型(subsystem,kset,kobject)
    kernel 目录 解析
    linux板级设备的初始化过程(转)
    Linux下I2C驱动程序的分析
    I2C总线设备驱动解析
    linux proc文件系统学习 (转)
    cscope的用法
    mvc3 Razor PartialView视图中Html.DropDownList用法
  • 原文地址:https://www.cnblogs.com/ITLiu/p/4555405.html
Copyright © 2011-2022 走看看