0

I am a newbie trying collectionview in Obj C

override init(frame:CGRect){
  super.init(frame:frame)
      let thumbnailImageView: UIImageView = {
   let imageView = UIImageView()
   imageView.backGroundColor = UIColor.blueColor()
   return imageView;
}

addSubView(thumbnailImageView)
thumbnailImageView.frame = CGRectMake(0,0,100,100)
}

i am trying to achieve above swift code in Obj C . I have tried below code but sub view is not showing.

#import "VideoCell.h"

@implementation VideoCell

- (instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
UIImageView * thumbnailImageView = [[UIImageView alloc] init];    
thumbnailImageView.backgroundColor = [UIColor greenColor];
thumbnailImageView.frame = CGRectMake(0, 0, 100, 100);

[self addSubview:thumbnailImageView];

    }
    return self;
} 
halfer
  • 19,824
  • 17
  • 99
  • 186
ios
  • 165
  • 1
  • 11
  • It's the same: https://stackoverflow.com/questions/45941619/assigning-a-variable-value-from-an-objective-c-block Why do you keep to want to init a UIView with a block (it's almost never done in Objective-C. It can be, but in your case it's not useful). – Larme Aug 29 '17 at 15:55
  • Please note that calling `thumbnailImageView()` 2 times creates 2 different UIImageView activities, so I'd suggest moving setting frame property into initialization block – Roman Aug 29 '17 at 15:56
  • 1
    Just write `if (self) {UIImageView * thumbnailImageView = [[UIImageView alloc] init]; thumbnailImageView.backgroundColor = [UIColor greenColor];thumbnailImageView.frame = CGRectMake(0, 0, 100, 100);[self addSubview:thumbnailImageView];} return self;` – Larme Aug 29 '17 at 15:56
  • @Larme Ignore last comment . i just saw your comment – ios Aug 29 '17 at 15:58

1 Answers1

1

That's what any Objective-C developer would do:

- (instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView * thumbnailImageView = [[UIImageView alloc] init];    
        thumbnailImageView.backgroundColor = [UIColor greenColor];
        thumbnailImageView.frame = CGRectMake(0, 0, 100, 100);
        [self addSubview:thumbnailImageView];
    }
    return self;
} 

Using a closure (or a Block in Objective-C) in your example is overkilled.

You can do it, but most of developers may be intrigued of that code. Here is what you can do :

- (instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *imageView = ({
            UIImageView *imgV = [[UIImageView alloc] init];
            imgV.backgroundColor = [UIColor greenColor];
            imgV;
        });
        [self.view addSubview:imageView];
        imageView.frame = CGRectMake(0, 0, 100, 100);
    }
    return self;
} 

This is taken from an NSHipster Article. It can be found there and is called "GCC Code Block Evaluation C Extension" or "statement expressions". There was a question on SO about it, but it was closed because of primarily opinion-based. As I stated, it seems quite strange. That's clearly not the first idea of code that 99% (ok, it's a random statistic guess) iOS developer would have.

Site Note:
Do not search replicating exactly an Swift code into an Objective-C or reverse.
While all CocoaTouch calls with API should have the same logic (there you can "translate it without much thinking), each language has it own logic, "tools" and ways to do it. In your example, no block in Objective-C.

Larme
  • 24,190
  • 6
  • 51
  • 81