项目开发笔记-2015.12.20-swift

前端之家收集整理的这篇文章主要介绍了项目开发笔记-2015.12.20-swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、UITextField--设置为密码

2、获取UITextField的文本

UITextField.text String类型

        //文字超出文本框宽度,文字缩小
        login_username.adjustsFontSizeToFitWidth = true;
        //最小字体大小
        login_username.minimumFontSize = 14;

3、判断String是否为空

String.isEmpty

4、对话框UIAlertController(UIAlertView可以舍去不用了)

    //提示框
    func login_alert(message : String){
        let Alert = UIAlertController(title: "提示",message: message,preferredStyle: UIAlertControllerStyle.Alert);
        let ok = UIAlertAction(title: "确认",style: UIAlertActionStyle.Default,handler: nil);
        Alert.addAction(ok);
        self.presentViewController(Alert,animated: true,completion: nil);
    }
5、网络访问

class NetWork{
    static func request(method:String,url:String,callback:(data:NSData!,resopnse:NSURLResponse!,error:NSError!)->Void){
        let session = NSURLSession.sharedSession();
        let request = NSMutableURLRequest(URL: NSURL(string: url)!);
        request.HTTPMethod = method;
        let task = session.dataTaskWithRequest(request,completionHandler:
            {(data,response,error) -> Void in
                callback(data: data,resopnse: response,error: error);
            }
        )
        task.resume();
    }
    
    
}

6、数据存储和取

class CommonMethod{
    
    func saveTemporayData(TemporayDataValue:String,TemporayDataKey:String){
        //利用NSUserDefaults
        let temporaydata = NSUserDefaults.standardUserDefaults();
        //存储数据
        temporaydata.setObject(TemporayDataValue,forKey: TemporayDataKey);
        //同步数据
        temporaydata.synchronize();
    }
    
    func readTemporayData(TemporayDataKey:String) ->String{
        let tempporaydata = NSUserDefaults.standardUserDefaults();
        let data = tempporaydata.objectForKey(TemporayDataKey) as! String;
        return data;
    }
    
}

7.json解析

        NetWork.request("GET",url:url.get_network("username="+username_string+"&password="+password_string),callback: { (data,resopnse,error) -> Void in
            do{
                let json_b : AnyObject! = try NSJSONSerialization.JSONObjectWithData(data!,options: NSJSONReadingOptions.AllowFragments) as! NSDictionary;
                if ((json_b.objectForKey("result") as? Int) == 0){
                    self.commonMethod.saveTemporayData((json_b.objectForKey("token") as? String)!,TemporayDataKey: "token");
                }else{
                    self.login_alert((json_b.objectForKey("message") as? String)!);
                }
            }catch{
                print("网络异常!");
            }
        });
但是我这里的
self.login_alert((json_b.objectForKey("message") as? String)!);
会报异常,由于第一次自己用swift做项目,暂时不晓得怎么改,你们谁晓得怎么修改的,求给个解答

猜你在找的Swift相关文章