No tienes acceso a esta clase

隆Contin煤a aprendiendo! 脷nete y comienza a potenciar tu carrera

Sub铆ndices

14/27
Recursos

Aportes 26

Preguntas 0

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad?

o inicia sesi贸n.

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

Aqu铆 mi propuesta de soluci贸n:

enum Planet: Int, Error {
    case mercury = 1, venus, earth, mars, jupiter, saturn,uranus,neptune
    static subscript(n: Int) -> Planet{
        guard let planet = Planet(rawValue: n) else {
            print("El planeta no existe. Pero te devuelvo mi favorito 馃榿")
            return Planet.earth
        }
        return planet
    }
}


let mars = Planet[4]
let somePlanet = Planet[56]

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)

enum Planet : Int , CaseIterable {
    case mercury = 1, venus,earth,mars,jupiter,saturn,uranos,neptune,notPlanet
    
    
    static subscript(n: Int) -> Planet {
        
        let countPlanet = Planet.allCases.count
        guard n <= countPlanet && n > 0  else {
            return Planet(rawValue: 9)!
        }
        return Planet(rawValue: n)!
    }
}

var mars = Planet[-400]
mars = Planet[2]

enum Planet: Int {
    
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune, error
    static subscript(n: Int) -> Planet {

        guard let planet = Planet(rawValue: n) else { return .error }
        return planet
    }
}

let planet = Planet[4]
print(planet)

I add another variable call 鈥淓RROR鈥 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

Reto:


enum Planet:Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
        static subscript (n: Int) -> Planet{
            if n >= 1 && n <= 8 {
                return Planet(rawValue: n) ?? mercury
            }else{
                print("This is a invalid number, the default planet is mercury")
                return Planet(rawValue: 1)!
            }
        }
}

let mars = Planet[4]

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)
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
    }