2017-03-15 10:03:01 u011312237 阅读数 493
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>name</key>
        <string>天天酷跑</string>
        <key>icon</key>
        <string>icon_00</string>
    </dict>
          <dict>
        <key>name</key>
        <string>保卫萝卜2</string>
        <key>icon</key>
        <string>icon_10</string>
    </dict>
    <dict>
        <key>name</key>
        <string>神偷奶爸</string>
        <key>icon</key>
        <string>icon_11</string>
    </dict>
</array>

</plist>


1.自定义的MyApp.h类,和字典中做到一一对应 
#import <Foundation/Foundation.h>
/**
 *  copy :NSString
    strong :一般对象
    weak:UI控件
    assign :基本数据类型
 */
@interface MyApp : NSObject
@property (nonatomic,copy) NSString *icon;
@property(nonatomic,copy) NSString *name;
/**
 *  通过字典来初始化模型对象
 *
 *  @param dic  字典对象
 *
 *  @return 已经初始化完毕的模型对象
 */
/*
instancetype的作用,就是使那些非关联返回类型的方法返回所在类的类型!
好处能够确定对象的类型,能够帮助编译器更好的为我们定位代码书写问题
instanchetype和id的对比
1、相同点
都可以作为方法的返回类型

2、不同点
①instancetype可以返回和方法所在类相同类型的对象,id只能返回未知类型的对象;

②instancetype只能作为返回值,不能像id那样作为参数,比如下面的写法:
*/
-(instancetype)initWithDict:(NSDictionary *)dict;

+(instancetype) appWithDict:(NSDictionary *)dict;
+(NSArray *)apps;
@end

2.MyApp.m类实现
@implementation MyApp
-(instancetype)initWithDict:(NSDictionary *)dict{
//第一种方式
    if (self=[super init]) {
        self.name=dict[@"name"];
        self.icon=dict[@"icon"];
    }
//第二种方式  常采用第二种方式
self=[super init];
if(self){
[self setValuesForKeysWithDictionary:dict];
}
    return self;
}
+(instancetype) appWithDict:(NSDictionary *)dict{
    
    // 为何使用self,谁调用self方法 self就会指向谁!!
    return [[self alloc] initWithDict:dict];
    
}


//处理plist数据 并返回模型对象的数组

+(NSArray *) apps{

if(_apps==nil){

//加载plist文件

NSString *path=[[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil];

//加载数组

NSArray *dicArray=[NSArray arrayWithContentsOfFile:path];

//上面两句话等同于

NSArray *dicArray=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil]];

//将dicArray里面的所有字典转换成模型对象 放到新的数组里

NSMutableArray *mutableArray=[NSMutableArray array];

for(NSDictionary *dict in dicArray){}

 

[mutableArray addObject:[self appWithDict:dict ]];

}

return mutableArray;

}

@end


3.在其他的Viewcontroller.m文件调用的时候

第一步导入MyApp.h文件

@interface ViewController()

//第二步做个懒加载

@property(nonatomic,strong) NSArray *result;

@end

@implementation ViewController{


-(NSArray *)result{

if(_result==nil){

//类方法调用方式直接 类名+方法名

_result=[MyApp apps];

}


}

}

@end

2015-08-04 11:00:36 syg90178aw 阅读数 1007
//字符串 转换成字典 
+(NSMutableDictionary *)parseJsonData:(NSString *)result{
    
    NSError *error;
    
    NSData  *data = [result dataUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableDictionary  *dictionaryFromJson = [NSJSONSerialization JSONObjectWithData:data
                                                                              options:NSJSONReadingMutableLeaves
                                                                                error:&error];
    //   NSLog(@"%@-->%@",dictionaryFromJson,[error description]);
    return dictionaryFromJson;
}


2019-04-28 10:55:29 qq_43361450 阅读数 210

在这里插入图片描述

//字典套字典
        self.moneyLabel.text=[[json objectForKey:@"data"] objectForKey:@"acctBal"];
        self.availableelementLabel.text=[[json objectForKey:@"data"] objectForKey:@"avlBal"];
        self.accountlementLabel.text=[[json objectForKey:@"data"] objectForKey:@"acctBal"];
        self.freezelementLabel.text=[[json objectForKey:@"data"] objectForKey:@"frzBal"];
        self.yuanLabel.text=[[json objectForKey:@"data"] objectForKey:@"avlBal"];
2017-04-11 16:28:34 ziyuzhiye 阅读数 18415
NSDictionary *dic= @{@"name":@"zero",@"age":@(25)};
NSData *data= [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
2016-09-23 00:40:59 wuyulunbi12580 阅读数 577

客户端代码

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
        NSDictionary *dict = @{@"method":@"setAddress",@"username":@"lisi",@"address":@"beijing"};
        [manager POST:USERSERVLET_PATH parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *status = [responseObject objectForKey:@"status"];
            if([@"success" isEqualToString:status]){
                NSLog(@"设置Address成功");
                //更新UI
                _addressLabel.text = address;
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"设置Address失败");
        }];

服务器用的是java的servlet

        PrintWriter out = null;
        try {
            out = response.getWriter();
            String address = request.getParameter("address");
            //要想通过此方法取得参数值,客户端要这样设置(默认就是此配置,可以不写)
            //manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            String username = request.getParameter("username");
            UserDao dao = new UserDaoImpl();
            String status = dao.setAddress(username,address);
            Map<String,String> map = new HashMap<String,String>();
            map.put("status", status);
            JSONObject result = JSONObject.fromMap(map);
            out.print(result.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            out.flush();
            out.close();
        }   

iOS 字符串转字典

阅读数 573

字典转成字符串iOS

阅读数 188

[iOS]字符串转字典

阅读数 681

没有更多推荐了,返回首页