简体   繁体   中英

iOS/Swift: Simple way to declare variable or property in NSObject

A newcomer to Swift, I have created an NSObject class, item.h in a separate file to keep track of my items. Each item has assorted properties.

In Objective-C, I would have just declared the properties as

@property(nonatomic) NSString *name; and so forth in the interface file.

I have tried to create something similar in Swift as follows:

import UIKit

class item: NSObject {
    var topSpeed: Double
    var aStrProperty: String
    var anIntProperty: Int
}

However, I am getting error: Class 'item' has no initializer . In the Apple docs, they seem to suggest creating a struct but I was hoping the NSObject would be the object for my items as I have customarily done in Objective-C

Would appreciate it if someone could explain the right way to give my object properties.

Thanks in advance for any suggestions.

In Swift, all properties must have a value when they are initialised. Currently, you haven't specified what the properties' values are.

Why not listen to what the error says and add an initialiser?

class item: NSObject {
    var topSpeed: Double
    var aStrProperty: String
    var anIntProperty: Int

    init(topSpeed: Double, aStrProperty: String, anIntProperty: Int) {
        self.topSpeed = topSpeed
        self.aStrProperty = aStrProperty
        self.anIntProperty = anIntProperty
    }
}

Alternatively, you can add a ! or ? to make them all optionals. This way, you are making all the properties' initial values be nil .

var topSpeed: Double!
var aStrProperty: String!
var anIntProperty: Int!

if you want to omit the init method then Your Property Either should have default value or they should be optional.

If you don't want it then you should create required init method to initialise all the properties

You have to have some default values for your properties. There multiple ways to achieve this. Either you create like suggested a init method for assigning those default values.

class item: NSObject {
    var topSpeed: Double
    var aStrProperty: String
    var anIntProperty: Int

    override init() {
        topSpeed = 120.0
        aStrProperty = "aStrValue"
        anIntProperty = 1
    }
}

You can also directly assign the values in the class.

class item: NSObject {
    var topSpeed: Double = 120.0
    var aStrProperty: String = "aStrValue"
    var anIntProperty: Int = 1
}

Or your mark your properties as optional. Then you have to unwrap each property when you are using them.

class item: NSObject {
    var topSpeed: Double?
    var aStrProperty: String?
    var anIntProperty: Int?
}

If you would use a struct. The init method is created automatically for you.

The properties in your class are not declared as optional (so cannot be nil) and do not have an initial value. So in the initialiser you need to give these properties a value.

If you were to use a struct instead of a class, you would automatically get a initialiser method:

struct Item {
    var topSpeed: Double
    var aStrProperty: String
    var anIntProperty: Int
}

let item = Item(topSpeed: 200, aStrProperty: "Car", anIntProperty: 2)

But with a class you would be required to implement this yourself:

class Item: NSObject {
    var topSpeed: Double
    var aStrProperty: String
    var anIntProperty: Int

    init(speed: Double, str: String, int: Int) {
        topSpeed = speed
        aStrProperty = str
        anIntProperty = int

        super.init()
    }
}

let item = Item(speed: 200, str: "Car", int: 2)

You should use Structs wherever possible, read the documentation here for an explanation of each.

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