No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Subíndices

14/27
Recursos

Aportes 22

Preguntas 0

Ordenar por:

Los aportes, preguntas y respuestas son vitales para aprender en comunidad. Regístrate o inicia sesión para participar.

enum Planet: Int, CaseIterable {
    
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    
    static subscript (n: Int) -> Planet {
        let myDefault = 1
        let count = Planet.allCases.count
        if n <= count && n > 0 {
            
            if let myPlanet = Planet(rawValue: n) {
                return myPlanet
            }
            
        }
        return Planet(rawValue: myDefault)!
    }
}

let marte = Planet[4]
let defaultPlanet = Planet[-4] //default```

Lo hice así para manejar casos invalidos

enum Planet : Int {
    case nonValid = 0, mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    static subscript(n: Int) -> Planet{
        //return Planet(rawValue: n)!
        if let planet = Planet(rawValue: n){
            return planet
        }else{
            return Planet(rawValue: 0)!
        }
    }
}



let planet = Planet[10]
planet

Mi aporte:

// With enums
enum Planet : Int, CaseIterable {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    // Depends of the enum
    static subscript(n: Int) -> Planet {
        let defaultValue = 1
        let totalPlanets = Planet.allCases.count
        // Subscripts cannot return a nil
        if let myPlanet = Planet(rawValue: n) {
            if n <= totalPlanets && n > 0 {
                return myPlanet
            }
        }
        return Planet(rawValue: defaultValue)!
    }
}

let giveMeSomePlanet = Planet[-50]
print(giveMeSomePlanet)

Yo he hecho uso de guard let para comprobar casos no válidos:

enum Planet: Int {
    case notValid = -1, mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    
    static subscript(n: Int) -> Planet {
        guard let planet = Planet(rawValue: n) else {
            return Planet(rawValue: -1)!
        }
 
        return planet
    }
}

Mi aporte

import Foundation

enum Planet : Int{
    case mercury = 1, venus, earth, mars, jupiter, saturn, urane, neptune, error = 404
    static subscript (n : Int) -> Planet{
      return Planet(rawValue: n) ?? Planet.error
    }
}
var mars = Planet[-15]
print(mars)
mars = Planet[3]
print(mars)
mars = Planet[7]
print(mars)

I add another variable call “ERROR” and assign the number 100 to avoid a user error 😶

enum planet : Int{
    case mercury = 1, venus, earth, mars, jupiter, saturn, urane, neptune, ERROR = 100
    static subscript (n : Int) -> planet{
        if n > 0 && n < 8{
            return planet(rawValue: n)!
        }else{
            return planet.ERROR
        }
    }
}
let mars = planet[-15]
mars
enum Planet : Int {
	case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune	
	static subscript(index: Int) -> Planet?{
        guard 
            let planet = Planet(rawValue: index)
        else{
            return nil;
        }
		return Planet(rawValue: index);
	}
}

if let mars = Planet[16]{
    print(mars);
}

Los enumerados tienen que ser declarados con el tipo de dato, para poder inicializarlo con el respectivo rawValue

enum Planet: Int {
    case noPlanet, mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    static subscript(n: Int) -> Planet {
        if n <= 9 && n > 0 {
            return Planet(rawValue: n)!
        } else {
            return Planet(rawValue: 0)!
        }
    }
}

let mars = Planet[18] // noPlanet
mars
enum Planet : Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    static subscript(n: Int) -> Planet{
        guard let _ = Planet(rawValue: n)
        else{
            print("Fuera de rango, mercury defecto")
            return Planet(rawValue: 1)!
    }
    return Planet(rawValue: n)!
}
}

let mars = Planet[-7]
mars
enum Planet: Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn,
         uranus, neptune
    static subscript(n: Int) -> Planet? {
        return Planet(rawValue:  n)
    }
}

if let mars = Planet[3] {
    print("EL planeta es \(mars)")
} else {
    print("El planeta no existe")
}

Tambien se pueden definir de la siguiente manera

subscript(index: Int) -> Int {
    get {
        // Return an appropriate subscript value here.
    }
    set(newValue) {
        // Perform a suitable setting action here.
    }
}
enum Planet: Int{
    case Planet0 = 0, mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
    static subscript(n: Int) -> Planet{
        let defaultN = 0
        if n < 10, n > 1 {
            return Planet(rawValue: n)!
        }else{
            return Planet(rawValue: defaultN)!
        }
    }
}

let mars = Planet[-14]
mars```
enum Planet: Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    
    static subscript(n: Int) -> Planet{
        guard let _ = Planet(rawValue: n) else{
            print("Fuera de rango, mercury por defecto")
            return Planet(rawValue: 1)!
        }
        return Planet(rawValue: n)!
    }
}

let mars = Planet[-10]
enum Planet : Int{
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    static subscript(n : Int) -> Planet?{
        return (n<0 && n>9) ? Planet(rawValue: n) : nil
    }
}

let pluto = Planet(rawValue: 9)
pluto // nil
enum Planet : Int {
    case nonPlanet = 0, mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
    static subscript(n: Int) -> Planet{
        if n <= 0 || n > 8 {
            return Planet(rawValue: 0)!
        }
        return Planet(rawValue: n)!
    }
}

let jupiter = Planet[-1]
let uranus = Planet [10]
enum Planets: Int{
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune, outofgalaxy = 0
    static subscript(n: Int) -> Planets{
        return (n < self.mercury.rawValue && n > self.neptune.rawValue) ? Planets(rawValue: n)! : Planets.outofgalaxy
    }
}

Lo único que hice fue poner un caso 0 y al momento de regresar hice la comprobación

enum Planet: Int{
    case unavalaible, mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    
    static subscript(n: Int) -> Planet{
        return Planet(rawValue: n) ?? Planet(rawValue: 0)!
    }
}
enum Planet:Int{
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    
    static subscript(n : Int) -> Planet{
        if let p = Planet(rawValue: n){
            return p
        }
        return Planet(rawValue: 1)!
    }
}

let planet = Planet[6]
let defaultPlanet = Planet[2349]
enum Planet : Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    
    static subscript(n: Int) -> Planet {
        if let myPlanet = Planet(rawValue: n) {
                        return myPlanet
        }
        return Planet(rawValue: 1)!
        
    }
}
static let defaultIndex = mercury.rawValue
    
    static subscript (index: Int) -> Planet {
        guard let planet = Planet(rawValue: index) else {
            return Planet(rawValue: Planet.defaultIndex)!
        }
        
        return planet
    }