1

I'm reading a code example on Github and I see something I would to understand how it works.

the code is something like:

- (void)viewDidLoad
{
    [super viewDidLoad];

    {
        self.formatter = [[NSDateFormatter alloc] init];
        [self.formatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"yyyyMMMd" options:0 locale:[NSLocale currentLocale]]];
    }

}

what does it mean? is it something related to the async execution of code portion? does anybody enlighten me?

Hugo Ferreira
  • 1,584
  • 16
  • 17
tylyo
  • 572
  • 5
  • 16
  • 1
    One can insert `{...}` surrounding any set of complete statements. It means that any automatic variables declared in that region cannot "escape" and be referenced outside. Otherwise, it has no effect. In the above I suspect there was once an `if` statement or some such "guarding" the two enclosed statements and the braces were simply left there when the `if` statement was removed. – Hot Licks Dec 27 '13 at 16:38
  • Yes I had misread it. You can also use those if you want to put variables inside switch statements without having it flip out on you. – 0xFADE Dec 27 '13 at 16:44
  • 2
    see this SO question [link](http://stackoverflow.com/questions/9704083/unnecessary-curly-braces-in-c) – Bamsworld Dec 27 '13 at 16:51

3 Answers3

4

You said brackets. Are you talking about curly braces instead? "{" and "}".

Curly braces define a local scope. It can be used simply for code readability, or you can also use it to limit the scope of local variables:

- (void)viewDidLoad
{
  [super viewDidLoad];
  {
    //local variables inside these braces are only defined inside this set of braces
    NSString *scratchString;
    int count = 1;
    scratchString = @"foo";
  }

  {
    //The string scratchString below is a different local variable than
    //The one defined above.
    NSString *scratchString;
    int count = 5;
    scratchString = @"bar";
  }
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • 2
    Could you also cite us a short scenario in which using braces would be highly preferred? Thanks. – Unheilig Dec 27 '13 at 16:46
  • @Unheiling he provided an example use in which one may use the braces to limit scope of a variable. Whether or not this method is highly preferred is highly subjective. – Jeremy Dec 27 '13 at 18:02
  • thanks Duncan, the more reasonable answer is yours in this case. I think it was a way to duplicate code without change variables name using a local scope. I thought that it could be something different with objective c. – tylyo Dec 28 '13 at 00:08
  • Debugging the execution code I notice that the code inside the braces is executed after the end of the method independently. So I think that it could be useful in some context. – tylyo Dec 29 '13 at 10:40
  • @tylyo, it does not make sense that the code in braces (`{` and `}`) in your example would execute after the line(s) below the braces. That might be true for a closure called using `dispatch_async` or `dispatch_after`, but curly braces will not otherwise change the order of execution. As @HotLicks suggests in his comment, the braces in this case are probably a leftover where there used to be an `if` statement (or similar) above the braces, but it was removed and the programmer did not remove the braces. – Duncan C Jan 09 '17 at 14:53
0

The [] is how objective C communicates through messages. It is just a little bit slower then if it was a function.

0xFADE
  • 832
  • 5
  • 7
0

Those brackets are Objective C method call syntax.

The basic syntax of an instance method call is

[target_object message_name];

if the message takes a parameter:

[target_object message_name: parameter];

I suggest reading a book on the Objective C language.

Duncan C
  • 128,072
  • 22
  • 173
  • 272