Reputation: 19
I'm a newbie for swift, Just a basic question:
let test: UITableView?
let test = UITableView()
what is the difference between these two tableviews. is there any memory issue with one of this or any drawbacks?
Upvotes: 1
Views: 68
Reputation: 30481
A variable or constant followed by : TYPE
means that it creates a memory location suitable for storing a reference of that type. This is a declaration.
let test: UITableView?
Using = REFERENCE()
is an assignment, where you create a memory location to hold the reference to the object.
let test = UITableView()
This type is implicitly inferred, however you can still explicitly create the type by combining the two:
let test: UITableView = UITableView()
See The Basics in the Swift documentation.
Correct me if some of this information is incorrect or inaccurate
Upvotes: 1
Reputation: 125037
what is the difference between these two tableviews
let test: UITableView?
let test = UITableView()
The first line is a declaration, in which you're telling the compiler that test
is an optional reference to a table view.
The second line combines a declaration and an assignment. The UITableView()
part is the initializer that actually creates a table view object. That object is then assigned to test
. Note that in this case you haven't explicitly specified the type, so the compiler infers it from the type of the thing that you're assigning, so the type of test
here will be UITableView
instead of UITableView?
, which is to say the it won't be an optional. If you want, you can specify the type and assign a value all in one step, like this:
var test : UITableView? = UITableView()
I used var
here because the fact that test
is being declared as an optional means that its value might change later to nil.
Upvotes: 2
Reputation: 63403
The first isn't a table view. It's a reference to a table view, with no value (nil
). No table view was actually made.
This is a very basic/general question. You would have much faster/easier learning progress if you just read the Swift Programming Language Guide.
Upvotes: 3