পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Wednesday, February 8, 2017

Swift - Classes

Classes in Swift are building blocks of flexible constructs. Similar to constants, variables and functions the user can define class properties and methods. Swift provides us the functionality that while declaring classes the users need not create interfaces or implementation files.
Swift allows us to create classes as a single file and the external interfaces will be created by default once the classes are initialized.

Benefits of having Classes

  • Inheritance acquires the properties of one class to another class
  • Type casting enables the user to check class type at run time
  • Deinitializers take care of releasing memory resources
  • Reference counting allows the class instance to have more than one reference

Common Characteristics of Classes and structures

  • Properties are defined to store values
  • Subscripts are defined for providing access to values
  • Methods are initialized to improve functionality
  • Initial state are defined by initializers
  • Functionality are expanded beyond default values
  • Confirming protocol functionality standards

Syntax

Class classname {
   Definition 1
   Definition 2
    --- 
   Definition N
}

Class Definition

class student {
   var studname: String
   var mark: Int 
   var mark2: Int 
}
The syntax for creating instances
let studrecord = student()

Example

class MarksStruct {
   var mark: Int
   init(mark: Int) {
      self.mark = mark
   }
}

class studentMarks {
   var mark = 300
}
let marks = studentMarks()
println("Mark is \(marks.mark)")
When we run the above program using playground, we get the following result −
Mark is 300

Accessing Class Properties as Reference Types

Class properties can be accessed by the '.' syntax. Property name is separated by a '.' after the instance name.
class MarksStruct {
   var mark: Int
   init(mark: Int) {
      self.mark = mark
   }
}

class studentMarks {
   var mark1 = 300
   var mark2 = 400
   var mark3 = 900
}
let marks = studentMarks()
println("Mark1 is \(marks.mark1)")
println("Mark2 is \(marks.mark2)")
println("Mark3 is \(marks.mark3)")
When we run the above program using playground, we get the following result −
Mark1 is 300
Mark2 is 400
Mark3 is 900

Class Identity Operators

Classes in Swift refers multiple constants and variables pointing to a single instance. To know about the constants and variables pointing to a particular class instance identity operators are used. Class instances are always passed by reference. In Classes NSString, NSArray, and NSDictionary instances are always assigned and passed around as a reference to an existing instance, rather than as a copy.
Identical to Operators Not Identical to Operators
Operator used is (===) Operator used is (!==)
Returns true when two constants or variables pointing to a same instance Returns true when two constants or variables pointing to a different instance
class SampleClass: Equatable {
   let myProperty: String
   init(s: String) {
      myProperty = s
   }
}
func ==(lhs: SampleClass, rhs: SampleClass) -> Bool {
   return lhs.myProperty == rhs.myProperty
}

let spClass1 = SampleClass(s: "Hello")
let spClass2 = SampleClass(s: "Hello")

spClass1 === spClass2 // false
println("\(spClass1)")

spClass1 !== spClass2 // true
println("\(spClass2)")
When we run the above program using playground, we get the following result −
main.SampleClass
main.SampleClass

No comments:

Post a Comment