What does this mean? Variable declared followed by a block without assignment

前端 未结 3 1961
梦毁少年i
梦毁少年i 2021-01-25 12:54

Noob Swift question--I can\'t figure out what this means in Swift:

public var currentTime: NSTimeInterval? {
    return self.audioPlayer?.currentTime
}
         


        
3条回答
  •  时光取名叫无心
    2021-01-25 13:49

    I wrote a little playground for clarification:

    //: Computed properties
    
    import UIKit
    
    var variable_int = 1
    
    var computed_int: Int {
    get { return 1 }
    set { newValue }
    }
    
    var get_only_int: Int {
      return 1
    }
    
    var get_only_int_2: Int {
    get { return 1 }
    }
    
    variable_int = 2 // legal
    computed_int = 2 // legal
    
    // computed_read_only_int = 2   // 'computed_read_only_int' is a get-only property
    // computed_read_only_int_2 = 2 // 'computed_read_only_int_2' is a get-only property
    
    
    // This is another way to specify a variable you could find useful, I found it somewhere on natashatherobot.com
    var variable_int_2: Int = {
      return 1
    }()
    
    variable_int_2 = 2 // legal
    

    works in Xcode 8.1

提交回复
热议问题