简体   繁体   English

Swift语法更新? 协议中不允许的类。 无法填充SKTexture

[英]Swift Syntax Update? Classes not allowed in protocol. Can't fill SKTexture

I'm currently working on a tutorial for creating an iOS Isometric Game. 我目前正在编写有关创建iOS等距游戏的教程。 You can find this one here . 你可以在这里找到这个

I just started coding Swift and because a lot of Syntax errors appeared while working with tutorials a little older than 3 months I asked my self if there were some main updates in Swift lately. 我刚刚开始对Swift进行编码,因为使用3个月以上的教程时会出现很多语法错误,所以我问自己最近Swift中是否有一些主要更新。

Until now, myself or XCode itself managed to fix those little issues, but I cant help myself with this: 到目前为止,我自己或XCode本身都已设法解决了这些小问题,但是我对此无能为力:

protocol TextureObject {
    class var sharedInstance: TextureDroid {get}
    var texturesIso:[[SKTexture]?] {get}
    var textures2D:[[SKTexture]?] {get}
}

This is the protocol I'm trying to set (exactly like in the tutorial), but XCode won't let me define the class variable. 这是我要设置的协议(与本教程中的完全一样),但是XCode不允许我定义类变量。 The error code is the following: 错误代码如下:

"Class properties are only allowed within classes: 
use static to declare a static property"

Both replacing "class" to "static" (which makes no logical sense to me) and deleting the protocol (and define the class that should inherit without the use of the protocol) lead to another error in this code: 将“类”替换为“静态”(这对我来说没有逻辑意义)并删除协议(并定义了不使用协议即可继承的类),这会导致此代码中的另一个错误:

class TextureDroid: TextureObject  {

class var sharedInstance: TextureDroid {
    return textureDroid
}

let texturesIso:[[SKTexture]?]
let textures2D:[[SKTexture]?]

init() {

    texturesIso = [[SKTexture]?](count: 2, repeatedValue: nil)
    textures2D = [[SKTexture]?](count: 2, repeatedValue: nil)

    //Idle
    texturesIso[Action.Idle.rawValue] = [
        SKTexture(imageNamed: "iso_3d_"+textureImage(Tile.Droid, Direction.N, Action.Idle)),
        SKTexture(imageNamed: "iso_3d_"+textureImage(Tile.Droid, Direction.NE, Action.Idle)),
        SKTexture(imageNamed: "iso_3d_"+textureImage(Tile.Droid, Direction.E, Action.Idle)),
        SKTexture(imageNamed: "iso_3d_"+textureImage(Tile.Droid, Direction.SE, Action.Idle)),
        SKTexture(imageNamed: "iso_3d_"+textureImage(Tile.Droid, Direction.S, Action.Idle)),
        SKTexture(imageNamed: "iso_3d_"+textureImage(Tile.Droid, Direction.SW, Action.Idle)),
        SKTexture(imageNamed: "iso_3d_"+textureImage(Tile.Droid, Direction.W, Action.Idle)),
        SKTexture(imageNamed: "iso_3d_"+textureImage(Tile.Droid, Direction.NW, Action.Idle)),
    ]

This error appears in all the lines where I want to fill the texturesIso 该错误出现在我要填充纹理的所有行中

Immutable value 'self.texturesIso' may not be assigned to

Here are my questions: 这是我的问题:

  1. How can I fix the first error? 如何解决第一个错误? Is there a new way to define classes inside a protocol? 有没有一种在协议内定义类的新方法?

  2. Are those two errors connected, or is the second just appearing, because i managed to eliminate the first? 是因为我设法消除了第一个错误,所以连接了这两个错误,还是刚刚出现了第二个错误?

  3. How can I fill the SKTexture in the right way? 如何以正确的方式填充SKTexture? texturesIso.append won't work either. texturesIso.append也不起作用。

  4. Am I right with the Swift Update? 我对Swift Update正确吗? If yes, is there an overview of all the sudden changes to Swift, because I could'n find one. 如果是的话,是否有Swift突然变化的概述,因为我找不到。

I would really appreciate anyone's help, thanks a lot in advance. 非常感谢任何人的帮助,在此先感谢。

This tutorial appears to use an older version of swift. 本教程似乎使用的是较早版本的swift。 You can either use an older version of xcode that uses an earlier swift compiler or update the code. 您可以使用使用较早的swift编译器的旧版xcode,也可以更新代码。 I will attempt to help you update the code. 我将尝试帮助您更新代码。

  1. "Protocol declarations can't contain class, structure, enumeration, or other protocol declarations. The protocol member declarations are discussed in detail below." “协议声明不能包含类,结构,枚举或其他协议声明。下面将详细讨论协议成员声明。” - Apple docs on swift protocol declaration -关于快速协议声明的Apple文档

Just change the class var to static. 只需将var类更改为static。 This doesn't cause the second error, its just that the compiler stops after the first one so the second one isn't revealed. 这不会导致第二个错误,只是编译器在第一个错误之后停止,因此第二个错误不会显示出来。

  1. Immutable value 'self.texturesIso' may not be assigned to 不变值'self.texturesIso'可能未分配给

    var texturesIso:[[SKTexture]?] {get} var texturesIso:[[SKTexture]?] {get}

defines a getter but no setter, therefore there is no way for you to set the self.texturesIso property. 定义了一个getter但没有setter,因此您无法设置self.texturesIso属性。 Change that to {get set}, and doing the same for the textures2d property might be necessary too, and change them to vars in the class. 将其更改为{get set},并且也有必要对textures2d属性执行相同的操作,并将其更改为类中的vars。

  1. There was a swift update yes. 有一个快速更新是。 The new version is Swift 1.2 新版本是Swift 1.2

On top of answering this specific question, when I downloaded the tutorial code, to get it to compile, I also needed to change the touchesEnded to use Set instead of NSSet, which breaks touches.anyObject, so I used touches.first instead. 除了回答这个特定问题外,当我下载教程代码以进行编译时,我还需要更改touchesEnded以使用Set而不是NSSet,它会破坏touches.anyObject,因此我改用touches.first。 Also, it was trying to change immutable tiles in gameScene, so I changed that to a var. 另外,它试图更改gameScene中的不可变图块,因此我将其更改为var。

Note: I just downloaded the code and got it to compile and run, I'm not exactly sure how it is supposed to run, but it seemed ok to me. 注意:我只是下载了代码,然后将其编译并运行,我不确定该如何运行,但是对我来说似乎还可以。 Also Does swift have class level static variables? swift是否具有类级别的静态变量? has some good information on computed class variables vs static variables, although it doesn't talk about the protocol bit of the question 关于计算类变量和静态变量有一些很好的信息,尽管它没有讨论问题的协议部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM