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
.