简体   繁体   English

Swift中没有类/结构自动完成

[英]No Class/Struct Autocomplete in Swift

I currently have two structs and a class in my sample code. 我的示例代码中目前有两个结构和一个类。 One struct is for Tiger, one is for Balloon and on is for Lion. 一种结构适用于Tiger,一种适用于Balloon,另一种适用于Lion。 Each are below: 每个都在下面:

struct Tiger {
    var name = ""
    var age = 0
    var breed = ""
    var image = UIImage(named: "")
    func chuff() {
        println("\(name): Chuff Chuff")
    }
    func chuffNumberOfTimes(numberOfTimes:Int) {
        for (var i = 0; i < numberOfTimes; i++) {
            self.chuff()
        }
    }
    func ageInTigerYearsFromAge(regularAge:Int) -> Int {
        return regularAge * 3
    }
    func randomFact() -> String {
        let randomFactNumber = Int(arc4random_uniform(UInt32(3)))
        var randomFact:String
        switch randomFactNumber {
        case 0:
            randomFact = "Tigers are 10 feet tall."
        case 1:
            randomFact = "Tigers are amazing."
        case 2:
            randomFact = "Tigers have 10 feet."
        default:
            randomFact = ""
        }
        return randomFact
    }
}
struct Balloon {
    var number = 0
    var image = UIImage(named: "")
}
class Lion {
    var name = ""
    var age = 0
    var isAlphaMale = false
    var image = UIImage(named: "")
    var subSpecies = ""
}

Each one is in its own file named identically to the struct/class & ".swift". 每个文件都在其自己的文件中,其名称与struct / class和“ .swift”相同。 However, the only one that autocompletes itself while typing in ViewController.swift is the Tiger struct. 但是,只有在输入ViewController.swift时自动完成自动填充的是Tiger结构。 For instance, if I were to set myTiger = Tiger( it would suggest name: String, age: Int etc. How can I get the other two to do the same? I really like defining the variables inline instead of having 5 lines of code to define all of the variables. 例如,如果我要设置myTiger = Tiger(它会建议name: String, age: Int等。我如何才能使另外两个做到这一点呢?我真的很喜欢内联定义变量,而不是用5行代码定义所有变量。

Is anyone familiar with how that works or why the Tiger struct would do it while the Balloon and Lion structs don't? 有谁熟悉它的工作原理,或者为什么Tiger结构会执行而Balloon和Lion结构却不这样做?

Classes don't get the initializers for free as the Struct does, if you want to have an initializer for a class you have to create it: 类不能像Struct那样免费获得初始化器,如果您想为一个类提供初始化器,则必须创建它:

class Lion {
    var name = ""
    var age = 0
    var isAlphaMale = false
    var image = UIImage(named: "")
    var subSpecies = ""

    init(name: String, age:Int, isAlphaMale: Boolean, image: UIImage, subSpecies: String){
        self.name = name 
        self.age = age 
        self.isAlphaMale = isAlphaMale 
        self.image = image 
        self.subSpecies = subSpecies 
    }
} 

The Balloon should work automatically, probably a clean and build in your code so xcode can be updated will fix the problem. Balloon应该会自动工作,可能是干净的并且可以在您的代码中进行构建,以便可以更新xcode来解决问题。

From Apple Documentation : 苹果文档

Memberwise Initializers for Structure Types 结构类型的成员初始化器

All structures have an automatically-generated memberwise initializer , which you can use to initialize the member properties of new structure instances. 所有结构都有一个自动生成的逐成员初始化器 ,您可以使用该初始化器来初始化新结构实例的成员属性。 Initial values for the properties of the new instance can be passed to the memberwise initializer by name: 可以通过名称将新实例的属性的初始值传递给成员初始化器:


copied from a previus part of the document to add context 从文档的前一部分复制来添加上下文

struct Resolution { var width = 0 var height = 0 }


let vga = Resolution(width: 640, height: 480)

Unlike structures, class instances do not receive a default memberwise initializer. 与结构不同,类实例不接收默认的成员初始化器。

With the class above you will have the initializer as in the screenshot below: 在上面的类中,您将具有初始化器,如下面的屏幕截图所示:

在此处输入图片说明

As you can see in the screenshot Balloon works perfectly fine as well 正如您在屏幕截图中所看到的,气球工作也很好

在此处输入图片说明

No need to enter once to start work, xcode is not perfect and sometimes it needs a clean and a new build to make it be in sync with your classes and structs. 无需输入一次就可以开始工作,xcode并不完美,有时它需要一个全新的版本才能使其与您的类和结构同步。

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

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