gpt4 book ai didi

ios - 计算属性 getter 出错

转载 作者:行者123 更新时间:2023-11-28 08:11:08 25 4
gpt4 key购买 nike

我在 iOS Swift 3 应用程序中有以下计算属性。

var countItems:[Int] {// Count the interesting items.
var countResult = [Int]()
for i in 0..<size {
var count = 0
for j in i*size..<(i+1)*size {
if binaryArray?[j] == true {count += 1}
}
countResult.append(count)
}
return countResult
}

它在 99.9% 的时间里都能完美运行。但是这条消息让我崩溃了:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16fc7bff0)
frame #0: 0x000000018f2d95b8 libsystem_malloc.dylib`malloc_zone_malloc
frame #1: 0x000000018f2dc56c libsystem_malloc.dylib`malloc + 32
frame #2: 0x0000000100964048 libswiftCore.dylib`swift_slowAlloc + 12
frame #3: 0x000000010096409c libswiftCore.dylib`_swift_allocObject_ + 28
frame #4: 0x0000000100f6cd50 libswiftSwiftOnoneSupport.dylib`generic specialization <preserving fragile attribute, Swift._ArrayBuffer<Swift.Double> with Swift._ArrayBuffer<Swift.Double> : Swift._ArrayBufferProtocol in Swift> of (extension in Swift):Swift._ArrayBufferProtocol._forceCreateUniqueMutableBuffer (countForNewBuffer : Swift.Int, minNewCapacity : Swift.Int) -> Swift._ContiguousArrayBuffer<A.Element> with unmangled suffix "_merged" + 84
frame #5: 0x0000000100f69d5c libswiftSwiftOnoneSupport.dylib`generic specialization <preserving fragile attribute, Swift.String.CharacterView> of Swift.Array._copyToNewBuffer (oldCount : Swift.Int) -> () with unmangled suffix "_merged" + 76
frame #6: 0x0000000100f655bc libswiftSwiftOnoneSupport.dylib`generic specialization <preserving fragile attribute, Swift.UInt64> of Swift.Array.append (A) -> () with unmangled suffix "_merged" + 124
* frame #7: 0x0000000100103078 MyApp`TheClass.countItems.getter(self=MyApp.TheClass @ 0x000000016fc7c260) at TheClass.swift:31
frame #8: 0x0000000100138354 MyApp`TheClass.callingFunction(self=0x000000010201d930) -> Bool at TheClass.swift:2343

我想知道计算属性本身是否存在问题(我没有看到)或者是否一定是外部出了问题。如果有人有线索,我会很高兴。在此先感谢您提供任何相关提示。

作为引用,这是另一个带有较短消息的崩溃:

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16fc27fb0)
frame #0: 0x00000001001567bc MyApp`TheClass.countItems.getter(self=MyApp.TheClass @ 0x000000016fc27fb0) at TheClass.swift:0
* frame #1: 0x000000010018c2e4 MyApp`TheClass.callingFunction(self=0x000000010200c730) -> Bool at TheClass.swift:2348

最佳答案

请尝试在后台线程中工作,因为您似乎在主线程中进行所有数字处理,这并不好。

要么你可以创建一个返回整数项数组的函数。它将在后台线程中处理数组。

因为我不知道什么是 binaryArray 变量。如果需要,可以传入。

SWIFT 3.0:

func countItems(_ size : Int, completion: ([Int])) {
let queue = DispatchQueue.global(qos: .background)
queue.async {
var countResult = [Int]()
for i in 0..<size {
var count = 0
for j in i*size..<(i+1)*size {
if self.binaryArray?[j] == true {count += 1}
}
countResult.append(count)
}
completion(countResult)
}
}

关于ios - 计算属性 getter 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43730028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com