jjzjj

swift - 实例化子类在 Swift playgrounds 中有效,但使用类本身却不行

coder 2023-09-15 原文

我更新了 AudioKit 的代码,现在 playgrounds(在 develop 分支上)不能正常工作,在研究中我发现了一个奇怪的情况

var oscillator = AKOscillator()

产生崩溃,但只是使用该类的一个普通子类:

class AKSubclassOfOscillator: AKOscillator {}
var oscillator = AKSubclassOfOscillator()

工作得很好。有人可以阐明这怎么可能发生吗?这个 Swift Playground 在 Xcode 10 中运行/崩溃。应用程序中的类似代码不会崩溃,只会在 playground 内崩溃。

这是日志

Playground execution failed:

error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x0).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x000000010c8b2d2f libswiftCore.dylib`swift_getObjectType + 47
    frame #1: 0x000000010c86a855 libswiftCore.dylib`findDynamicValueAndType(swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*, swift::OpaqueValue*&, swift::TargetMetadata<swift::InProcess> const*&, bool&, bool, bool) + 245
    frame #2: 0x000000010c86efa7 libswiftCore.dylib`_dynamicCastToExistential(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetExistentialTypeMetadata<swift::InProcess> const*, swift::DynamicCastFlags) + 135
    frame #3: 0x000000010cbda1b7 PlaygroundLogger`___lldb_unnamed_symbol258$$PlaygroundLogger + 599
    frame #4: 0x000000010cbdc8e6 PlaygroundLogger`___lldb_unnamed_symbol269$$PlaygroundLogger + 262
    frame #5: 0x000000010cbdf02c PlaygroundLogger`___lldb_unnamed_symbol298$$PlaygroundLogger + 76
    frame #6: 0x000000010cbdf6f9 PlaygroundLogger`___lldb_unnamed_symbol333$$PlaygroundLogger + 9
    frame #7: 0x000000010cbdf07f PlaygroundLogger`___lldb_unnamed_symbol300$$PlaygroundLogger + 31
    frame #8: 0x000000010cbdf609 PlaygroundLogger`___lldb_unnamed_symbol318$$PlaygroundLogger + 9
    frame #9: 0x000000010c58a50f libswiftCore.dylib`(extension in Swift):Swift.Collection.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 527
    frame #10: 0x000000010c6d600e libswiftCore.dylib`protocol witness for Swift.Sequence.map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> in conformance Swift.LazyMapCollection<A, B> : Swift.Sequence in Swift + 78
    frame #11: 0x000000010c783347 libswiftCore.dylib`Swift._RandomAccessCollectionBox._map<A>((A.Element) throws -> A1) throws -> Swift.Array<A1> + 135
    frame #12: 0x000000010cbdcbaf PlaygroundLogger`___lldb_unnamed_symbol270$$PlaygroundLogger + 383
    frame #13: 0x000000010cbde62d PlaygroundLogger`___lldb_unnamed_symbol284$$PlaygroundLogger + 1357
    frame #14: 0x000000010cbdcd1a PlaygroundLogger`___lldb_unnamed_symbol270$$PlaygroundLogger + 746
    frame #15: 0x000000010cbde62d PlaygroundLogger`___lldb_unnamed_symbol284$$PlaygroundLogger + 1357
    frame #16: 0x000000010cbdbc84 PlaygroundLogger`___lldb_unnamed_symbol258$$PlaygroundLogger + 7460
    frame #17: 0x000000010cbd5752 PlaygroundLogger`___lldb_unnamed_symbol179$$PlaygroundLogger + 114
    frame #18: 0x000000010cbd78e8 PlaygroundLogger`playground_log_hidden + 264
    frame #19: 0x000000010d1e9427 $__lldb_expr10`main at Oscillator.xcplaygroundpage:16
    frame #20: 0x0000000108f881a0 com.apple.dt.Xcode.PlaygroundStub-macosx
    frame #21: 0x00007fff3643bf8c CoreFoundation`__invoking___ + 140
    frame #22: 0x00007fff3643be5f CoreFoundation`-[NSInvocation invoke] + 311
    frame #23: 0x00007fff3649d53d CoreFoundation`-[NSInvocation invokeWithTarget:] + 56
    frame #24: 0x00007fff5f53cbf5 ViewBridge`__68-[NSVB_ViewServiceImplicitAnimationDecodingProxy forwardInvocation:]_block_invoke_2 + 46
    frame #25: 0x00007fff5f501ec6 ViewBridge`-[NSViewServiceMarshal withHostWindowFrameAnimationInProgress:perform:] + 53
    frame #26: 0x00007fff5f53cbbe ViewBridge`__68-[NSVB_ViewServiceImplicitAnimationDecodingProxy forwardInvocation:]_block_invoke + 113
    frame #27: 0x00007fff33a19731 AppKit`+[NSAnimationContext runAnimationGroup:] + 55
    frame #28: 0x00007fff33a196e7 AppKit`+[NSAnimationContext runAnimationGroup:completionHandler:] + 82
    frame #29: 0x00007fff5f55f154 ViewBridge`runAnimationGroup + 295
    frame #30: 0x00007fff5f53c695 ViewBridge`+[NSVB_View _animateWithAttributes:animations:completion:] + 490
    frame #31: 0x00007fff5f53cb28 ViewBridge`-[NSVB_ViewServiceImplicitAnimationDecodingProxy forwardInvocation:] + 206
    frame #32: 0x00007fff3648580e CoreFoundation`___forwarding___ + 780
    frame #33: 0x00007fff36485478 CoreFoundation`__forwarding_prep_0___ + 120
    frame #34: 0x00007fff3643bf8c CoreFoundation`__invoking___ + 140
    frame #35: 0x00007fff3643be5f CoreFoundation`-[NSInvocation invoke] + 311
    frame #36: 0x00007fff3649d53d CoreFoundation`-[NSInvocation invokeWithTarget:] + 56
    frame #37: 0x00007fff5f504973 ViewBridge`-[NSVB_QueueingProxy forwardInvocation:] + 324
    frame #38: 0x00007fff3648580e CoreFoundation`___forwarding___ + 780
    frame #39: 0x00007fff36485478 CoreFoundation`__forwarding_prep_0___ + 120
    frame #40: 0x00007fff3643bf8c CoreFoundation`__invoking___ + 140
    frame #41: 0x00007fff3643be5f CoreFoundation`-[NSInvocation invoke] + 311
    frame #42: 0x00007fff3649d53d CoreFoundation`-[NSInvocation invokeWithTarget:] + 56
    frame #43: 0x00007fff3648580e CoreFoundation`___forwarding___ + 780
    frame #44: 0x00007fff36485478 CoreFoundation`__forwarding_prep_0___ + 120
    frame #45: 0x00007fff3643bf8c CoreFoundation`__invoking___ + 140
    frame #46: 0x00007fff3643be5f CoreFoundation`-[NSInvocation invoke] + 311
    frame #47: 0x00007fff5f4d16cd ViewBridge`__deferNSXPCInvocationOntoMainThread_block_invoke + 237
    frame #48: 0x00007fff5f4c6af3 ViewBridge`__wrapBlockWithVoucher_block_invoke + 37
    frame #49: 0x00007fff5f4c68a4 ViewBridge`__deferBlockOntoMainThread_block_invoke_2 + 553
    frame #50: 0x00007fff36469ca7 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #51: 0x00007fff3642cced CoreFoundation`__CFRunLoopDoBlocks + 395
    frame #52: 0x00007fff3642c3eb CoreFoundation`__CFRunLoopRun + 1204
    frame #53: 0x00007fff3642bce4 CoreFoundation`CFRunLoopRunSpecific + 463
    frame #54: 0x00007fff356c5895 HIToolbox`RunCurrentEventLoopInMode + 293
    frame #55: 0x00007fff356c55cb HIToolbox`ReceiveNextEventCommon + 618
    frame #56: 0x00007fff356c5348 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 64
    frame #57: 0x00007fff3398295b AppKit`_DPSNextEvent + 997
    frame #58: 0x00007fff339816fa AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1362
    frame #59: 0x00007fff5f4cf226 ViewBridge`-[NSViewServiceApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 92
    frame #60: 0x00007fff3397b75d AppKit`-[NSApplication run] + 699
    frame #61: 0x00007fff3396ae97 AppKit`NSApplicationMain + 780
    frame #62: 0x00007fff636fbf1f libxpc.dylib`_xpc_objc_main + 612
    frame #63: 0x00007fff636fb9e5 libxpc.dylib`xpc_main + 433
    frame #64: 0x00007fff5f4c0a76 ViewBridge`-[NSXPCSharedListener resume] + 16
    frame #65: 0x00007fff5f4c9a03 ViewBridge`NSViewServiceApplicationMain + 2877
    frame #66: 0x0000000108f881ca com.apple.dt.Xcode.PlaygroundStub-macosx`main + 42
    frame #67: 0x00007fff634c5085 libdyld.dylib`start + 1

最佳答案

此代码依赖于 _Self,我认为这不是记录在案的行为,并且可能并非在所有用法中都可靠。我相信这段代码在任何地方都使用了 _Self,这意味着使用 type(of: self)

关于swift - 实例化子类在 Swift playgrounds 中有效,但使用类本身却不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52800459/

有关swift - 实例化子类在 Swift playgrounds 中有效,但使用类本身却不行的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  5. ruby - 在 Ruby 中使用匿名模块 - 2

    假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于

  6. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  7. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  8. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  9. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  10. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

随机推荐