Swift: error: linker command failed with exit code 1

Swift: error: linker command failed with exit code 1

有类似的问题,但 none 回答了我的问题。

我正在使用 Swift 2.0 我正在开发一个使用 CoreLocation 显示经度和纬度的项目。

我也在使用社交框架 post 到 Twitter 和 Facebook。

我收到一个错误 "error: linker command failed with exit code 1 " 然后它告诉我“(使用 -v 查看调用)”但我不明白。

我将在 SO 上写下位置服务部分的答案。这是 link

这是我的代码:

import UIKit
import Social
import CoreLocation

@UIApplicationMain

class FirstViewController: UIViewController, CLLocationManagerDelegate, UIApplicationDelegate {


var window: UIWindow?
var locationManager: CLLocationManager!
var seenError : Bool = false
var locationFixAchieved: Bool = false
var locationStatus : NSString = "Not Started"

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    initLocationManager();
    return true
}

func initLocationManager() {
    seenError = false
    locationFixAchieved = false
    locationManager = CLLocationManager()
    locationManager.delegate = self
    CLLocationManager.locationServicesEnabled()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    locationManager.requestAlwaysAuthorization()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    locationManager.stopUpdatingLocation()
    if (error == true) {
        if (seenError == false) {
            seenError = true
            print(error)
        }
    }
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if (locationFixAchieved == false) {
        locationFixAchieved = true
        let locationArray = locations as NSArray
        let locationObj = locationArray.lastObject as! CLLocation
        let coord = locationObj.coordinate

        print(coord.latitude)
        print(coord.longitude)
    }
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    var shouldIAllow = false

    switch status {
    case CLAuthorizationStatus.Restricted:
        locationStatus = "Restricted Access to location"
    case CLAuthorizationStatus.Denied:
        locationStatus = "User denied access to location"
    case CLAuthorizationStatus.NotDetermined:
        locationStatus = "Status not determined"
    default:
        locationStatus = "Allowed to location Access"
        shouldIAllow = true
    }
    NSNotificationCenter.defaultCenter().postNotificationName("LabelHasBeenUpdated", object: nil)
    if (shouldIAllow == true) {
        NSLog("Location to Allowed")
        //Start location services
        locationManager.startUpdatingLocation()
    } else {
        NSLog("Denied access: \(locationStatus)")
    }
}



@IBAction func postToFacebookButton(sender: UIButton) {
    if(SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook)){
        let socialController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        //creates post with pre-desired text
        socialController.setInitialText("")

        self.presentViewController(socialController, animated: true, completion: nil)
    }
}


@IBAction func postTweetButton(sender: UIButton) {
    if(SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter)){
        let socialController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
        //creates post with pre-desired text
        socialController.setInitialText("")

        self.presentViewController(socialController, animated: true, completion: nil)
    }
}

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
}

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//layer.cornerRadius layer.cornerRadius

}

整个错误消息:

重复符号 _main 在: /Users/user/Library/Developer/Xcode/DerivedData/FarOut-ekrxzlgzfahpruavmlhyhiwiynum/Build/Intermediates/FarOut.build/Debug-iphonesimulator/FarOut.build/Objects-normal/x86_64/AppDelegate.o /Users/user/Library/Developer/Xcode/DerivedData/FarOut-ekrxzlgzfahpruavmlhyhiwiynum/Build/Intermediates/FarOut.build/Debug-iphonesimulator/FarOut.build/Objects-normal/x86_64/FirstViewController.o ld:1 个重复的建筑符号 x86_64 clang:错误:linker 命令失败,退出代码为 1(使用 -v 查看调用)

你的代码在我的 Xcode 中运行良好。我认为删除派生数据后,清理和重建会正常工作。还有一件事,您需要拆分 AppDelegate 和 ViewController 的代码,因为它们有自己的角色。

同一条错误消息有这么多不同的问题。(Linker command failed with exit code 1)

1) 如果你有两个 same constants 在不同的 类 那么也会发生这个问题。

2) 如果您不小心在实现文件中导入了 .m file 而不是 .h file

3) 如果您导入了同一个库的两个不同版本,也会出现此错误,在这种情况下,只需删除旧版本并仅保留一个版本即可。

4) 在“Project”而不是“Targets”中添加“other linker flags”。所以,你把它移到“Targets”,它不应该在“Project”。

5)project->target->build settings->search enable bitcode->DEBUG中设置NO查看

看看这个..如果没问题就按一下吧。

菜单 > 产品 > 清理 ... 然后 运行 项目

希望对你有帮助.. :)