这是我遇到的错误,请查看所附图片以获取更多信息。
com.apple.scenekit.scnview-renderer(17):EXC_BAD_ACCESS(代码= 1,地址= 0xf000000010a10c10)
我可以在调用以下函数时重现此错误,但前提是此函数在一秒钟内被多次调用。如果用户快速点击该按钮以循环到下一辆可用的汽车,则会发生这种情况。
如您所见,我尝试将其包装在a中DispatchQueue
以解决我的问题。
您还会注意到,我创建了一个Bool alreadyCyclingCars
来跟踪该cycleCarNext()
函数是否已完成,然后再允许再次调用它。
这个功能主要是通过所有的迭代unlockedCars
中unlockedCars
阵列。
如果汽车的类型与我们当前正在寻找的汽车相匹配,我会打破循环。
然后我们确定当前汽车的索引,以查看我需要显示的下一辆汽车是否是数组中的下一辆汽车(如果有)(如果没有),我们到达了数组的末尾,因此我显示了第一辆汽车在数组中。
有谁比我对此了解更多?真的很感谢,谢谢!
func cycleCarNext() {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
if !self.alreadyCyclingCars {
self.alreadyCyclingCars = true
var indexOfCurrentCar = 0
for (index, car) in UserData.shared.unlockedCars.enumerated() {
if car.type == self.overlayScene.currentCarOnDisplay {
indexOfCurrentCar = index
break
}
}
if indexOfCurrentCar < UserData.shared.unlockedCars.count - 1 {
let nextCar = UserData.shared.unlockedCars[indexOfCurrentCar+1]
self.playerNode.removeFromParentNode()
self.playerNode = …
Run Code Online (Sandbox Code Playgroud) 我注意到,即使没有明确要求(即在闭包之外),业内的一些人也会使用self关键字.
例:
import UIKit
import MapView
import CoreLocation
class viewController: UIViewController, MKMapViewDelegate, CLLocationDelegate {
let mapView = MKMapView()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
self.mapView.showsUserLocation = true
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
}
Run Code Online (Sandbox Code Playgroud)
运行时间方面是否有明显的好处?或者这纯粹是一种风格选择?
我刚刚在我的应用中添加了本地通知。仅当应用区域设置regionCode
(即Locale.current.regionCode
)为“ US”或“ CA”时,才触发这些通知。我对语言环境不感兴趣language
。
我还将要编写补充的测试用例,但是一旦我知道如何编写一个测试用例,其他的就自然而然地遵循了。
因此,我的问题是:如何将Locale注入测试中(请参阅参考资料testSuccessfulNotificationDelivery()
)?
LocalNotificationTests.swift:
class LocalNotificationTests: XCTestCase {
let notification1 = LocalNotification(toTriggerInSeconds: 5)
let notification2 = LocalNotification(toTriggerInSeconds: 6)
// This object manages LocalNotifications
// by building them into UNUserNotifications
// and then scheduling them using UNUserNotificationCenter.
let notificationManager = NotificationManager()
func testSuccessfulNotificationDelivery() {
// setup locale and use it for testing, somehow
let = Locale(identifier: "en_CA")
// The answer to my question would …
Run Code Online (Sandbox Code Playgroud) 以下是摘要:
libMobileGestalt MobileGestalt.c:890:此平台不支持MGIsDeviceOneOfType
。
我已经将应用程序从Swift 2更新到4.2。大多数错误修复只是在更新语法。
任何有助于解决此问题的见解将不胜感激!
private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: Any]?) -> Bool {
setConstants()
window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = RootViewController.sharedInstance
window!.makeKeyAndVisible()
preloadKeyboard()
return true
}
Run Code Online (Sandbox Code Playgroud)
RootViewController的共享实例:
static let sharedInstance = RootViewController(nibName: "RootViewController", bundle: nil)
Run Code Online (Sandbox Code Playgroud)
为什么这不是重复的问题:
我试图只使用递归(并没有内置函数)编写一个函数,它消耗两个数字,x和y并产生总和
1 + x + x^2 + ... + x^(y-1) + x^y
请注意,我正在寻找一种不使用for/while循环的方法,因为我还没有学习它们.到目前为止,我有以下功能:
def power_addition (x, y):
i = 0
if i < y:
i = i+1
return x**i + power_addition (x, y)
else:
return x**i
Run Code Online (Sandbox Code Playgroud)
据我所知,代码中断有一个特殊原因.