简体   繁体   中英

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. 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.

Until now, myself or XCode itself managed to fix those little issues, but I cant help myself with this:

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. 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? texturesIso.append won't work either.

  4. Am I right with the Swift Update? If yes, is there an overview of all the sudden changes to Swift, because I could'n find one.

I would really appreciate anyone's help, thanks a lot in advance.

This tutorial appears to use an older version of swift. You can either use an older version of xcode that uses an earlier swift compiler or update the code. 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

Just change the class var to 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

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

defines a getter but no setter, therefore there is no way for you to set the self.texturesIso property. 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.

  1. There was a swift update yes. The new version is 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. Also, it was trying to change immutable tiles in gameScene, so I changed that to a 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? has some good information on computed class variables vs static variables, although it doesn't talk about the protocol bit of the question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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