iOS:用 Swift 创建一个对象类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28407942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:45:20  来源:igfitidea点击:

iOS: create an object class with Swift

iosoopswiftnsobject

提问by cyclingIsBetter

I created this class for my object City

我为我的对象 City 创建了这个类

class City: NSObject {

    var _name:String = ""
    var name:String {
        get {
            return _name
        }
        set (newVal) {
            _name = newVal
        }
    }
}

then when I create my object I do:

然后当我创建我的对象时:

var city:City!

city.name = "London" //crash here

println("name city is\(city.name)");

it crash when I set the name with message "fatal error: unexpectedly found nil while unwrapping an Optional value"

当我使用消息“致命错误:在解开可选值时意外发现 nil”设置名称时它崩溃了

回答by Antonio

This is not actually an answer (see other answers for a solution, such as @Greg's and @zelib's), but an attempt to fix some mistakes I see in your code

这实际上不是答案(请参阅解决方案的其他答案,例如 @Greg 和 @zelib),而是尝试修复我在您的代码中看到的一些错误

  1. No need to create computed + stored property (unless you have a reason for that):

    class City: NSObject {
        var name: String = ""
    }
    
  2. If you inherit from NSObject, you automatically lose all swift features - avoid it (unless you have a reason for that)

    class City {
        var name: String = ""
    }
    
  3. You are using an empty string as absence of value - swift provides optionalsfor that

    class City {
        var name: String?
    }
    
  4. Alternative to 3., a city without a name wouldn't make much sense, so you probably want each instance to have a name. Use non optional property and an initializer:

    class City {
        var name: String
        init(name: String) {
            self.name = name
        }
    }
    
  5. Avoid implicitly unwrapped optionals (unless you have a reason for that):

    var city: City
    
  1. 无需创建计算 + 存储属性(除非您有理由这样做):

    class City: NSObject {
        var name: String = ""
    }
    
  2. 如果您继承自NSObject,您将自动失去所有 swift 功能 - 避免使用它(除非您有理由这样做)

    class City {
        var name: String = ""
    }
    
  3. 您正在使用空字符串作为缺少值 - swift为此提供了选项

    class City {
        var name: String?
    }
    
  4. 作为 3. 的替代方案,没有名称的城市没有多大意义,因此您可能希望每个实例都有一个名称。使用非可选属性和初始值设定项:

    class City {
        var name: String
        init(name: String) {
            self.name = name
        }
    }
    
  5. 避免隐式解包的选项(除非你有理由这样做):

    var city: City
    

回答by Mithra Singam

Just like any other object oriented programming language, and object should be initialized before accessing it.
Like:

就像任何其他面向对象的编程语言一样,在访问对象之前应该对其进行初始化。
喜欢:

var city:City!

This is just reference of the object. So, actual memory is not created here. You need to create actual object for City Class.

这只是对象的引用。所以,这里没有创建实际的内存。您需要为 City Class 创建实际对象。

Fix it by adding following statement:

通过添加以下语句来修复它:

city = City()

回答by Greg

You haven't initialised the city variable and when you trying to use it it crash.

您尚未初始化 city 变量,当您尝试使用它时它会崩溃。

initialise it first before you use it:

在使用之前先初始化它:

city = City()
city.name = "London"

回答by Zell B.

You are getting error because you are not initializing your city variable instead you just implicitly unwrap it without initializing at any stage. To initialize it you must use the following code

您收到错误是因为您没有初始化您的城市变量,而是隐式地解包它而没有在任何阶段进行初始化。要初始化它,您必须使用以下代码

var city:City = City()

回答by Dhruv Ramani

You have to call the init method. So you would do it like this :

您必须调用 init 方法。所以你会这样做:

var city:City=City() //Calls init and creates an instance
city.name="foo"

If you don't define an init method(it's always a good practice that you do), the default init method is called.

如果您没有定义 init 方法(这样做总是一个好习惯),则会调用默认的 init 方法。

回答by Sushobhit

Create a object with value

创建一个有值的对象

var cityName = City("cityName")// create a object with string value