0

I found a tutorial on how to do something that's written in Swift. I'm at the tail end of an Objective-C project and I've tried importing the classes into Objective-C, but that's been a bug-riddled, time monopolizing disaster, so I'm translating from Swift to Objective-C instead.

I got the UIView.h/m to the point where there are no warnings/errors, but adding the view on the UIViewController is a different story. I've posted the initialization code I'm trying to "translate" below. I welcome input re: what I'm fouling up.

Code from Swift class for initializing UIView:

var parentFrame :CGRect = CGRectZero

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = Colors.clear
}

Code from my "translated" Objective-C class for initializing the UIView

myUIView.h

// At MartinR's suggestion, I removed the pointer here
@property (nonatomic) CGRect parentFrame;

myUIView.m

// No compiler errors -- the errors occur on the UIViewController
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:(self.parentFrame)];
    self.parentFrame = CGRectZero;
    if (self) {
        [super setFrame:self.parentFrame];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

The problem occurs when I try to add the UIView to the UIViewController. I can't figure out why I'm getting a warning below:

Swift UIViewController code to add the UIView

  func addHolderView() {
    let boxSize: CGFloat = 75.0
    holderView.frame = CGRect(x: view.bounds.width / 2 - boxSize / 2,
                              y: view.bounds.height / 2 - boxSize / 2,
                              width: boxSize,
                              height: boxSize)
    holderView.parentFrame = view.frame
    holderView.delegate = self
    view.addSubview(holderView)
    holderView.addOval()
  }

My "translated" Objective-C ViewController riddled with implementation errors

myUIViewController.h

#import "HolderView.h"

// blah blah

@property (nonatomic, strong) HolderView *holderView;

myUIViewController.m

- (void)addHolderView {
    CGFloat boxSize = 75.0;
// WARNING: The following line says "expression result unused"
    [self.holderView initWithFrame:CGRectMake(_view.bounds.size.width / 2 - boxSize / 2,
                                              _view.bounds.size.height / 2 - boxSize / 2,
                                              boxSize,
                                              boxSize)];
    // Warning here went away by removing the pointer on CGRect parentFrame
    self.holderView.parentFrame = view.frame;
    self.holderView.delegate = self; // I have the delegate stuff squared away
    [self.view addSubview:self.holderView];
}

Thank you for reading.

Updated to reflect input from MartinR in comments.

I updated the code above to remove a pointer on CGRect parentFrame.

Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180
  • 1
    Once you have realized that `parentFrame` should **not be a pointer** you can clean up your code considerably :) – Martin R Jul 24 '15 at 11:16
  • Thank you @MartinR! I'm venturing outside of my Interface Builder "comfort zone" here. I've spent more time than I care to admit on this. If you've got more to add, I'm all ears. – Adrian Jul 24 '15 at 11:22
  • 1
    `@property (nonatomic) CGRect parentFrame;` without the `*`. (Do you know what a pointer is?). Then clean up your `initWithFrame` method. – Martin R Jul 24 '15 at 11:24

1 Answers1

1

The Swift version of addHolderView is using a holderView that already exists and updating it. Yours is either trying to re-init something that exists or else init something that hasn't been allocated. It's hard to know which, but either one is a bad idea.

To mimic the original code, just assign a new frame size without an init. To be sure of what's going on, check that self.holderView points to a valid object using the debugger or a NSLog statement.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • Thank you! I'll tinker with it this morning. Once I get this initialization out of the way, everything else I'm "translating" is straightforward. It's frustrating to be stuck on the first step, but knowing everything afterwards ;) – Adrian Jul 24 '15 at 12:27
  • Finally got it. Had 2 problems--1) Wasn't instantiating the UIViewController and 2) I was trying to do too much in the initializer--I set the frame dimensions locally on the UIViewController rather than the class. – Adrian Jul 27 '15 at 16:44