Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: create an object class with Swift

I created this class for my object 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"

like image 573
cyclingIsBetter Avatar asked Feb 09 '15 10:02

cyclingIsBetter


People also ask

How do you create a class object in Swift?

Swift Initializer We have used the self.name to refer to the name property of the bike1 object. If we use an initializer to initialize values inside a class, we need to pass the corresponding value during the object creation of the class. Here, "Mountain Bike" is passed to the name parameter of init()

How do you define a class in Swift?

In Swift, you define a structure or class in a single file, and the external interface to that class or structure is automatically made available for other code to use. An instance of a class is traditionally known as an object.

What is OOP in iOS Swift?

Object-Oriented Programming (OOP) helps you structure your Swift code with so-called classes. These classes have properties and functions, and classes can inherit attributes from each other.

What is a Swift class and object?

In this tutorial, we will learn about Swift Classes and we'll also learn to create objects with the help of examples. Swift is also an object-oriented programming language. And, like other oop languages, it also supports the concept of objects and classes. An object is simply a collection of data (variables) and methods (functions).

Do you have to have an interface in Swift?

Unlike other programming languages, Swift doesn’t require you to create separate interface and implementation files for custom structures and classes. In Swift, you define a structure or class in a single file, and the external interface to that class or structure is automatically made available for other code to use.

What is Swift language for iOS?

Swift is a powerful and intuitive programming language for iOS, iPadOS, macOS, tvOS, and watchOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design, yet also produces software that runs lightning-fast.

How do you define a new type in Swift?

Whenever you define a new structure or class, you define a new Swift type. Give types UpperCamelCase names (such as SomeStructure and SomeClass here) to match the capitalization of standard Swift types (such as String, Int, and Bool ).


2 Answers

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

  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 optionals for 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
    
like image 108
Antonio Avatar answered Sep 24 '22 02:09

Antonio


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.

Fix it by adding following statement:

city = City()
like image 24
Mithra Singam Avatar answered Sep 21 '22 02:09

Mithra Singam