Skip to content

Commit

Permalink
#23-Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
allbertoMD committed Jun 26, 2024
1 parent 753bf90 commit ea27d69
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions Roadmap/23 - SINGLETON/swift/allbertoMD.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import Foundation



class Singleton {
// Propiedad estática que contiene la única instancia de la clase
static let shared = Singleton()

// Inicializador privado para prevenir la creación de otras instancias
private init() {}

// Métodos y propiedades de la clase
func sayHi(to name: String) {
print("Hi, \(name)")
}
}

// Acceso global desde cualquier parte del código
let instance1 = Singleton.shared
instance1.sayHi(to: "Charly")

let instance2 = Singleton.shared
instance2.sayHi(to: "Marta")

// Verificar que ambas referencias apuntan a la misma instancia
print(instance1 === instance2) // true




// DIFICULTAD EXTRA
print("\nDificultad Extra.\n")


class UserSession {
static let shared = UserSession()

private init() {}

var id: String = ""
var userName: String = ""
var name: String = ""
var email: String = ""

func assigntID(_ id: String) {
self.id = id
}

func assigntUserName(_ userName: String) {
self.userName = userName
}

func assigntName(_ name: String) {
self.name = name
}

func assignEmail(_ email: String) {
self.email = email
}

func showSessionInfo() {
print("id: \(id)")
print("User Name: \(userName)")
print("Name: \(name)")
print("email: \(email)")
}

func removeSession() {
id = ""
userName = ""
name = ""
email = ""
print("\nSession Removed")
}
}


let userSession = UserSession.shared

userSession.assigntID(UUID().uuidString)
userSession.assigntUserName("Teophilus")
userSession.assigntName("Pablo")
userSession.assignEmail("[email protected]")

userSession.showSessionInfo()
userSession.removeSession()




0 comments on commit ea27d69

Please sign in to comment.