How to import objective c header file in swift project
Asked Answered
L

2

6

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?

Leyden answered 18/7, 2016 at 6:26 Comment(9)
Do you have use_frameworks! uncommented in your podfile?Telium
Possible duplicate of How to call Objective C code from SwiftInvert
i am not using pod fileLeyden
can you show the errorCordy
see this once it helps you https://mcmap.net/q/1635160/-how-to-install-sdwebimageCordy
@Leyden check my answerCircularize
see the image i have addedLeyden
now it is giving me error in viewController.swift file that : at this line import UIImageView+WebCache.h like this it is giving me error saying add ; before + sign that consecutive statements must be separated by ;Leyden
ok i have edited my code have a look so how should use sdimage now to set my image view to show imagesLeyden
T
8

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.

Thedrick answered 18/7, 2016 at 6:31 Comment(9)
imported header file like #import UIImageView+WebCatche.h in the bridging header alsoLeyden
Then you should be able to use the code in swift without importing anything in swift file.Thedrick
it is giving error in my bringing header file as "UIImageView+WebCatche.h" file not foundLeyden
You need to check the project header search path. And adding the 'umbrella' header would be better than adding headers individually.Thedrick
now it is giving me error in viewController.swift file that : at this line import UIImageView+WebCache.h like this it is giving me error saying add ; before + sign that consecutive statements must be separated by ;Leyden
sorry, i forget to append semi-colon after the import statement. See my updated answer.Thedrick
You don't need to import anything in your swift file because you have imported them in the bridging header.Thedrick
ok i have edited my code have a look so how should use sdimage now to set my image view to show images @FijiaLeyden
thanks your comment : You don't need to import anything in your swift file because you have imported them in the bridging header. – worked for meLeyden
G
8

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

                            }
                    })
Glidebomb answered 18/7, 2016 at 6:30 Comment(9)
it is giving error in my bringing header file as "UIImageView+WebCatche.h" file not foundLeyden
done with all your suggested steps but now its giving me error in bridging header file as : "UIImageView+WebCatche.h" file not foundLeyden
Have you drag and drop the full proj?Glidebomb
now it is giving me error in viewController.swift file that : at this line import UIImageView+WebCache.h like this it is giving me error saying add ; before + sign that consecutive statements must be separated by ;Leyden
now it is giving me error in viewController.swift file that : at this line import UIImageView+WebCache.h like this it is giving me error saying add ; before + sign that consecutive statements must be separated by ;Leyden
You must also make a clean of the project, try to remove derivedData, then re-build allGlidebomb
not working deleting derived data also please check the image i have addedLeyden
ok i have edited my code have a look so how should use sdimage now to set my image view to show imagesLeyden
No please, stop it. You cannot change your question on the fly whenever you want, please open another question and flag this as answered (choose the answer you want): people have just answer to the main question title, if you change your question content this thread become completely incomprehensibleGlidebomb
T
8

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.

Thedrick answered 18/7, 2016 at 6:31 Comment(9)
imported header file like #import UIImageView+WebCatche.h in the bridging header alsoLeyden
Then you should be able to use the code in swift without importing anything in swift file.Thedrick
it is giving error in my bringing header file as "UIImageView+WebCatche.h" file not foundLeyden
You need to check the project header search path. And adding the 'umbrella' header would be better than adding headers individually.Thedrick
now it is giving me error in viewController.swift file that : at this line import UIImageView+WebCache.h like this it is giving me error saying add ; before + sign that consecutive statements must be separated by ;Leyden
sorry, i forget to append semi-colon after the import statement. See my updated answer.Thedrick
You don't need to import anything in your swift file because you have imported them in the bridging header.Thedrick
ok i have edited my code have a look so how should use sdimage now to set my image view to show images @FijiaLeyden
thanks your comment : You don't need to import anything in your swift file because you have imported them in the bridging header. – worked for meLeyden

© 2022 - 2024 — McMap. All rights reserved.