Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is type context?

Tags:

ios

swift

swift2

I'm currently stuck to understand which variables are considered to be local, and which are global.

In docs we can read that:

Global variables are variables that are defined outside of any function, method, closure, or type context.

I'm guessing that: variables defined in type context == type/instance variables defined inside class/struct?

For example:

var foo = "foo" // global variable

class Foobar {
  static var foo = "foo" // local variable -> declared inside Foobar type context
  var bar = "bar" // local variable -> declared inside Foobar type context

  func foobar() {
    var foo = "" // local variable -> declared inside method
  }
}
like image 648
matDobek Avatar asked Oct 15 '25 10:10

matDobek


1 Answers

You are absolutely correct, variables defined inside class context become either instance variables or type variables.cVariables defined inside the context of a function, method, or a closure become local variables.

A declaration of a global variable must be at the top level, outside of classes, functions, and so on.

like image 167
Sergey Kalinichenko Avatar answered Oct 17 '25 00:10

Sergey Kalinichenko