IOS中將字典轉成模型對象,ios中將字典模型

來源:互聯網
上載者:User

IOS中將字典轉成模型對象,ios中將字典模型

作為IOS開發初級者今天學習了 如何將plist資料字典轉成 資料對象數組中 。有點像C#中解析xml資料 的過程。

apps.plist的xml資料是這樣的

 

<?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>


從處理plist中的資料 並返回模型對象的數組
/** *  從處理plist中的資料 並返回模型對象的數組 * *  @return  NSArray *apps; */-(NSArray *) apps{    if (_apps==nil) {        // 過去plist的全路徑        NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];        //載入數組        NSArray *dicArray=[NSArray arrayWithContentsOfFile:path];        //將dicArray裡面的所有字典轉成模型對象,放到新的數組中。        NSMutableArray *appArray=[NSMutableArray  array];        for (NSDictionary *dict in dicArray) {            //建立模型對象                                                /*            MyApp *app=[[MyApp alloc] initWithDict:dict];                         [NSString stringWithFormat:<#(NSString *), ...#>];            [[NSString alloc] initWithFormat:<#(NSString *), ...#>];                        [NSArray arrayWithContentsOfFile:<#(NSString *)#>]            [[NSArray alloc] initWithContentsOfFile:<#(NSString *)#>;             通過這裡 我們需要提取一個appWith                一個命名規範的問題             */                        MyApp *app=[MyApp appWithDict:dict];            //添加到對象到數組中            [appArray addObject:app];        }        //賦值        _apps=dicArray;            }    return _apps;}

 

 

自訂的MyApp類,和字典中做到一一對應 

#import <Foundation/Foundation.h>/** *  copy :NSString    strong :一般對象    weak:UI控制項    assign :基礎資料型別 (Elementary Data Type) */@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;@end

 

 

@implementation MyApp-(instancetype)initWithDict:(NSDictionary *)dict{    if (self=[super init]) {        self.name=dict[@"name"];        self.icon=dict[@"icon"];    }    return self;}+(instancetype) appWithDict:(NSDictionary *)dict{        // 為何使用self,誰調用self方法 self就會指向誰!!    return [[self alloc] initWithDict:dict];    }@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.