Swift错误:无法找到' - '接受提供的参数

时间:2015-11-23 02:26:26

标签: ios swift

我试图将现有的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位置。错误消息是找不到&#39; - &#39;接受提供的参数。运行搜索时,我发现当您没有明确声明变量类型时,您将收到此错误。如果我创建整数变量,如

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)

我仍然得到同样的错误。

我做错了什么?

Muchos thankos。

1 个答案:

答案 0 :(得分:0)

类型推断正在进行所有常量声明和类型为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)
            }
        }
    }
}