0

Can someone tell me what is going to be equivalence of this swift code in objective c

let someView = SomeView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height:self.view.bounds.size.height ))
self.view.addSubview(SomeView)

Where SomeView is a UIView.

I went through Apple docs: https://developer.apple.com/documentation/coregraphics/cgrect

and

this question on stack overflow: How do I create a CGRect from a CGPoint and CGSize?

but couldn't able to comprehend how can I do the same in Obj-c

Alwaysblue
  • 9,948
  • 38
  • 121
  • 210

1 Answers1

1

As mentioned in comments, the CGRectMake function is the preferred way. Given a point that is CGPoint and a size that is CGSize:

CGRect r = CGRectMake(point.x, point.y, size.width, size.height);

However CGRect is just a C struct that is defined to be this:

struct CGRect {
    CGPoint origin;
    CGSize size;
};

so you can also initialize it just like you would any struct in standard C.

CGRect r = { point, size };

Also since C is a lot less picky about uninitialized variables than Swift you can also do this, though it is less preferred even in C:

CGRect r;
r.origin = point;
r.size = size;

So to translate this Swift code,

let someView = SomeView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height:self.view.bounds.size.height ))
self.view.addSubview(SomeView)

into Objective-C, first we have to assume SomeView is a type accessible in Objective-C (for example, NSView or UIView, but not SwiftUI's View). You also have to keep in mind that Objective-C separates allocation and initialization, creating something of an idiom that looks like [[ClassName alloc] init:...];.

Additionally you have to take into account something that Swift underwent early on called the "Grand Renaming". A lot of methods and initializers from AppKit/UIKit were renamed in Swift to be more "Swifty", but they retain their original names in Objective-C. For that reason when you look things up in Apple docs in Swift to find how to call it in Objective-C, it's useful to go to the method/initializer detail in Swift, because you're familiar with it, and then switch to Obj-C to see it's Obj-C name.

If you do that with NSView's init(frame:) you find that in Obj-C it's initWithFrame:.

SomeView* someView = [[SomeView alloc] initWithFrame: { {0, 0}, bounds.size }];
[self addSubview: someView];

or

SomeView* someView = [[SomeView alloc] initWithFrame: CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
[self addSubview: someView];
Chip Jarred
  • 2,600
  • 7
  • 12
  • In swift, they have a UI view to which they are passing this a parameter or something like that? `SomeView(frame: CGRect(x`. How would this work in obj-C. Also, thanks for answering :) – Alwaysblue Jul 04 '21 at 22:55
  • 1
    I'll update my answer to include that view creation example. – Chip Jarred Jul 04 '21 at 22:57
  • Updated, also corrected the name of the function to make the rect. It's `CGRectMake`, not `CGMakeRect` – Chip Jarred Jul 04 '21 at 23:11