我正在尝试使用 Swift 实现 NSOutlineViewDataSource,当使用 Swift 字符串时,应用程序会崩溃。当我将数组 people 更改为 NSString 时,它工作正常。
根据 Working with Cocoa Data Types当您导入 Foundation 时,字符串桥接应该是自动的。
数据源代码如下:
import Cocoa
import Foundation
class ViewController: NSViewController, NSOutlineViewDataSource {
let people: [String] = ["Mary", "John", "Paul"] // when using NSString it works fine
func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int {
return item == nil ? people.count : 0
}
func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {
return false
}
func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {
return people[index]
}
func outlineView(outlineView: NSOutlineView, objectValueForTableColumn tableColumn: NSTableColumn?, byItem item: AnyObject?) -> AnyObject? {
return item
}
}
知道为什么会这样吗?
Xcode 版本 6.1 (6A1052d)
Xcode 项目托管在这里:https://github.com/tanob/ExampleNSOutlineView
最佳答案
我总是有点不愿意将我不理解的行为标记为错误,但这对我来说就像一个错误。
如果您在填充表时记录传递给 outlineView:objectValueForTableColumn:byItem 的值,则在第三次调用之后 item 的值是意外的(使用NSString 该值始终是您的项目之一):
func outlineView(outlineView: NSOutlineView,
objectValueForTableColumn tableColumn: NSTableColumn?,
byItem item: AnyObject?) -> AnyObject? {
callCount++
if callCount > people.count {
println(item)
return "junk"
} else {
return item
}
}
// output ->
// call count: 1; <item> value: Optional(Mary)
// call count: 2; <item> value: Optional(John)
// call count: 3; <item> value: Optional(Paul)
// call count: 4; <item> value: Optional(<__NSBlockVariable__: 0x60000004cb40>)
// call count: 5; <item> value: Optional(<RIPData 0x608000043300>) // surely not good!
// call count: 5; <item> value: Optional(<RIPData 0x608000043390>)
// ...
大多数情况下,此实现也会崩溃,但偶尔会加载大纲 View - 当然三个单元格中的每一个都具有值 junk。
同样有趣的是,在基于单元格的 NSTableView 中使用 String 值似乎没有任何问题 - 我将您的数据放入一个没有问题。
所以我对你的问题的简短回答为什么会这样? 将是一个试探性的因为你发现了一个错误 - 但是基于单元格的 NSOutlineView 错误,而不是 String 错误。
关于macos - NSOutlineViewDataSource 和 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28284723/
当我尝试在macOSMojave(10.14.1)上构建带有ruby支持(--enable-rubyinterp)的vim(8.1.0509)时,出现错误:Infileincludedfrom/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/ruby.h:33:/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/ruby/ruby.h:24:10:fatalerror:'ru
将MacOS升级到10.14.2Mojave后,我无法再使用RVM安装任何Ruby版本。它总是给出这样的错误:$rvminstall2.5.3ruby-2.5.3-#removingsrc/ruby-2.5.3..Searchingforbinaryrubies,thismighttakesometime.Nobinaryrubiesavailablefor:osx/10.14/x86_64/ruby-2.5.3.Continuingwithcompilation.Pleaseread'rvmhelpmount'togetmoreinformationonbinaryrubies.Ch
Ignoringbinding_of_caller-0.7.2becauseitsextensionsarenotbuilt.Try:gempristinebinding_of_caller--version0.7.2Ignoringbyebug-9.0.6becauseitsextensionsarenotbuilt.Try:gempristinebyebug--version9.0.6Ignoringcapybara-webkit-1.11.1becauseitsextensionsarenotbuilt.Try:gempristinecapybara-webkit--versio
考拉版本:2.2.0Errormessage:/scss/styles.scss/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/dependency.rb:319:into_specs':Couldnotfind'sass'(>=0)among15totalgem(s)(Gem::LoadError)Checkedin'GEM_PATH=/Users/monstercritic/.gem/ruby/2.3.0:/Library/Ruby/Gems/2.3.0:/Syst
我正在使用macOSHighSierra并一直在尝试通过rbenv安装ruby2.5.0但不断收到如下错误AppleLLVMversion9.0.0(clang-900.0.39.2)Target:x86_64-apple-darwin17.4.0Threadmodel:posixInstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bincompiling./main.ccompilingdmydln.ccompilingminiinit.cc
我尝试使用RCenvironment、.bashrc、.MacOSX/environment.plist和/etc/launchd.conf设置这些并重新启动计算机....IntelliJ仍然没有获取我的环境变量:gem文件p"ENVIRONMENT:#{ENV['VAR_PRIVATE_GEM_USERNAME']}"#输出"ENVIRONMENT:" 最佳答案 查看相关问题:https://apple.stackexchange.com/q/51677https://apple.stackexchange.com/q/57385
努力奔跑if[["$TRAVIS_OS_NAME"=="osx"]];thenbrewinstallpython3;fi在before_install中,我最终得到了/usr/local/Homebrew/Library/Homebrew/brew.rb:12:in\`':HomebrewmustberununderRuby2.3!(RuntimeError)Thecommand"if[["$TRAVIS_OS_NAME"=="osx"]];thenbrewinstallpython3;fi"failedandexitedwith1during.Yourbuildhasbeenstop
如何在macOS上卸载rbenv?我的rbenv版本一团糟,brewuninstallrbenv无法正常工作,因为brew依赖于ruby。 最佳答案 我采取了这些步骤,成功地摆脱了运行ElCapitan(10.11.4)的mac上的rbenv。根据我的经验,Rbenv是Failedtobuildgemnativeextensionerrors的主要原因。使用brew删除:brewremoverbenv删除.rbenv目录:rm-rf~/.rbenv对于Bash打开.bash_profile文件并删除其中包含rbenv的所有行:v
在使用Ruby和Rails一段时间后,我想试试RVM。一切正常,除了一件事:在新打开的终端中,ruby指向系统的ruby,尽管我使用了rvm--default命令。user@terra~$ruby-vruby1.8.7(2010-08-16patchlevel302)[i686-darwin10]user@terra~$whichruby/opt/local/bin/rubyuser@terra~$rvmlistruby-1.8.7-p334[]=>ruby-1.9.2-p180[]调用rvmreload后一切正常user@terra~$rvmreloaduser@terra~$r
我无法尝试在我的Mac上安装Rails。我有OSX10.6.8,我已经确认我有Ruby,版本1.8.7我运行了sudogemupdate和sudogemupdate--system来获取最新版本的软件。但是,当我运行sudogeminstallrails时,出现了这个错误:ERROR:Errorinstallingrails:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/rubyextconf.rbmkmf.rbcan'tfindh