我试图在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
我不知道这意味着什么。我的意思是,我的解释是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)
来自源底部的第三行似乎导致了问题。这是我搞砸了的东西还是beta bug?我迷路了。
答案 0 :(得分:3)
添加field
作为view
的子视图,然后设置约束。为不在同一视图层次结构中的视图添加约束时会引发异常。
例如:
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
您通常还需要致电view.layoutIfNeeded()
答案 1 :(得分:0)
我从苹果开发者页面下载了playground file of swift tour并双击打开Xcode 9.2并得到了与上述相同的错误。
然后我创建了新的操场文件并粘贴了下载文件的内容,并且执行时没有错误。
修改#1:强> 即使拖放文件到新创建的游乐场也可以解决错误。
修改#2:强> 当我重新打开我新创建的游乐场时,它显示了同样的错误。然后我导航到不同的文件,这使得文件和错误的快速处理消失了。所以,我用下载的文件重复了这个,这些错误也消失了,代码执行正常。