如何更改属性中的结构

问题描述 投票:0回答:1

我正在尝试制作一个服装推荐应用程序,但遇到了问题。我有一个名为衣柜的属性,我想在其中存储与用户选择的活动类型相关的所有衣物。

我的方法如下:

func recommendClothing(for conditions: WeatherForSelection, type: ActivityType){
        temperature = conditions.feelsLike ?? 0
        isWindy = conditions.windSpeed ?? 0 > 5
        
        let wardrobe = CyclingClothing.self
    
        
        clothing = [
            Clothing(title: "Head", description: wardrobe.Head.forTemperature(temperature).name, image: wardrobe.Head.forTemperature(temperature).image),
            Clothing(title: "Base Layer", description: wardrobe.BaseLayer.forTemperature(temperature).name, image: wardrobe.BaseLayer.forTemperature(temperature).image),
            Clothing(title: "Mid Layer", description: wardrobe.MidLayer.forTemperature(temperature).name, image: wardrobe.MidLayer.forTemperature(temperature).image),
            Clothing(title: "Legs", description: wardrobe.Legs.forTemperature(temperature).name, image: wardrobe.Legs.forTemperature(temperature).image),
            Clothing(title: "Hands", description: wardrobe.Hands.forTemperature(temperature).name, image: wardrobe.Hands.forTemperature(temperature).image),
            Clothing(title: "Feet", description: wardrobe.Feet.forTemperature(temperature).name, image: wardrobe.Feet.forTemperature(temperature).image)
            
        ]
        
        if wardrobe.Shell.forTemperature(temperature) != .none {
            clothing.append(Clothing(title: "Shell", description: wardrobe.Shell.forTemperature(temperature).name, image: wardrobe.Shell.forTemperature(temperature).image))
        }
        
        if isWindy {
            clothing.append(Clothing(title: "Wind Protection", description: "Windproof Layer", image: "windproof"))
        }
        if conditions.isRaining ?? false {
            clothing.append(Clothing(title: "Rain Protection", description: "Waterproof Layer", image: "raincoat"))
        }
        
    }

我想,我只需打开输入type并根据它选择衣柜即可。但接下来我必须定义衣柜的类型,它总是不同的。所以我尝试将 Clothing 结构放入一个名为 Wardrobe 的主结构中,并仅选择衣柜的子结构,但这对我来说也不起作用......我也尝试了一些协议和扩展的恶作剧,但自从我'我对此还很陌生,它也不起作用......

类可以继承工作吗?

有没有办法让它工作或者我必须一起改变逻辑?

服装结构示例如下所示:

struct CyclingClothing {
    
    //MARK: - Head
    enum Head {
        case helmet, headBand, lightCap, winterCap
        
        static func forTemperature(_ temperature: Double) -> Head {
            switch temperature {
            case ..<5:
                    .winterCap
            case 5..<10:
                    .lightCap
            case 10..<15:
                    .headBand
            case 15..<30:
                    .helmet
            default:
                    .helmet
            }
        }
        
        var name: String {
            switch self {
            case .helmet:
                "Just a Helmet"
            case .headBand:
                "Headband"
            case .lightCap:
                "Skullcap"
            case .winterCap:
                "Winter Cap"
            }
        }
        
        var image: String {
            switch self {
                
            case .helmet:
                "helmet"
            case .headBand:
                "headband"
            case .lightCap:
                "light_cap"
            case .winterCap:
                "winter_cap"
            }
        }
    }
    
    //MARK: - Base Layer
    enum BaseLayer {
        case none, lightBase, thermalBase
        
        static func forTemperature(_ temperature: Double) -> BaseLayer {
            switch temperature {
            case ..<10:
                    .thermalBase
            case 10..<20:
                    .lightBase
            default:
                    .none
            }
        }
        
        var name: String {
            switch self {
            case .none:
                "No Base Layer"
            case .lightBase:
                "Light"
            case .thermalBase:
                "Thermal"
            }
        }
        
        var image: String {
            switch self {
            case .none:
                "body"
            case .lightBase:
                "light_base"
            case .thermalBase:
                "thermal_base"
            }
        }
    }
    
    //MARK: - Mid Layer
    enum MidLayer {
        case jersey, longJersey, thermalJersey
        
        static func forTemperature(_ temperature: Double) -> MidLayer {
            switch temperature {
            case ..<10:
                    .thermalJersey
            case 10..<20:
                    .longJersey
            default:
                    .jersey
            }
        }
        
        var name: String {
            switch self {
            case .jersey:
                "Jersey"
            case .longJersey:
                "Long Sleeved Jersey"
            case .thermalJersey:
                "Thermal Jersey"
            }
        }
        
        var image: String{
            switch self {
            case .jersey:
                "jersey"
            case .longJersey:
                "long_jersey"
            case .thermalJersey:
                "thermal_jersey"
            }
        }
    }
    
    //MARK: - Shell
    enum Shell {
        case none, lightJacket, insulatedJacket
        
        static func forTemperature(_ temperature: Double) -> Shell {
            switch temperature {
            case ..<5:
                    .insulatedJacket
            case 5..<15:
                    .lightJacket
            default:
                    .none
            }
        }
        
        var name: String {
            switch self {
            case .none:
                "No Shell"
            case .lightJacket:
                "Light Jacket"
            case .insulatedJacket:
                "Insulated Jacket"
            }
        }
        
        var image: String {
            switch self {
            case .none:
                "no_shell"
            case .lightJacket:
                "light_jacket"
            case .insulatedJacket:
                "insulated_jacket"
            }
        }
        
    }
    
    //MARK: - Legs
    enum Legs {
        case shorts, longBibs, thermalBibs
        
        static func forTemperature(_ temperature: Double) -> Legs {
            switch temperature {
            case ..<10:
                    .thermalBibs
            case 10..<18:
                    .longBibs
            default:
                    .shorts
            }
        }
        
        var name: String {
            switch self {
            case .shorts:
                "Bib Shorts"
            case .longBibs:
                "Long Bibs"
            case .thermalBibs:
                "Thermal Bibs"
            }
        }
        
        var image: String {
            switch self {
            case .shorts:
                "bib_shorts"
            case .longBibs:
                "long_bibs"
            case .thermalBibs:
                "thermal_bibs"
            }
        }
    }
    
    //MARK: - Hands
    enum Hands {
        case longGloves, insulatedGloves, summerGloves
        
        static func forTemperature(_ temperature: Double) -> Hands {
            switch temperature {
            case ..<10:
                    .insulatedGloves
            case 10..<15:
                    .longGloves
            default:
                    .summerGloves
            }
        }
        
        var name: String {
            switch self {
            case .summerGloves:
                "Summer Gloves"
            case .longGloves:
                "Long Gloves"
            case .insulatedGloves:
                "Insulated Gloves"
            }
        }
        var image: String {
            switch self {
            case .summerGloves:
                "gloves"
            case .longGloves:
                "long_gloves"
            case .insulatedGloves:
                "insulated_gloves"
            }
        }
    }
    
    //MARK: - Feet
    enum Feet {
        case summerSocks, warmSocks, shoeCovers, winterShoes
        
        static func forTemperature(_ temperature: Double) -> Feet {
            switch temperature {
            case ..<3:
                return .winterShoes
            case 3..<7:
                return .shoeCovers
            case 7..<15:
                return .warmSocks
            default:
                return .summerSocks
            }
        }
        
        var name: String {
            switch self {
            case .summerSocks:
                "Light Socks"
            case .warmSocks:
                "Warm Socks"
            case .shoeCovers:
                "Shoe Covers"
            case .winterShoes:
                "Winter Shoes"
            }
        }
        
        var image: String {
            switch self {
            case .summerSocks:
                "socks"
            case .warmSocks:
                "warm_socks"
            case .shoeCovers:
                "covers"
            case .winterShoes:
                "winter_shoes"
            }
        }
    }
}



swift struct
1个回答
0
投票

您的代码中可能还有其他问题,但要修复

wardrobe
,我建议您将
CyclingClothing
struct
更改为
enum

然后从函数中删除

wardrobe
常量,并将其所有使用替换为
CyclingClothing

比如说

Clothing(title: "Head", 
         description: CyclingClothing.Head.forTemperature(temperature).name, 
         image: CyclingClothing.Head.forTemperature(temperature).image)
        
© www.soinside.com 2019 - 2024. All rights reserved.