2

I got a swift category for UIImage, but I can't figure out how to make it work.

This is the swift category i'm trying to use:

import UIKit

 extension UIImage {
    func imageWithColor(color1: UIColor) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
    color1.setFill()

    let context = UIGraphicsGetCurrentContext()! as CGContextRef
    CGContextTranslateCTM(context, 0, self.size.height)
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, CGBlendMode.Normal)

    let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
    CGContextClipToMask(context, rect, self.CGImage)
    CGContextFillRect(context, rect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
    UIGraphicsEndImageContext()

    return newImage
    }
}

In my objective C View Controller code I Tried to import:

#import "UIImage+Overlay.swift"

And xcode gives me a lot of errors, it's looks like Objective C errors. It seems it doesn't understand swift code.

Errors: unexpected '@' in program when I declare the first property:

@property (nonatomic, strong) NSArray *contentImages;

As suggested by Leo Natan, I did try to import, but I think is because the name of my App. I did try all this kind of import, but still saying can't find it.

#import <ProductName/ProductModuleName-Swift.h>
#import <Say_Cheese_/Say_Cheese_-Swift.h>
#import <Say Cheese!/Say_Cheese_-Swift.h>
#import <Say Cheese!-Say_Cheese_-Swift.h>
#import <Say Cheese!-Bridging-Header.h>

My Product Name is "Say Cheese!" My Product Module Name is Say_Cheese_, but behind is $(PRODUCT_NAME:c99extidentifier)

I'm afraid to change the Product Module Name, is it safe?

2 Answers2

5

Do not import the Swift file. Instead, import the compiler generated "ProductModuleName-Swift.h" file.

Importing Swift into Objective-C

When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. This automatically generated file is an Objective-C header that declares the Swift interfaces in your target. It can be thought of as an umbrella header for your Swift code. The name of this header is your product module name followed by adding "-Swift.h". (You’ll learn more about the product module name later, in Naming Your Product Module.)

By default, the generated header contains interfaces for Swift declarations marked with the public modifier. It also contains those marked with the internal modifier if your app target has an Objective-C bridging header. Declarations marked with the private modifier do not appear in the generated header. Private declarations are not exposed to Objective-C unless they are explicitly marked with @IBAction, @IBOutlet, or @objc as well. If your app target is compiled with testing enabled, a unit test target can access any declaration with the internal modifier as if they were declared with the public modifier by prepending @testable to the product module import statement.

You don’t need to do anything special to create the generated header file—just import it to use its contents in your Objective-C code. Note that the Swift interfaces in the generated header include references to all of the Objective-C types used in them. If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types before importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.

In your project's target, find the product module name. Usually, it has the same name as the target. This will be the name of the compiler generated header file.

In your Objective C source file, import this header.

#import <ProductModuleName-Swift.h>

This header file is generated every time you build your project, so any changes to Swift files will require a compilation before being able to use in Objective C.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
-1

I got some tips at this link: How to import Swift code to Objective-C

But to solve this, I did use "" instead <> on #import

#import "SayCheese-Swift.h"

and then I define the Product Module Name to "SayCheese" without special chars and spaces.

Thanks Leo.

Community
  • 1
  • 1