19

I have been given Objective C code and I need to add extra functionalities to it. I am very unfamiliar with Objective C so doing the most I possibly could on Swift would be very optimal for me.

This is my Swift file/class:

import Foundation
import UIKit

@objc class ImageBarSize: NSObject{

  static func changeContadorImageSize(img:UIImage, newSize:CGSize) -> UIImage{
    UIGraphicsBeginImageContextWithOptions(newSize, true, 0.0)
    let x:CGFloat = 0
    let y:CGFloat = 0
    img.draw(in: CGRect(x:x,y:y,width:newSize.width,height:newSize.height))
    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return newImage;
  }
}

And this is the code on my Objective C .m file:

 imgBarCounter = [UIImage imageNamed:@"bar-counter-pd.png"]; 

 self.image = [ImageBarSize changeContadorImageSize:imgBarCounter newSize:CGSizeMake(300, 300)];

I get the Error "No known class method for selector 'changeContadorImageSize:newSize:'".

I did the whole bridging process and I have

 #import <WHS_Live_2-Swift.h>

At the beginning of the file, and it all seems to be working fine. I've looked through what seemed like similar error threads here on SO, but to no avail.

Miguel Guerreiro
  • 356
  • 1
  • 2
  • 11

1 Answers1

12

Seeing this line, you are using Swift 3.

img.draw(in: CGRect(x:x,y:y,width:newSize.width,height:newSize.height))

In Swift 3, the first parameter is also treated as having argument label.

Establish consistent label behavior across all parameters including first labels (SE-0046)

You can check how they are exported to Objective-C by Command-clicking on #import <YourProjectName-Swift.h>. (You may need to wait till Xcode finishes Indexing.)

Tested in Xcode 8 beta 6, your class method becomes like this:

+ (UIImage * _Nonnull)changeContadorImageSizeWithImg:(UIImage * _Nonnull)img newSize:(CGSize)newSize;

So, you may need to call it like this:

self.image = [ImageBarSize changeContadorImageSizeWithImg:imgBarCounter newSize:CGSizeMake(300, 300)];

Or, you can change the Swift method as:

static func changeContadorImageSize(_ img:UIImage, newSize:CGSize) -> UIImage{

Then you can call it as in the original form:

self.image = [ImageBarSize changeContadorImageSize:imgBarCounter newSize:CGSizeMake(300, 300)];
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • Ahhh, I see! Thank you very much for that link and explanation, I understand it now. The Command+Click on the #import is also something extremely useful for me and I had no idea that it existed! Thank you again c: – Miguel Guerreiro Aug 30 '16 at 08:12
  • A year later, at least in Xcode 8.3.3, Command+Click on #import is not working, at least for me, for the . I can however, add the Class name to my code, and then Control or right click on the class name and then choose "Jump to Definition" and I'll get an option to jump to either the coded version or the derived/generated version. The later will often clear up some mysteries. – Tony Adams Jul 26 '17 at 17:23
  • 7
    Using Xcode 10.0. To make the whole thing working I had to prefix not only the swift class but also the static method signature with @objc. – Joss Nov 05 '18 at 14:20
  • Man, I will second Mr. Guerreiro in saying your Command+Click on the import was very helpful. Thank you – ptdecker Oct 26 '22 at 19:24