Swift3.0 与 Android对应的语法或方法(持续更新)
习惯了android开发的我转swift,当然是找相同点,qq 549890025 可以共同研究
这里有篇概括性文章:
http://lib.csdn.net/article/swift/44966
android: toast提示
toast.makeText(this,"hahaha",1).show();
swift: toast提示
亲测可用,代码是copy的,论英文的重要性。要引入第三方frameword Toast-Swift,使用cocopods的话 pod 'Toast-Swift','~> 2.0.0' 然后在终端terminal cd到项目根目录(里面包涵profile文件)输入
pod install 不填版本默认最新,下载下来之后,command+B build项目一下,然后就可以引入 import Toast_Swift。
// basic usage self.view.makeToast("This is a piece of toast") // toast with a specific duration and position self.view.makeToast("This is a piece of toast", duration: 3.0, position: .Top) // toast with all possible options self.view.makeToast("This is a piece of toast", duration: 2.0, position: CGPoint(x: 110.0, y: 110.0), title: "Toast Title", image: UIImage(named: "toast.png"), style:nil) { (didTap: Bool) -> Void in if didTap { print("completion from tap") } else { print("completion without tap") } } // display toast with an activity spinner self.view.makeToastActivity(.Center) // display any view as toast self.view.showToast(myView) // create a new style let style = ToastStyle() // this is just one of many style options style.messageColor = UIColor.blueColor() // present the toast with the new style self.view.makeToast("This is a piece of toast", duration: 3.0, position: .Bottom, style: style) // or perhaps you want to use this style for all toasts going forward? // just set the shared style and there's no need to provide the style again ToastManager.shared.style = style // toggle "tap to dismiss" functionality ToastManager.shared.tapToDismissEnabled = true // toggle queueing behavior ToastManager.shared.queueEnabled = true
swift: UIAlertController提示
let alertController = UIAlertController(title: "通知", message: "确定还是取消", preferredStyle: UIAlertControllerStyle.Alert) // 这里因为控件都不存在改变的可能,所以一律使用let类型 let alertView1 = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in print("确定按钮点击事件") } let alertView2 = UIAlertAction(title: "取消", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in print("取消按钮点击事件") } let alertView3 = UIAlertAction(title: "下次吧", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in print("下次吧按钮点击事件") } alertController.addAction(alertView1) alertController.addAction(alertView2) alertController.addAction(alertView3) // 当添加的UIAlertAction超过两个的时候,会自动变成纵向分布 self.presentViewController(alertController, animated: true, completion: nil)
android:存储字段
public int getInt(String key) { SharedPreferences sharedata = context.getSharedPreferences("data", 0); return sharedata.getInt(key, 1); }
swift:存储字段
UserDefaults.standard.set("content", forkey:"keyword")
UserDefaults.standard.object(forkey:"keyword")
浙公网安备 33010602011771号