Cheddar Mac 开源项目教程

Cheddar Mac 开源项目教程

cheddar-macCheddar for Mac项目地址:https://gitcode.com/gh_mirrors/ch/cheddar-mac

1. 项目的目录结构及介绍

cheddar-mac/
├── CheddarKit/
│   ├── Models/
│   ├── Networking/
│   ├── Persistence/
│   ├── Utilities/
│   └── Extensions/
├── CheddarKitTests/
├── CheddarKitUITests/
├── CheddarUI/
│   ├── Controllers/
│   ├── Views/
│   ├── Storyboards/
│   └── Assets/
├── CheddarUITests/
├── Cheddar/
│   ├── AppDelegate.swift
│   ├── Info.plist
│   └── Main.storyboard
├── CheddarTests/
├── CheddarUITests/
├── Carthage/
├── Pods/
├── Cheddar.xcodeproj
└── Cheddar.xcworkspace

目录结构介绍

  • CheddarKit: 包含项目的核心逻辑,如模型、网络请求、持久化存储和工具类。
  • CheddarKitTests: 包含 CheddarKit 的单元测试。
  • CheddarKitUITests: 包含 CheddarKit 的 UI 测试。
  • CheddarUI: 包含项目的 UI 相关代码,如控制器、视图、故事板和资源文件。
  • CheddarUITests: 包含 CheddarUI 的 UI 测试。
  • Cheddar: 包含应用的主入口文件 AppDelegate.swift 和其他应用配置文件。
  • CheddarTests: 包含 Cheddar 的单元测试。
  • CheddarUITests: 包含 Cheddar 的 UI 测试。
  • Carthage: 包含 Carthage 依赖管理工具的文件。
  • Pods: 包含 CocoaPods 依赖管理工具的文件。
  • Cheddar.xcodeproj: Xcode 项目文件。
  • Cheddar.xcworkspace: Xcode 工作区文件。

2. 项目的启动文件介绍

AppDelegate.swift

AppDelegate.swift 是项目的启动文件,负责应用的生命周期管理。以下是文件的主要内容:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 初始化应用配置
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // 应用即将进入非活动状态
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // 应用进入后台
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // 应用即将进入前台
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // 应用变为活动状态
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // 应用即将终止
    }
}

Info.plist

Info.plist 是应用的配置文件,包含应用的基本信息和权限设置。以下是一些常见的配置项:

<key>CFBundleDisplayName</key>
<string>Cheddar</string>
<key>CFBundleIdentifier</key>
<string>com.soffes.Cheddar</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
</array>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>

3. 项目的配置文件介绍

Carthage

Carthage 目录包含通过 Carthage 管理的第三方库。要使用 Carthage,首先需要安装 Carthage 工具,然后在项目根目录下运行以下命令:

carthage update --platform iOS

Pods

Pods 目录包含通过 CocoaPods 管理的第三方库。要使用 CocoaPods,首先需要安装 CocoaPods 工具,然后在项目根目录下运行以下命令:

pod install

Cheddar.xcodeproj

cheddar-macCheddar for Mac项目地址:https://gitcode.com/gh_mirrors/ch/cheddar-mac

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
不使用LINQ查询和操作集合 改进代码 namespace SandwichCalories { class Program { static void Main(string[] args) { // sandwich ingredients and their associated calories Dictionary<string, int> ingredients = new Dictionary<string, int>() { { "Bread", 66 }, { "Ham", 72 }, { "Bologna", 57 }, { "Chicken", 17 }, { "Corned Beef", 53 }, { "Salami", 40 }, { "Cheese, American", 104 }, { "Cheese, Cheddar", 113 }, { "Cheese, Havarti", 105 }, { "Mayonnaise", 94 }, { "Mustard", 10 }, { "Butter", 102 }, { "Garlic Aioli", 100 }, { "Sriracha", 15 }, { "Dressing, Ranch", 73 }, { "Dressing, 1000 Island", 59 }, { "Lettuce", 5 }, { "Tomato", 4 }, { "Cucumber", 4 }, { "Banana Pepper", 10 }, { "Green Pepper", 3 }, { "Red Onion", 6 }, { "Spinach", 7 }, { "Avocado", 64 } }; // prompt user for calorie range Console.Write("Enter minimum calories: "); int min_calories = int.Parse(Console.ReadLine()); Console.Write("Enter maximum calories: "); int max_calories = int.Parse(Console.ReadLine()); // calculate the minimum and maximum calories for the sandwich int min_sandwich_calories = 2 * ingredients["Bread"] + ingredients.Values.Min() * 2; int max_sandwich_calories = 2 * ingredients["Bread"] + ingredients.Values.Max() * 2; // check if the calorie range is valid if (max_calories < min_sandwich_calories) { Console.WriteLine("Sorry, it is impossible to create a sandwich within the given calorie range."); } else { // create the sandwich List<string> sandwich = new List<string> { "Bread" }; int sandwich_calories = 1 * ingredients["Bread"]; while (sandwich_calories < min_calories) { // add random ingredient string ingredient = ingredients.Keys.ElementAt(new Random().Next(ingredients.Count)); sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; } while (sandwich_calories <= max_calories) { // add random ingredient string ingredient = ingredients.Keys.ElementAt(new Random().Next(ingredients.Count)); // check if the ingredient is the same as the previous one if (sandwich.Count >= 3 && ingredient == sandwich[sandwich.Count - 2]) { continue; } sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; // check if the sandwich is already at the maximum calorie limit if (sandwich_calories == max_sandwich_calories) { break; } } // add the last slice of bread sandwich.Add("Bread"); // print the sandwich and its total calories Console.WriteLine("Your sandwich: " + string.Join(", ", sandwich)); Console.WriteLine("Total calories: " + sandwich_calories); } } } }
06-10
以下是不使用LINQ查询和操作集合改进的代码,这里使用了foreach循环和if条件语句来实现相同的功能: ```csharp using System; using System.Collections.Generic; namespace SandwichCalories { class Program { static void Main(string[] args) { // sandwich ingredients and their associated calories Dictionary<string, int> ingredients = new Dictionary<string, int>() { { "Bread", 66 }, { "Ham", 72 }, { "Bologna", 57 }, { "Chicken", 17 }, { "Corned Beef", 53 }, { "Salami", 40 }, { "Cheese, American", 104 }, { "Cheese, Cheddar", 113 }, { "Cheese, Havarti", 105 }, { "Mayonnaise", 94 }, { "Mustard", 10 }, { "Butter", 102 }, { "Garlic Aioli", 100 }, { "Sriracha", 15 }, { "Dressing, Ranch", 73 }, { "Dressing, 1000 Island", 59 }, { "Lettuce", 5 }, { "Tomato", 4 }, { "Cucumber", 4 }, { "Banana Pepper", 10 }, { "Green Pepper", 3 }, { "Red Onion", 6 }, { "Spinach", 7 }, { "Avocado", 64 } }; // prompt user for calorie range Console.Write("Enter minimum calories: "); int min_calories = int.Parse(Console.ReadLine()); Console.Write("Enter maximum calories: "); int max_calories = int.Parse(Console.ReadLine()); // calculate the minimum and maximum calories for the sandwich int min_sandwich_calories = 2 * ingredients["Bread"] + GetMinValue(ingredients.Values) * 2; int max_sandwich_calories = 2 * ingredients["Bread"] + GetMaxValue(ingredients.Values) * 2; // check if the calorie range is valid if (max_calories < min_sandwich_calories) { Console.WriteLine("Sorry, it is impossible to create a sandwich within the given calorie range."); } else { // create the sandwich List<string> sandwich = new List<string> { "Bread" }; int sandwich_calories = 1 * ingredients["Bread"]; while (sandwich_calories < min_calories) { // add random ingredient string ingredient = GetRandomIngredient(ingredients); sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; } while (sandwich_calories <= max_calories) { // add random ingredient string ingredient = GetRandomIngredient(ingredients); // check if the ingredient is the same as the previous one if (sandwich.Count >= 3 && ingredient == sandwich[sandwich.Count - 2]) { continue; } sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; // check if the sandwich is already at the maximum calorie limit if (sandwich_calories == max_sandwich_calories) { break; } } // add the last slice of bread sandwich.Add("Bread"); // print the sandwich and its total calories Console.WriteLine("Your sandwich: " + string.Join(", ", sandwich)); Console.WriteLine("Total calories: " + sandwich_calories); } } // helper methods static int GetMinValue(IEnumerable<int> values) { int minValue = int.MaxValue; foreach (int value in values) { if (value < minValue) { minValue = value; } } return minValue; } static int GetMaxValue(IEnumerable<int> values) { int maxValue = int.MinValue; foreach (int value in values) { if (value > maxValue) { maxValue = value; } } return maxValue; } static string GetRandomIngredient(Dictionary<string, int> ingredients) { int index = new Random().Next(ingredients.Count); int i = 0; foreach (string key in ingredients.Keys) { if (i == index) { return key; } i++; } return null; } } } ``` 在这个改进的代码中,我们使用了GetMinValue和GetMaxValue方法来获取字典中值的最小值和最大值,使用GetRandomIngredient方法来获取随机的食材名称。虽然这种方式代码量增加了,但是这种方式可以更好地理解代码的每个步骤,也可以更好地掌控代码的细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芮逸炯Conqueror

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值