21

Now as swift is the new language to develop iOS apps.

How can we integrate with the AFNetworking or using NSURLSession will be a better option ???

Please help me out..

Anurag Kabra
  • 444
  • 1
  • 5
  • 18
  • 8
    AFNetworking for Swift is now called AlamoFire: https://github.com/Alamofire/Alamofire – koen Aug 01 '14 at 12:01
  • 1
    ^ AlamoFire is still missing some functionality expected by AFN users, such as setImageWithURL: – d2burke Feb 24 '15 at 19:31

2 Answers2

32

You have to add AFNetworking to your swift project

In Build Settings -> Defines Module set to Yes

In Build Settings -> Swift compiler -> Objective-C bridging file set the name of bridging file 'ProjectName-Bridging-Header.h' for example

In ProjectName-Bridging-Header.h write:

#import "AFNetworking.h"
Alexey Sidorov
  • 846
  • 1
  • 9
  • 17
  • 6
    For these who search in settings - key is 'Defines Module' (under Packaging) – Nat Jul 03 '14 at 14:08
  • One trick: If you already add ObjC file and accept that it to create Bridging file, but delete it and try do this again, XCode don't ask if you want to create it anymore. Restart the XCode and voilá. – seufagner Jul 18 '14 at 23:57
  • 2
    In "Objective-C bridging file", set the name to: "ProjectName/bridge.h" – ullstrm Jul 25 '14 at 13:55
  • I had a Swift project with a bridge header for Parse SDK. Now I added AFNetworking and added AFNetworking.h to the birding header. Restarted Xcode but it can't find AFHTTPClient, AFJSONRequestOperations and others. – marciokoko Oct 04 '14 at 16:21
  • This is probably an ignorant question, but how exactly should I add AFNetworking. It does not have a sperate project with its own build settings, but only loose Objective-C files. How can I go about this? – gebirgsbärbel Oct 09 '14 at 17:04
12

Any Objective-C framework (or C library) that’s accessible as a module can be imported directly into Swift. This includes all of the Objective-C system frameworks—such as Foundation, UIKit, and SpriteKit—as well as common C libraries supplied with the system. For example, to import Foundation, simply add this import statement to the top of the Swift file you’re working in:

you just need to import them into your project and create bridging header. you can find detailed explanation How to call Objective-C code from Swift

Community
  • 1
  • 1
modusCell
  • 13,151
  • 9
  • 53
  • 80