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?
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.
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.
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.
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.
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.
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.
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.
It's not enough, you must add this bridge header filename also in your Build Settings - Swift Compiler Code Generation like in this picture:
Don't forget also this (required by SDWebImage
):
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
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With