Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import objective c header file in swift project

enter image description here

I am trying to use SDWebImage in a Swift project. I dragged and dropped the SDWebImage library folder into my Swift project and created a bridging header by name listingApp-Bridging-Header.h

Then I imported the header file into my Swift file; like so imported

    import UIImageView+WebCache.h  

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell : TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell


    let strTitle : NSString = arrDict[indexPath.row].valueForKey("clip_name") as! NSString

      cell.clipName.text = strTitle as String


    cell.videoImage sd_setImageWithURL=NSURL URLWithString,[NSString stringWithFormat:@"%@%@",K_Server_imageurl,[record valueForKey:@"clip_image_path"]];



        return cell

}

It's giving me an error saying add ; before + How would I import the file above into my Swift project correctly?

like image 798
virat Avatar asked Jul 18 '16 06:07

virat


People also ask

Can you mix Objective-C and Swift?

Can I mix them in the same class? Yes! Just like you can extend classes in ObjC, you can extend classes with Swift as well.

How can Swift and Objective-C be used in the same project?

To access and use swift classes or libraries in objective-c files start with an objective-c project that already contains some files. Add a new Swift file to the project. In the menu select File>New>File… then select Swift File, instead of Cocoa Touch Class. Name the file and hit create.

How do I import an Objective-C file into Swift?

Alternatively, you can create a bridging header yourself by choosing File > New > File > [ operating system] > Source > Header File. Edit the bridging header to expose your Objective-C code to your Swift code: In your Objective-C bridging header, import every Objective-C header you want to expose to Swift.

How do I use the Objective-C bridging header in Swift?

In your Objective-C bridging header, import every Objective-C header you want to expose to Swift. In Build Settings, in Swift Compiler - General, make sure the Objective-C Bridging Header build setting has a path to the bridging header file.

Can Objective-C and Swift coexist in a single project?

Objective-C and Swift files can coexist in a single project, whether the project was originally an Objective-C or Swift project. You can simply add a file of the other language directly to an existing project.

What is a header in Objective-C?

This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code. You don’t need to do anything special to create the generated header—just import it to use its contents in your Objective-C code.


2 Answers

You need to quote the header file name when importing it in the bridging header. Like this:

#import "UIImageView+WebCache.h";

This is exactly the same syntax as importing header files in objc.

like image 172
Fujia Avatar answered Oct 22 '22 09:10

Fujia


It's not enough, you must add this bridge header filename also in your Build Settings - Swift Compiler Code Generation like in this picture:

enter image description here

Don't forget also this (required by SDWebImage):

enter image description here

About the next step:

imageDownloader.downloadImageWithURL(
                        NSURL(string: urlString!),
                        options: SDWebImageDownloaderOptions.UseNSURLCache,
                        progress: nil,
                        completed: { (image, data, error, bool) -> Void in
                            if image != nil {
                                self.bannerImageView.image = image

                            }
                    })
like image 36
Alessandro Ornano Avatar answered Oct 22 '22 08:10

Alessandro Ornano