xcode AFNetworking 无法编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29417613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:54:56  来源:igfitidea点击:

AFNetworking won't compile

iosxcodeafnetworkingcocoapods

提问by AndroidDev

I'm totally stuck trying to run a project on the simulator using AFNetworking. I've used this dependency before on other projects, so I don't understand what is going wrong here. First, the error when I try to run the project:

我完全无法尝试使用 AFNetworking 在模拟器上运行一个项目。我之前在其他项目上使用过这种依赖关系,所以我不明白这里出了什么问题。首先,当我尝试运行项目时的错误:

Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_AFHTTPSessionManager", referenced from:
      _OBJC_CLASS_$_SharedNetworkObject in SharedNetworkObject.o
  "_OBJC_CLASS_$_AFJSONResponseSerializer", referenced from:
      objc-class-ref in SharedNetworkObject.o
  "_OBJC_METACLASS_$_AFHTTPSessionManager", referenced from:
      _OBJC_METACLASS_$_SharedNetworkObject in SharedNetworkObject.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm sure that this is part of the problem, but I don't know how to resolve it:

我确定这是问题的一部分,但我不知道如何解决:

enter image description here

enter image description here

This is the offending .h file:

这是有问题的 .h 文件:

#import <Foundation/Foundation.h>
#import <AFNetworking/AFNetworking.h>
#import "SharedSessionKey.h"

@interface SharedNetworkObject : AFHTTPSessionManager

+ (SharedNetworkObject *) sharedNetworkObject;   // class method to return the singleton object

@end

It may be of interest to note that when I start typing the import line for AFNetworking, the line fills in after a few characters, so I know that there is some awareness of the presence of the AFNetworking dependency.

可能需要注意的是,当我开始为 AFNetworking 键入导入行时,该行会在几个字符后填充,因此我知道对 AFNetworking 依赖项的存在有所了解。

I installed the dependency using CocoaPods. Here is my Podfile:

我使用 CocoaPods 安装了依赖项。这是我的 Podfile:

# Uncomment this line to define a global platform for your project
platform :ios, '8.0'

source 'https://github.com/CocoaPods/Specs.git'

target 'WeRun' do
pod "AFNetworking", "2.5.2"
end

target 'WeRunTests' do

end

And, of course I am working in .xcworkspace(not .xcodeproj).

而且,当然我在工作.xcworkspace(不是.xcodeproj)。

One more thing, my xcconfig file looks like this, which matches with other successful AFNetworking builds:

还有一件事,我的 xcconfig 文件看起来像这样,它与其他成功的 AFNetworking 构建相匹配:

GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1
HEADER_SEARCH_PATHS = "${PODS_ROOT}/Headers/Public" "${PODS_ROOT}/Headers/Public/AFNetworking"
OTHER_CFLAGS = $(inherited) -isystem "${PODS_ROOT}/Headers/Public" -isystem "${PODS_ROOT}/Headers/Public/AFNetworking"
OTHER_LDFLAGS = -ObjC -l"Pods-MyApp-AFNetworking" -framework "CoreGraphics" -framework "MobileCoreServices" -framework "Security" -framework "SystemConfiguration"
OTHER_LIBTOOLFLAGS = $(OTHER_LDFLAGS)
PODS_ROOT = ${SRCROOT}/Pods

I don't know what else to look for. I've completely removed the pod and reinstalled, only to find the exact same error. Can anyone help me figure this out? Thanks!

我不知道还能找什么。我已经完全移除了吊舱并重新安装,却发现完全相同的错误。谁能帮我解决这个问题?谢谢!

回答by mhalttu

I spent a couple of hours fighting with this exact same issue, going through all the Google hits I could find. Building for the device was working, but building for the simulator was not.

我花了几个小时来解决这个完全相同的问题,浏览了我能找到的所有 Google 搜索结果。为设备构建工作正常,但为模拟器构建没有。

Here are the steps that finally solved the issue for me:

以下是最终为我解决问题的步骤:

  1. Clear the XCode caches:

    rm -rf ~/Library/Developer/Xcode/DerivedData/
    
  2. Clear the CocoaPods caches and reinstall the dependencies:

    rm -rf "${HOME}/Library/Caches/CocoaPods"
    rm -rf "`pwd`/Pods/"
    pod update
    
  3. Finally go to the "Pods" project and set the Build Active Architectures Onlyto Noalso for the "Debug" configuration.

  1. 清除 XCode 缓存:

    rm -rf ~/Library/Developer/Xcode/DerivedData/
    
  2. 清除 CocoaPods 缓存并重新安装依赖项:

    rm -rf "${HOME}/Library/Caches/CocoaPods"
    rm -rf "`pwd`/Pods/"
    pod update
    
  3. 最后转到“荚”的项目,并设置Build Active Architectures OnlyNo也为“调试”配置。

回答by Daniel Morandini

I would suggest you to add the $(inherited) flag into the "Build settings".

我建议您将 $(inherited) 标志添加到“构建设置”中。

  • Tap on the blue project icon
  • Select "Build settings" tab
  • Under "Linking" you'll find a setting called "Other Linker Flags"
  • add $(inherited) into the textfield
  • 点击蓝色项目图标
  • 选择“构建设置”选项卡
  • 在“链接”下,您会找到一个名为“其他链接器标志”的设置
  • 将 $(inherited) 添加到文本字段中

回答by Kiran Jasvanee

I had the same issue, when I had -lc++already set on Other linker flagsfor cpp library usage purpose.
I added these two flags -ObjC -l"AFNetworking"on Other linker flags, which worked and compiled successfully. These are the two flags -ObjCand -l"AFNetworking". You scan add simultaneously -ObjC -l"AFNetworking".

当我-lc++已经Other linker flags为 cpp 库使用目的设置时,我遇到了同样的问题。
我加入这两个标志-ObjC -l"AFNetworking"Other linker flags,其工作和成功编译。这是两个标志-ObjC-l"AFNetworking"。您同时扫描添加-ObjC -l"AFNetworking"

回答by Jassi

Do not give version after AFNetworking in pod file. And try again. Make sure you remove the comma also.

不要在 pod 文件中提供 AFNetworking 之后的版本。然后再试一次。确保你也删除了逗号。

回答by AndroidDev

Well, I resolved this, but I'm not sure why it worked. I was experimenting with the Other Linker Flags in the Build Settings. I deleted the flag that was put there by the CocoaPods installation (presumably) and replaced it by copying the flag from the .xcconfigfile. Specifically, I added this text: -l"Pods-WeRun-AFNetworking"as the second flag right after -ObjC. It looks to me to be exactly what I had just deleted, so I really hesitate to suggest this as an 'answer', but it works now, so something must have changed.

好吧,我解决了这个问题,但我不确定它为什么起作用。我正在尝试构建设置中的其他链接器标志。我删除了 CocoaPods 安装(大概)放置在那里的标志,并通过从.xcconfig文件中复制标志来替换它。具体来说,我添加了这个文本:-l"Pods-WeRun-AFNetworking"作为-ObjC. 在我看来,这正是我刚刚删除的内容,所以我真的很犹豫是否建议将其作为“答案”,但它现在有效,所以一定有什么改变了。

If anyone can shed any light on this or tell me that I am fooling myself, please let me know. Thanks for the suggestions.

如果有人能对此有所了解或告诉我我在自欺欺人,请告诉我。感谢您的建议。

回答by Rizwan Ahmed

A simple method. Download the repository file. Drag and drop the contents of the AFnetworking folder (You can create groups if necessary) then use the library. (Use #import "AFnetworking.h" )

一个简单的方法。下载存储库文件。拖放 AFnetworking 文件夹的内容(如有必要,您可以创建组)然后使用库。(使用 #import "AFnetworking.h" )