XCode 8 奇怪的操场错误,无法查找符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39237298/
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 09:11:12  来源:igfitidea点击:

XCode 8 strange Playground error, Couldn't Lookup Symbols

iosswiftxcodeuikitswift-playground

提问by PopKernel

I was trying to use auto layout in a Playground on Xcode 8. However, as soon as I added the line, I started receiving this error:

我试图在 Xcode 8 的 Playground 中使用自动布局。但是,一旦我添加了该行,我就开始收到此错误:

Playground execution failed: error: Couldn't lookup symbols:
  __TWPCSo6UIView17PlaygroundSupport22PlaygroundLiveViewableS0_
  __swift_FORCE_LOAD_$_swiftCoreGraphics
  __swift_FORCE_LOAD_$_swiftDarwin
  __TMaC17PlaygroundSupport14PlaygroundPage
  __swift_FORCE_LOAD_$_swiftObjectiveC
  __swift_FORCE_LOAD_$_swiftUIKit
  _playground_log_hidden
  _playground_logger_initialize
  __swift_FORCE_LOAD_$_swiftCoreImage
  __swift_FORCE_LOAD_$_swiftDispatch
  __TFC17PlaygroundSupport14PlaygroundPageau7currentS0_
  __swift_FORCE_LOAD_$_swiftFoundation
  __TFC17PlaygroundSupport14PlaygroundPages8liveViewGSqPS_22PlaygroundLiveViewable__

* thread #1: tid = 0x14b6b4, 0x000000010f2f43c0 MyPlayground`executePlayground, queue = 'com.apple.main-thread', stop reason = breakpoint 1.2
  * frame #0: 0x000000010f2f43c0 MyPlayground`executePlayground
    frame #1: 0x000000010f2f39c0 MyPlayground`__37-[XCPAppDelegate enqueueRunLoopBlock]_block_invoke + 32
    frame #2: 0x000000010fe0c89c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #3: 0x000000010fdf1944 CoreFoundation`__CFRunLoopDoBlocks + 356
    frame #4: 0x000000010fdf10b5 CoreFoundation`__CFRunLoopRun + 901
    frame #5: 0x000000010fdf0ad4 CoreFoundation`CFRunLoopRunSpecific + 420
    frame #6: 0x000000011518aa61 GraphicsServices`GSEventRunModal + 161
    frame #7: 0x0000000110996de4 UIKit`UIApplicationMain + 159
    frame #8: 0x000000010f2f36e9 MyPlayground`main + 201
    frame #9: 0x00000001132e068d libdyld.dylib`start + 1

I have no idea what this means. I mean, my interpretation is that Xcode can't find libraries like UIKit, but I know that is not the case, as I used UIKit and such fine until that line of auto layout crashed the party. The source code is really small, so I am including it in its entirety:

我不知道这是什么意思。我的意思是,我的解释是 Xcode 找不到像 UIKit 这样的库,但我知道事实并非如此,因为我使用了 UIKit 并且很好,直到那条自动布局线使派对崩溃。源代码真的很小,所以我把它完整包括在内:

import UIKit  
import PlaygroundSupport  
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))  
view.translatesAutoresizingMaskIntoConstraints = false  
view.backgroundColor = UIColor.white  
PlaygroundPage.current.liveView = view  
let field = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 50))  
field.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1).isActive = true  
field.backgroundColor = UIColor.red  
view.addSubview(field)

The third line from the bottom of the source seems to be causing the problem. Is this something that I messed up or is it a beta bug? I'm lost here.

源代码底部的第三行似乎是导致问题的原因。这是我搞砸的事情还是测试版错误?我在这里迷路了。

回答by beyowulf

Add fieldas a sub view of viewthen set your constraints. There is an exception that is raised when constraints are added for views not in the same view hierarchy.

添加field为子视图,view然后设置您的约束。为不在同一视图层次结构中的视图添加约束时会引发异常。

For example:

例如:

import UIKit
import PlaygroundSupport

let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
view.backgroundColor = UIColor.white
PlaygroundPage.current.liveView = view
let field = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 50))

view.addSubview(field)

field.translatesAutoresizingMaskIntoConstraints = false
field.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1).isActive = true
// add the rest of your constraints 
field.backgroundColor = UIColor.red

You also typically need to call view.layoutIfNeeded()

您通常还需要致电 view.layoutIfNeeded()

回答by zeeawan

I downloaded the playground file of swift tourfrom apple developer page and double click to open with Xcode 9.2 and got the same error mentioned above.

我从苹果开发者页面下载了swift tourplayground 文件,双击用 Xcode 9.2 打开,得到了和上面提到的一样的错误。

Then I created new playground file and pasted the contentsof downloaded file and it executed without errors.

然后我创建了新的 Playground 文件并粘贴了下载文件的内容,并且它没有错误地执行。

Edit#1:Even drag n drop files to newly created playground resolved the errors.

编辑#1:甚至将 n 个拖放文件拖到新创建的操场上也解决了错误。

Edit#2:When I reopened my newly created playground, it showed the same error. Then I navigated to different filesand this made swift process the files and errors disappeared. So, I repeated this with the downloaded file and those errors also disappeared and code executed OK.

编辑#2:当我重新打开我新创建的操场时,它显示了同样的错误。然后我导航到不同的文件,这使得快速处理文件和错误消失了。所以,我用下载的文件重复了这个,那些错误也消失了,代码执行正常。