zoukankan      html  css  js  c++  java
  • iPhone开发:综合NSValue和NSData

    将前面的两篇文章综合起来,我们可以随心所欲地保存数据了

    http://blog.csdn.net/iBright/archive/2010/06/08/5655857.aspx

    http://blog.csdn.net/iBright/archive/2010/06/08/5656164.aspx

    代码如下:

    typedef struct _AA{

    NSString *nameA;

    }AA;

    typedef struct _BB{

    int k;

    float m;

    NSString *nameB;

    AA aa;

    }BB;

    BB f[2] = {

    {10,0.5f,@"bright",{@"a1"}},

    {5,0.3f,@"mtf",{@"a2"}}

    };

    NSValue *value1 = [NSValue valueWithBytes:&f[0] objCType: @encode(BB)];

    NSValue *value2 = [NSValue valueWithBytes:&f[1] objCType: @encode(BB)];

    NSArray *array = [NSArray arrayWithObjects:value1,value2,nil];

    NSLog(@"array == %@",array);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    if (!documentsDirectory) {

    NSLog(@"Documents directory not found!");

    }

    NSString *appFile =[documentsDirectory stringByAppendingPathComponent:@"save"];

    NSMutableData *data = [NSMutableData data];

    [data appendBytes:&array length:sizeof(array)];

    NSLog(@"data===%@",data);

    [data writeToFile:appFile atomically:YES]; 

    NSMutableData *readData = [NSMutableData dataWithContentsOfFile:appFile];

    NSLog(@"readData==%@",readData);

    NSArray *final;

    [readData getBytes:&final range:NSMakeRange(0,sizeof(final))];

    NSLog(@"final===%@",final);

    BB c;

    for (NSValue *aValue in final) {

    NSLog(@"%@",aValue);

    [aValue getValue:&c];

    NSLog(@"%d",c.k);

    NSLog(@"%f",c.m);

    NSLog(@"%@",c.nameB);

    NSLog(@"%@",c.aa.nameA);

    }

  • 相关阅读:
    vue-resource请求
    vue的生命周期
    Swift-多类型封装
    Swift
    Swift-structures 和 classes 初始化
    iOS-延时加载,延时初始化
    Swift-Closures
    WKWebView-填坑总结
    存档&&解档游戏状态
    循环引用 && weak strong
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2458481.html
Copyright © 2011-2022 走看看