美文网首页
Swift 3.0 新特性

Swift 3.0 新特性

作者: Jniying | 来源:发表于2020-04-29 11:20 被阅读0次

    1.所有函数参数都有标签

    names.indexOf("Taylor")
    names.index(of: "Taylor")
    
    "Taylor".writeToFile("filename", atomically: true, encoding: NSUTF8StringEncoding)
    "Taylor".write(toFile: "somefile", atomically: true, encoding: String.Encoding.utf8)
    
    SKAction.rotateByAngle(CGFloat(M_PI_2), duration: 10)
    SKAction.rotate(byAngle: CGFloat(M_PI_2), duration: 10)
    
    UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
    UIFont.preferredFont(forTextStyle: UIFontTextStyle.subheadline)
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int
    override func numberOfSections(in tableView: UITableView) -> Int
    
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
    func viewForZooming(in scrollView: UIScrollView) -> UIView?
    
    NSTimer.scheduledTimerWithTimeInterval(0.35, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true)
    Timer.scheduledTimer(timeInterval: 0.35, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true)
    

    2.省略api不必要的说明

    befor

    let blue = UIColor.blueColor()
    let min = numbers.minElement()
    attributedString.appendAttributedString(anotherString)
    names.insert("Jane", atIndex: 0)
    UIDevice.currentDevice()
    

    now

    let blue = UIColor.blue
    let min = numbers.min()
    attributedString.append(anotherString)
    names.insert("Jane", at: 0)
    UIDevice.current
    

    例如string api 更改

    "  Hello  ".stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())
    "  Hello  ".trimmingCharacters(in: .whitespacesAndNewlines)
    
    "Taylor".containsString("ayl")
    "Taylor".contains("ayl")
    
    "1,2,3,4,5".componentsSeparatedByString(",")
    "1,2,3,4,5".components(separatedBy: ",")
    
    myPath.stringByAppendingPathComponent("file.txt")
    myPath.appendingPathComponent("file.txt")
    
    "Hello, world".stringByReplacingOccurrencesOfString("Hello", withString: "Goodbye")
    "Hello, world".replacingOccurrences(of: "Hello", with: "Goodbye")
    
    "Hello, world".substringFromIndex(7)
    "Hello, world".substring(from: 7)
    
    "Hello, world".capitalizedString
    "Hello, world".capitalized
    

    3.枚举类型更改

    UIInterfaceOrientationMask.Portrait // old
    UIInterfaceOrientationMask.portrait // new
    
    NSTextAlignment.Left // old
    NSTextAlignment.left // new
    
    SKBlendMode.Replace // old
    SKBlendMode.replace // new
    

    4. C 函数在swift 上的写法

    old

    let ctx = UIGraphicsGetCurrentContext()
    
    let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512)
    CGContextSetFillColorWithColor(ctx, UIColor.redColor().CGColor)
    CGContextSetStrokeColorWithColor(ctx, UIColor.blackColor().CGColor)
    CGContextSetLineWidth(ctx, 10)
    CGContextAddRect(ctx, rectangle)
    CGContextDrawPath(ctx, .FillStroke)
    
    UIGraphicsEndImageContext()
    

    new

    if let ctx = UIGraphicsGetCurrentContext() {
        let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512)
        ctx.setFillColor(UIColor.red.cgColor)
        ctx.setStrokeColor(UIColor.black.cgColor)
        ctx.setLineWidth(10)
        ctx.addRect(rectangle)
        ctx.drawPath(using: .fillStroke)
    
        UIGraphicsEndImageContext()
    }
    
    CGAffineTransformIdentity
    CGAffineTransform.identity
    
    CGAffineTransformMakeScale(2, 2)
    CGAffineTransform(scaleX: 2, y: 2)
    
    CGAffineTransformMakeTranslation(128, 128)
    CGAffineTransform(translationX: 128, y: 128)
    
    CGAffineTransformMakeRotation(CGFloat(M_PI))
    CGAffineTransform(rotationAngle: CGFloat(M_PI))
    

    5.动词和名词的更改

    myArray.enumerate()
    myArray.enumerated()
    
    myArray.reverse()
    myArray.reversed()
    
    PS参考链接

    参考Swift版本差异

    相关文章

      网友评论

          本文标题:Swift 3.0 新特性

          本文链接:https://www.haomeiwen.com/subject/gzkgwhtx.html