0

I saw some people create a Object like this in Objective-C

_user = ({
    User *user = [[User alloc]init];
    user.name = @"Joe";
    user.age = 18;
    user;
});

Is it builder pattern or ????

Shinren Pan
  • 49
  • 1
  • 4

2 Answers2

2

This is a statement expression, a GCC extension to standard C. Also usable with LLVM, the default compile of Xcode.

It is useful to define a place to setup an object. and than write it fully configurated to a variable or property.

I used it a lot in the past, but recently I started to use implicit and immediate executed blocks for that, as they provide a better scoping.

_user = ^{
    User *user = [[User alloc]init];
    user.name = @"Joe";
    user.age = 18;
    return user;
}();
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0

No, it is not a builder. It is just and object with properties.

In builder pattern you should incapsulate object realisation. This mean you should separate the construction of an object from its representation. This allow you build different objects by the same construction process.

Pavel Gatilov
  • 2,570
  • 2
  • 18
  • 31