码迷,mamicode.com
首页 > 移动开发 > 详细

ios开发——实用技术总结Swift篇&swift常用开发技术总结

时间:2015-08-12 01:06:57      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:

swift常用开发技术总结

 

懒加载:属性,数组(字典),控件。。。

技术分享

 

数组(懒加载)

    

 1 lazy var shops:Array<Dictionary<String, String>> = {
 2 
 3         return [
 4 
 5             [
 6 
 7             "icon" : "danjianbao",
 8 
 9             "name" : "单肩包"
10 
11             ],
12 
13             [
14 
15             "icon" : "liantiaobao",
16 
17             "name" : "链条包"
18 
19             ],
20 
21             [
22 
23             "icon" : "qianbao",
24 
25             "name" : "钱包"
26 
27             ],
28 
29             [
30 
31             "icon" : "shoutibao",
32 
33             "name" : "手提包"
34 
35             ],
36 
37             [
38 
39             "icon" : "shuangjianbao",
40 
41             "name" : "双肩包"
42 
43             ],
44 
45             [
46 
47             "icon" : "xiekuabao",
48 
49             "name" : "斜挎包"
50 
51             ]
52 
53         ]
54 
55  
56 
57         }()

 

读取plist文件

    

 1     let path = NSBundle.mainBundle().pathForResource("shops", ofType: "plist")!
 2 
 3         
 4 
 5         let shopArray:AnyObject = NSArray(contentsOfFile: path)!
 6 
 7         
 8 
 9         let dict = shopArray as! NSDictionary
10 
11         
12 
13         let text = dict["name"] as! String
14 
15         label.text = text
16 
17         
18 
19         let imageName = dict["icon"] as! String
20 
21         let image = UIImage(named: imageName)
22 
23         imageView.image = image

 

 

将plist数据转模型;

 

 1   class Shop: NSObject {
 2 
 3         var name: String!
 4 
 5         var icon: String!
 6 
 7         
 8 
 9         init(dict: [String : String]) {
10 
11             super.init()
12 
13             //        self.name = dict["name"]
14 
15             //        self.icon = dict["icon"]
16 
17             setValuesForKeysWithDictionary(dict)
18 
19         }
20 
21     }

 

    

    加载Plist转为模型放到数组中

  

 1   lazy var shops:NSMutableArray = {
 2 
 3         let path:String = NSBundle.mainBundle().pathForResource("shops.plist", ofType: nil)!
 4 
 5         let tempArr:NSMutableArray = NSMutableArray(contentsOfFile: path)!
 6 
 7         let shopsArrM:NSMutableArray = NSMutableArray(capacity: tempArr.count)
 8 
 9         for dict in tempArr
10 
11         {
12 
13             let shop: Shop = Shop(dict: dict as! [String : String])
14 
15             shopsArrM.addObject(shop)
16 
17         }
18 
19         return shopsArrM
20 
21         }()
22 
23     

 

字典转模型:先来看看Objective—C是怎么写的!

 1 +(NSString *)stringTOjson:(id)temps   //把字典和数组转换成json字符串
 2 
 3 {
 4 
 5 NSData* jsonData =[NSJSONSerialization dataWithJSONObject:temps
 6 
 7                                                       options:NSJSONWritingPrettyPrinted error:nil];
 8 
 9     NSString *strs=[[NSString alloc] initWithData:jsonData
10 
11                                          encoding:NSUTF8StringEncoding];
12 
13     return strs;
14 
15 }

 

 

利用NSJSONSerialization序列化成NSData,再通过NSString 转成JSON字符串

 1 func toJSONString(dict:NSDictionary!)->NSString{
 2 
 3  
 4 
 5     var data = NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
 6 
 7     var strJson=NSString(data: data, encoding: NSUTF8StringEncoding)
 8 
 9     return strJson
10 
11  
12 
13 }

 常用语法分析

 

 1 关于数组和字典的总结:
 2 
 3         /******************************************************************************
 4 
 5         *                                                                            *
 6 
 7         *                              Inquiry macros                                *
 8 
 9         *                                                                            *
10 
11         *                            iCocos--Description                             *
12 
13         *                                                                            *
14 
15         ******************************************************************************/
16 
17         /*****数组****/
18 
19         var s1:Array<String> = ["1", "2", "3"]
20 
21         var s2:[String] = ["1", "2", "3"] //let
22 
23         var s3 = [String]()
24 
25         //操作
26 
27         s1.append("4") // 追加
28 
29         s1 += ["5"] // 追加
30 
31         s1.insert("6", atIndex: s1.count)  //插入
32 
33         s1.removeAtIndex(0)  //移除s1.removeLast()/s1.removeAtIndex(<#index: Int#>)/s1.removeRange(<#subRange: Range<Int>#>)
34 
35         s1[1] = "10"  //修改
36 
37         //遍历
38 
39         for (index, value) in enumerate(s1) {   }  // 遍历
40 
41         
42 
43         
44 
45         
46 
47         /******************************************************************************
48 
49          *                                                                            *
50 
51          *                              Inquiry macros                                *
52 
53          *                                                                            *
54 
55          *                            iCocos--Description                             *
56 
57          *                                                                            *
58 
59          ******************************************************************************/
60 
61         /******字典******/
62 
63         var st1:Dictionary<Int, String> = [102:"iCocos"]
64 
65         var st2 = [102:"iCocos"]
66 
67         var st3 = Dictionary<Int, String>()
68 
69         
70 
71         //操作
72 
73         st1[105] = "iCocos"   //增加
74 
75         st1.removeValueForKey(102) //移除  st1.removeAtIndex(<#index: DictionaryIndex<Key, Value>#>)
76 
77         st1[102] = nil //清空
78 
79         st1[102] = "iCocos"  //修改
80 
81         st1.updateValue("iCocos", forKey: 102)   //修改
82 
83         
84 
85         //遍历
86 
87         for s in st1.keys {}
88 
89         for ss1 in st1.values {}
90 
91         for (sss, ssss) in st1 {}
92 
93         

 

ios开发——实用技术总结Swift篇&swift常用开发技术总结

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4722802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!