如何将Github中的集成代码添加到现有项目中

时间:2016-03-19 00:30:34

标签: ios objective-c swift github


我想在我想要开发的应用程序中获取设备位置。
我创建了一个SWIFT项目,我想在这里添加代码
https://github.com/intuit/LocationManager
进入我现有的快速项目。

所以在这里说

Manually from GitHub

Download all the files in the INTULocationManager subdirectory.
Add the source files to your Xcode project (drag and drop is easiest).
Import the INTULocationManager.h to your bridging header.
Swift: Add #import "INTULocationManager.h" to your bridging header.

所以基本上我可以将源代码拖到我的项目中吗?

另外,我如何创建桥接头?
然后让我说如何在我的ViewController中使用它?

由于

1 个答案:

答案 0 :(得分:1)

也许你应该考虑使用CocoaPods,因为它可能更容易集成,而且从长远来看,更容易更新。

要回答有关手动安装的具体问题:

,您只需将源代码拖到项目中即可。可以在互联网上找到创建桥接头的说明(例如,here)。

接下来,如说明所示,将#import "INTULocationManager.h"添加到您的桥接标题中,例如:

#ifndef TestBridgingHeader_Bridging_Header_h
#define TestBridgingHeader_Bridging_Header_h

#import "INTULocationManager.h"

#endif /* TestBridgingHeader_Bridging_Header_h */

要使用该库,您必须将Github页面上给出的示例从Objective-C翻译为Swift。

let locMgr = INTULocationManager.sharedInstance()
locMgr.requestLocationWithDesiredAccuracy(INTULocationAccuracy.City, timeout: 10, block: { currentLocation, achievedAccuracy, status in
    if status == .Success {

    } else if status == .TimedOut {

    } else {

    }
})

通过记住一些规则,这变得更容易:

  • Obj-C中的方法调用通过括起方括号(例如[INTULocationManager sharedInstance])来调用,而在Swift中它们使用点语法(例如INTULocationManager.sharedInstance()
  • INTULocationAccuracyCity之类的枚举通常只会转换为最后一部分(在本例中为.City)。 Swift推断.City相当于INTULocationAccuracy.City
  • 诸如INTULocationRequestBlock之类的Obj-C块等同于相同类型的Swift闭包。