我正在尝试向节点添加滑动手势,以便当用户滑动它时,它会离开屏幕,但我不断收到SIGABRT
错误消息:
`Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[fidget2.PlankScene swipedRight:]: unrecognized selector sent to instance 0x7ff4c3603e00'`
Run Code Online (Sandbox Code Playgroud)
我不确定为什么会弹出这个错误。我确保节点在.sks
文件中正确标记。这是我的代码:
import SpriteKit
let plankName = "woodPlank"
class PlankScene: SKScene {
var plankWood : SKSpriteNode?
override func didMove(to view: SKView) {
plankWood = childNode(withName: "woodPlank") as? SKSpriteNode
let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
}
func swipedRight(sender: UISwipeGestureRecognizer) {
print("Object has been swiped")
}
func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent)
{
let touch …
Run Code Online (Sandbox Code Playgroud) 我有一组 CGPoints:
let points = [(1234.0, 1053.0), (1241.0, 1111.0), (1152.0, 1043.0)]
Run Code Online (Sandbox Code Playgroud)
我想做的是找到 CGPoints 的中心。所以我可以将一个对象放置在所有点的中心。如果这是一个整数数组,我会像这样减少数组:
points.reduce(0, +)
Run Code Online (Sandbox Code Playgroud)
然后除以数组总数以获得平均值。但由于它的 CGPoints 这不起作用。关于如何实现这一目标有什么想法吗?
我已经设置推送通知并在appdelegate中工作,但是一旦应用程序打开,我就不会让它们启动,我希望它们在用户注册应用程序后在单独的"允许推送通知"视图控制器上启动.我尝试appDelegate
通过这样做来引用:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
Run Code Online (Sandbox Code Playgroud)
我有一个registerForPushNotifications
功能appDelegate
:
func registerForPushNotifications(application: UIApplication) {
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
Run Code Online (Sandbox Code Playgroud)
从didFinishLaunchingWithOptions
函数内部调用时有效,AppDelegate
但是当我尝试在我的视图控制器上调用该函数时,我没有任何东西可以传递给函数的应用程序部分,所以我最终得到一个错误.
@IBAction func yesButtonPressed(sender: AnyObject) {
appDelegate.registerForPushNotifications(application)
performSegueWithIdentifier("pushToHomeSegue", sender: self)
}
Run Code Online (Sandbox Code Playgroud) push-notification apple-push-notifications ios appdelegate swift
我正在尝试以编程方式在Swift中创建标签,但我遇到的问题是基于数据模型,文本的数量可能会发生变化,从而改变标签的大小.通常我会在知道文本之前创建这样的标签:
headerLabel.frame = CGRect(x: 0, y: 0, width: screenSize.width/2.5, height: screenSize.height/45)
headerLabel.center = CGPoint(x: screenSize.width/2, y: 245)
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,文本可以是从一行到一个段落的任何数量,因此硬编码高度将不起作用.如何创建标签,以便它可以容纳任何数量的文本?
我正在尝试创建一个函数来检查值是否为nil,以及是否为其提供默认值.这是我到目前为止:
func appendMessage(message: String?, fixLength: Int){
if let message = message {
} else {
message = DEFAULT_CHAR//this is a default value, error is here
}
messageBody.append(getFixedLengthString(message, fixLength))
}
Run Code Online (Sandbox Code Playgroud) 目前我有一个协议:
protocol CameraOpen: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func openCamera()
}
Run Code Online (Sandbox Code Playgroud)
该函数在协议扩展中定义:
extension CameraOpen where Self: UIViewController {
func openCamera() {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题是我不知道如何实际测试协议扩展中定义的函数。通常我会创建一个我想要测试的类的实例并使用点符号来测试类中的每个函数,但由于它是一个协议,我无法创建它的实例。例如,如果我做这样的事情:
class CamerOpenTests: XCTestCase {
var cameraOpen: CameraOpen?
override func setUp() {
super.setUp()
cameraOpen = CameraOpen()// error here
}
override func tearDown() {
cameraOpen = nil
super.tearDown()
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
无法构造“CameraOpen”,因为它没有可访问的初始值设定项
关于如何对这个函数进行单元测试的任何想法?
swift ×6
ios ×5
appdelegate ×1
arrays ×1
cgpoint ×1
dynamic ×1
frame ×1
protocols ×1
skscene ×1
skspritenode ×1
sprite-kit ×1
uilabel ×1
unit-testing ×1