Swift代码片段整理

2014年6月4日 分类: swift (262 个脚步)

苹果在2014年的WWDC大会上公布了新语言Swift,关于Swift的争论也不断的出现。

有人说Swift还不能完全将Objective-C替代,又有人说苹果官方都把底层都重写了,所以替换掉Objective-C是早日的时候。

不管怎么样,一门新语言出现总是有好处的,多学一门语言就多一门技术。活到老,学到老。

下面就是我所知道的从Objective-C转到Swift后的各种代码片段,做个备忘~

附上官方的demo代码:ListerAProductivityAppBuiltinSwift.zip

UIAlert


func notifyUserOfAccountChange() {
    let title = NSLocalizedString("iCloud Sign Out", comment: "")
    let message = NSLocalizedString("You have signed out of the iCloud account previously used to store documents. Sign back in to access those documents.", comment: "")
    let okActionTitle = NSLocalizedString("OK", comment: "")
        
    let signedOutController = UIAlertController(title: title,  : message, preferredStyle: .Alert)
        
    let action = UIAlertAction(title: okActionTitle, style: .Cancel, handler: nil)
    signedOutController.addAction(action)
        
    self.presentViewController(signedOutController, animated: true, completion: nil)
}

类型转换

//String to Double
var t:String? = "10.213"
NSString.stringWithString(t).doubleValue

//String to Int
var StringInt:String? = "12"
StringInt.toInt()

控件

// Label
var label = UILabel(frame: self.view.bounds)
label.backgroundColor = UIColor.clearColor()
label.textAlignment = NSTextAlignment.Center
label.font = UIFont.systemFontOfSize(36)
label.text = "Hello, Swift"
self.view.addSubview(label)
// Button
var button = UIButton.buttonWithType(UIButtonType.System) as? UIButton
button!.frame = CGRectMake(110.0, 120.0, 100.0, 50.0)
button!.backgroundColor = UIColor.grayColor()
button?.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
button!.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
button?.setTitle("Touch Me", forState: UIControlState.Normal)
button?.setTitle("Touch Me", forState: UIControlState.Highlighted)
button?.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
button!.tag = 100
self.view.addSubview(button)
//WebView
var webView = UIWebView(frame:self.view.bounds)
var url = NSURL(string: "http://caipiao.taobao.com")
var request = NSURLRequest(URL: url)
webView.loadRequest(request)
self.view.addSubview(webView)
//Switch
var switchControl = UISwitch(frame:CGRectMake(130.0, 120.0, 100.0, 30.0))
switchControl.on = true
self.view.addSubview(switchControl)
//TextField
var textField = UITextField(frame:CGRectMake(60.0, 120.0, 200.0, 30.0))
textField.backgroundColor = UIColor.lightGrayColor()
textField.placeholder = "input text"
self.view.addSubview(textField)
var image     = UIImage(named: "swift-hero.png")
var imageView = UIImageView(frame: CGRectMake((CGRectGetWidth(self.view.bounds) - image.size.width) / 2.0, 120.0, image.size.width, image.size.height))
imageView.image = image
self.view.addSubview(imageView)

键盘隐藏

override func viewDidLoad() {
        super.viewDidLoad()
        textfield.addTarget(self, action: "nextOnKeyboard", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    }
    func nextOnKeyboard() {
        textfield.becomeFirstResponder()
    }

Swift代码片段整理 【声明】本文 Swift代码片段整理 为柠之漠然原创文章,转载请注明出自 枫之落叶
并保留本文有效链接:https://blog.shiniv.com/2014/06/objective-c-to-swift-code/ , 转载请保留本声明!

标签: , ,
2 条评论
你必须要启用 Javascript