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

swift常用开发技术总结

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

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

数组(懒加载):

 lazy var shops:Array<Dictionary<String, String>> = {

         return [

             [

             "icon" : "danjianbao",

             "name" : "单肩包"

             ],

             [

             "icon" : "liantiaobao",

             "name" : "链条包"

             ],

             [

             "icon" : "qianbao",

             "name" : "钱包"

             ],

             [

             "icon" : "shoutibao",

             "name" : "手提包"

             ],

             [

             "icon" : "shuangjianbao",

             "name" : "双肩包"

             ],

             [

             "icon" : "xiekuabao",

             "name" : "斜挎包"

             ]

         ]

         }()

读取plist文件

     let path = NSBundle.mainBundle().pathForResource("shops", ofType: "plist")!

         let shopArray:AnyObject = NSArray(contentsOfFile: path)!

         let dict = shopArray as! NSDictionary

         let text = dict["name"] as! String

         label.text = text

         let imageName = dict["icon"] as! String

         let image = UIImage(named: imageName)

         imageView.image = image

将plist数据转模型;

   class Shop: NSObject {

         var name: String!

         var icon: String!

         init(dict: [String : String]) {

             super.init()

             //        self.name = dict["name"]

             //        self.icon = dict["icon"]

             setValuesForKeysWithDictionary(dict)

         }

     }

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

   lazy var shops:NSMutableArray = {

         let path:String = NSBundle.mainBundle().pathForResource("shops.plist", ofType: nil)!

         let tempArr:NSMutableArray = NSMutableArray(contentsOfFile: path)!

         let shopsArrM:NSMutableArray = NSMutableArray(capacity: tempArr.count)

         for dict in tempArr

         {

             let shop: Shop = Shop(dict: dict as! [String : String])

             shopsArrM.addObject(shop)

         }

         return shopsArrM

         }()

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

 +(NSString *)stringTOjson:(id)temps   //把字典和数组转换成json字符串

 {

 NSData* jsonData =[NSJSONSerialization dataWithJSONObject:temps

                                                       options:NSJSONWritingPrettyPrinted error:nil];

     NSString *strs=[[NSString alloc] initWithData:jsonData

                                          encoding:NSUTF8StringEncoding];

     return strs;

 }

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

 func toJSONString(dict:NSDictionary!)->NSString{

     var data = NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)

     var strJson=NSString(data: data, encoding: NSUTF8StringEncoding)

     return strJson

 }

常用语法分析

 关于数组和字典的总结:

         /******************************************************************************

         *                                                                            *

         *                              Inquiry macros                                *

         *                                                                            *

         *                            iCocos--Description                             *

         *                                                                            *

         ******************************************************************************/

         /*****数组****/

         var s1:Array<String> = ["]

         var s2:[String] = ["] //let

         var s3 = [String]()

         //操作

         s1.append(") // 追加

         s1 += ["] // 追加

         s1.insert(", atIndex: s1.count)  //插入

         s1.removeAtIndex()  //移除s1.removeLast()/s1.removeAtIndex(<#index: Int#>)/s1.removeRange(<#subRange: Range<Int>#>)

         s1[] = "  //修改

         //遍历

         for (index, value) in enumerate(s1) {   }  // 遍历

         /******************************************************************************

          *                                                                            *

          *                              Inquiry macros                                *

          *                                                                            *

          *                            iCocos--Description                             *

          *                                                                            *

          ******************************************************************************/

         /******字典******/

         var st1:Dictionary<Int, String> = [:"iCocos"]

         var st2 = [:"iCocos"]

         var st3 = Dictionary<Int, String>()

         //操作

         st1[] = "iCocos"   //增加

         st1.removeValueForKey() //移除  st1.removeAtIndex(<#index: DictionaryIndex<Key, Value>#>)

         st1[] = nil //清空

         st1[] = "iCocos"  //修改

         st1.updateValue()   //修改

         //遍历

         for s in st1.keys {}

         for ss1 in st1.values {}

         for (sss, ssss) in st1 {}
上一篇:秘钥登录服务器执行shell脚本


下一篇:Parencodings 分类: POJ 2015-06-28 22:00 7人阅读 评论(0) 收藏