SwiftUI与UIKit:开发对比与实战应用
1. SwiftUI在watchOS上的应用
在开发过程中,我们首先要创建一个名为 WatchHelper.swift 的新类,其代码如下:
class WatchHelper: Any {
// Add Recipe
static func addRecipe(recipeString: String) -> [RecipeModel] {
// Remaing code here...
}
static func getRecipes() -> [RecipeModel] {
// Remaing code here...
}
}
addRecipe 函数会将JSON数据转换为 RecipeModel 实例,并将其持久化到手表的 UserDefaults 中,方便后续获取。而 getRecipes 函数则从手表中提取已保存的食谱列表,并将其作为 RecipeModel 数组返回。
接下来,我们要对项目进行构建。按下 command + B 对两个目标进行构建,如果一切顺利,项目和每个目标都应该能成功构建。之后,就可以启动模拟器来查看成果了。
超级会员免费看
订阅专栏 解锁全文
1345

被折叠的 条评论
为什么被折叠?



