gpt4 book ai didi

ios - 快速错误: Could not find an overload for '-' that accepts the supplied arguments

转载 作者:行者123 更新时间:2023-11-30 13:54:10 25 4
gpt4 key购买 nike

我正在尝试将现有的 Objective-C 代码行转换为 Swift。以下是我所拥有的。

import UIKit
import SpriteKit

let kWorldWidth = 6324
let kWorldHeight = 620
let kWorldTileSize = 62
let kWorldCenterX = 3162
let kWorldCenterY = 310

class ViewController: UIViewController {
weak var scene : SKScene?
var backgroundTextures = NSMutableArray()

override func viewDidLoad() {
super.viewDidLoad()
loadWorldTiles()
}

func loadWorldTiles() {
let tileAtlas = SKTextureAtlas(named:"Tiles")
backgroundTextures = NSMutableArray(capacity: 1020)
for var y = 0; y < 10; y++ {
for var x = 0; x < 102; x++ {
let tileNumber = (y * 102) + x
let tileNode = SKSpriteNode(texture:tileAtlas.textureNamed(String(format:"%d",tileNumber)))
var position: CGPoint = CGPointMake(x*kWorldTileSize-kWorldCenterX+0.5*kWorldTileSize,kWorldHeight-y*kWorldTileSize-kWorldCenterY-0.5*kWorldTileSize)
tileNode.position = position
backgroundTextures.addObject(tileNode)
}
}
}
}

当我有 var 位置时出现错误。错误消息是无法找到接受提供的参数的“-”重载。运行搜索,我发现当您没有显式声明变量类型时,您会收到此错误。如果我创建像这样的整数变量

var xval:CGFloat = x*kWorldTileSize-kWorldCenterX+0.5*kWorldTileSize
var yval:CGFloat = kWorldHeight-y*kWorldTileSize-kWorldCenterY-0.5*kWorldTileSize
var position: CGPoint = CGPointMake(xval,yval)

我仍然遇到同样的错误。

我做错了什么?

非常感谢。

最佳答案

类型推断正在将所有常量声明和 for 循环计数器变量设为 Int 类型,因为您使用的是 Int 文字。您本质上无法使用 IntDoubleCGFloat 等混合类型进行数学计算。另外,您最终会尝试创建一个 CGFloat 类型,因此最好将其显式地用于您的常量,如下所示:

import UIKit
import SpriteKit

let kWorldWidth: CGFloat = 6324
let kWorldHeight: CGFloat = 620
let kWorldTileSize: CGFloat = 62
let kWorldCenterX: CGFloat = 3162
let kWorldCenterY: CGFloat = 310

class ViewController: UIViewController {
weak var scene : SKScene?
var backgroundTextures = NSMutableArray()

override func viewDidLoad() {
super.viewDidLoad()
loadWorldTiles()
}

func loadWorldTiles() {
let tileAtlas = SKTextureAtlas(named:"Tiles")
backgroundTextures = NSMutableArray(capacity: 1020)
for var y = 0; y < 10; y++ {
for var x = 0; x < 102; x++ {
let tileNumber = (y * 102) + x
let tileNode = SKSpriteNode(texture:tileAtlas.textureNamed(String(format:"%d",tileNumber)))
var position: CGPoint = CGPointMake(CGFloat(x)*kWorldTileSize-kWorldCenterX+0.5*kWorldTileSize,kWorldHeight-CGFloat(y)*kWorldTileSize-kWorldCenterY-0.5*kWorldTileSize)
tileNode.position = position
backgroundTextures.addObject(tileNode)
}
}
}
}

关于ios - 快速错误: Could not find an overload for '-' that accepts the supplied arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33862850/

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