我试图在Swift中用Collection View创建一个简单的Soundboard,每个按钮代表一个可以播放的声音。结构如下(我知道,这可能不是最聪明的方法,但是直到我添加更多声音之前它才起作用):我有一个SoundFiles.swift和SoundFiles类,我声明了
static let shared = SoundFiles()
它包含两个数组
let soundfiles: [String] = ["example_bla"]
let soundnames: [String] = ["example bla"]
以及
var translation: [String: String] = [:]
var currentSoundfiles: [String] = []
其中“翻译”字典应该在文件名和屏幕上显示的内容之间有所区别,即它与数组“ soundfiles”和“ soundnames”匹配。应用过滤器(搜索功能)后,“ currentSoundfiles”数组将处理显示的声音文件。 在我的标签栏控制器中,我有一个包含以下内容的视图控制器
var soundPlayers: [Sound?] = []
override func viewDidLoad() {
super.viewDidLoad()
setUpView()
setupSwiftySound()
setupDismissKeyboard()
SoundFiles.shared.currentSoundfiles = SoundFiles.shared.soundfiles
SoundFiles.shared.findTranslation()
fillDropDowns()
fillSounds()
}
override func viewDidAppear(_ animated: Bool) {
refreshCollectionView()
}
其中
func fillSounds(){
soundPlayers.removeAll()
for (index, _) in SoundFiles.shared.currentSoundfiles.enumerated(){
if let playingURL = Bundle.main.url(forResource: SoundFiles.shared.currentSoundfiles[index], withExtension: "wav"){
soundPlayers.append(Sound(url: playingURL))
soundPlayers[index]?.volume = SoundFiles.shared.volume
}
}
}
是唯一相关的功能。只要我包含49个或更少的声音文件,一切都可以正常工作。包括50个或更多声音文件,多次发生以下错误/警告:
SwiftySound初始化错误:错误域= NSOSStatusErrorDomain 代码= -42“(空)”
奇怪的是,我仍然可以运行前49个声音文件而不会出现问题(单击其他按钮则无济于事),但是任何其他操作都会导致应用崩溃,例如尝试更改为该应用程序的其他View Controller或通过单击按钮旁边的“ +”(通过下拉菜单实现)来检索声音文件的其他信息。尝试转到第二个“视图控制器”选项卡时,崩溃错误如下:
由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法在捆绑包中加载NIB:“ NSBundle(已加载)”,名称为“ fGY-5H-E9k-view-obO-1i-lrO”,目录为“ SbOne” .storyboardc'' ***首先抛出调用堆栈: (0x185e48ec4 0x185019a50 0x185d4f594 0x1b2c8fea8 0x1b2a208e0 0x1b2a2128c 0x1b2a21554 0x1b298dea8 0x1b298e1b0 0x1b298f140 0x1b2990440 0x1b2972630 0x1b349177c 0x18a444b7c 0x18a449b34 0x18a3a8598 0x18a3d6ec8 0x18a3d7d30 0x185dd87cc 0x185dd3460 0x185dd3a00 0x185dd31f0 0x18804c584 0x1b2fe8c00 0x102474838 0x185892bb4) libc ++ abi.dylib:以类型为NSException的未捕获异常终止 (lldb)
和XCode将我带到显示
的AppDelegate.swift线程1:信号SIGABRT
该问题与所包括的声音文件无关,仅取决于数量。有人知道这里发生了什么吗?
答案 0 :(得分:0)
正如@DavidPasztor指出的那样,问题是内存问题,因为我一次打开了太多声音文件。将awk ' ##Starting awk program here.
BEGIN{ ##Mentioning BEGIN section here which will be executed once and before reading Input_file only.
b["min"]=3 ##Creating an array named b whose index is string min and value is 3.
b["max"]=5 ##Creating an array named b whose index is string max and value is 5.
b["median"]=4 ##Creating an array named b whose index is string median and value is 4.
} ##Closing BLOCK section here.
FNR==NR{ ##Checking condition FNR==NR which will be executed when 1st Input_file named config.txt is being read.
c[$1] ##Creating an array named c whose index is $1.
++d[$1] ##Creating an array named d and with index is $1 whose value is keep increasing with 1 on its each occurence.
a[$1 d[$1]]=tolower($NF) ##Creating an array named a whose index is $1 and value of d[$1] and value is small letters value of $NF(last column) of current line.
next ##Using next keyword of awk to skip all further statements from here.
}
($1 in c){ ##Checking conditions if $1 of current line is present of array c then do following.
if(e[$1]<d[$1]){ ##Checking condition if value of e[$1] is lesser than d[$1] then do following.
++e[$1] ##Creating array named e whose index is $1 and incrementing its value with 1 here.
}
else{ ##Using else for above if condition here.
e[$1]!=""?e[$1]:++e[$1] ##Checking if e[$1] is NULL then increment it with 1 or leave it as it is.
}
print $1,$2,$b[a[$1 e[$1]]] ##Printing 1st, 2nd fields value along with field value of array b whose index is value of array a with index of $1 e[$1] here.
}' config.txt file.txt ##Mentioning Input_files here.
属性减小为1确实可以解决该问题,但是当然会在某些时候再次发生。我现在为我解决问题的方式如下:
我为每个声音保留5个播放器,以使其能够在声音播放之前多次播放(对于我的其他Storyboard / View Controller来说,这很重要,用户应该从第一个Storyboard / View Controller中选择较短的声音列表)查看控制器以进行快速选择)。在第一个Storyboard / View Controller中,我放弃了playersPerSound
函数,而是在应该播放声音时使用了以下代码:
fillSounds
此代码段现在不再使用每个声音5个播放器,而是为相同的声音添加一个新播放器(如果再次播放),并且如果大小达到20,则删除前10个播放器。其他Storyboards / View控制器不受限制,即板上的声音数量不能超过20(或任何其他合理的数量)。到目前为止,我还没有遇到这种方式的应用程序任何问题。