खोज…


टिप्पणियों

init() कक्षाओं में एक विशेष विधि है जिसका उपयोग कक्षा के लिए इनिशियलाइज़र घोषित करने के लिए किया जाता है। अधिक जानकारी यहां पाई जा सकती है: शुरुआती

एक कक्षा को परिभाषित करना

आप एक वर्ग को इस तरह परिभाषित करते हैं:

class Dog {}

एक वर्ग दूसरे वर्ग का उपवर्ग भी हो सकता है:

class Animal {}
class Dog: Animal {}

इस उदाहरण में, Animal भी एक प्रोटोकॉल हो सकता है जो Dog अनुरूप है।

संदर्भ शब्दार्थ

कक्षाएं संदर्भ प्रकार हैं , जिसका अर्थ है कि कई चर एक ही उदाहरण को संदर्भित कर सकते हैं।

class Dog {
    var name = ""
}

let firstDog = Dog()
firstDog.name = "Fido"

let otherDog = firstDog  // otherDog points to the same Dog instance
otherDog.name = "Rover"  // modifying otherDog also modifies firstDog

print(firstDog.name)  // prints "Rover"

क्योंकि कक्षाएं संदर्भ प्रकार हैं, भले ही वर्ग एक स्थिर है, इसके चर गुणों को अभी भी संशोधित किया जा सकता है।

class Dog {
    var name: String // name is a variable property.
    let age: Int // age is a constant property.
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}

let constantDog = Dog(name: "Rover", age: 5)// This instance is a constant.
var variableDog = Dog(name: "Spot", age 7)// This instance is a variable.

constantDog.name = "Fido" // Not an error because name is a variable property.
constantDog.age = 6 // Error because age is a constant property.
constantDog = Dog(name: "Fido", age: 6)
/* The last one is an error because you are changing the actual reference, not
just what the reference points to. */

variableDog.name = "Ace" // Not an error because name is a variable property.
variableDog.age = 8 // Error because age is a constant property.
variableDog = Dog(name: "Ace", age: 8)
/* The last one is not an error because variableDog is a variable instance and
therefore the actual reference can be changed. */

परीक्षण करें कि क्या दो वस्तुएं समान हैं ( === समान उदाहरण की ओर इंगित करें)

class Dog: Equatable {
    let name: String
    init(name: String) { self.name = name }
}

// Consider two dogs equal if their names are equal.
func ==(lhs: Dog, rhs: Dog) -> Bool {
    return lhs.name == rhs.name
}

// Create two Dog instances which have the same name.
let spot1 = Dog(name: "Spot")
let spot2 = Dog(name: "Spot")

spot1 == spot2   // true, because the dogs are equal
spot1 != spot2   // false

spot1 === spot2  // false, because the dogs are different instances
spot1 !== spot2  // true

गुण और विधियाँ

कक्षाएं उन गुणों को परिभाषित कर सकती हैं जो वर्ग के उदाहरणों का उपयोग कर सकते हैं। इस उदाहरण में, Dog के दो गुण हैं: name और dogYearAge :

class Dog {
    var name = ""
    var dogYearAge = 0
}

आप गुणों को डॉट सिंटैक्स के साथ एक्सेस कर सकते हैं:

let dog = Dog()
print(dog.name)
print(dog.dogYearAge)

कक्षाएं उन तरीकों को भी परिभाषित कर सकती हैं जिन्हें उदाहरणों पर बुलाया जा सकता है, उन्हें सामान्य कार्यों के समान घोषित किया जाता है , बस कक्षा के अंदर:

class Dog {
    func bark() {
        print("Ruff!")
    }
}

कॉलिंग विधियाँ भी डॉट सिंटैक्स का उपयोग करती हैं:

dog.bark()

कक्षाएं और एकाधिक वंशानुक्रम

स्विफ्ट मल्टीपल इनहेरिटेंस का समर्थन नहीं करता है। यही है, आप एक से अधिक वर्ग से वारिस नहीं हो सकते।

class Animal { ... }
class Pet { ... }

class Dog: Animal, Pet { ... } // This will result in a compiler error.

इसके बजाय आपको अपने प्रकार बनाते समय रचना का उपयोग करने के लिए प्रोत्साहित किया जाता है। यह प्रोटोकॉल का उपयोग करके पूरा किया जा सकता है।

deinit

class ClassA {

    var timer: NSTimer!

    init() {
        // initialize timer
    }

    deinit {
        // code
        timer.invalidate()
    }
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow