gpt4 book ai didi

ios - 如何将变量分离到其他文件并访问

转载 作者:行者123 更新时间:2023-11-30 12:33:22 24 4
gpt4 key购买 nike

我是 swift 和 IOS 的新手,我不知道将变量(mylessons)分离到其他文件(.swift)或(.json)以实现 MVC。有人可以教我吗?非常感谢

class LessonsTableViewController: UITableViewController {

var mylessons = [["title":"Posture", "subtitle":"Set up your body",
"bgimage":"Ln1", "lesimage":"Posture_pic", "lestitle":"So starting from
the head and moving down:", "lescontent":"1) The top back part of your
head should be pointing up.."], ..]

override func viewDidLoad() {
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}


override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
..
}

最佳答案

引用您给定的代码片段,以实现 MVC ,您应该为 lesson 创建一个模型 - 其中包含所需的属性 - 因此,mylessons 数组应包含 lesson 的对象。 p>

如果您想将不同文件中的层分开(这是一个好主意),您可能需要创建一个文件 - 例如,Lesson.swift - 其中包含您的模型,如下所示:

Lesson.swift:

// Note that I decalred it as 'struct'
// instead of 'class' do what is suitable
// for your requirements...
struct Lesson {
var title:String?
var subtitle: String?
var bgImage: String?
var lesImage: String?
var lesTitle: String?
}

View Controller :

// note that myLessons data type is: '[Lesson]'
var myLessons = [Lesson(title: "Posture",
subtitle: "Set up your body",
bgImage: "Ln1",
lesImage: "Posture_pic",
lesTitle: "So starting from the head and moving down",
lesContent: "1) The top back part of your head should be pointing up..")]

备注:如果您正在实现table​View:​cell​For​Row​At​Index​Path:​,您应该像这样访问每一行的类(class):

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//....

let currentLesson = myLessons[indexPath.row]

// ...
}
<小时/>

另外:

如果您能够将给定的字典转换为类(class)对象,那就太好了,实际上这是可能的,通过添加以下字典扩展:

extension Dictionary where Key: ExpressibleByStringLiteral, Value: ExpressibleByStringLiteral {
func toLesson() -> Lesson {
var lessonToRetrun = Lesson()

if let title = self["title"] as? String {
lessonToRetrun.title = title
}

if let subtitle = self["subtitle"] as? String {
lessonToRetrun.subtitle = subtitle
}

if let bgimage = self["bgimage"] as? String {
lessonToRetrun.bgImage = bgimage
}

if let lesimage = self["lesimage"] as? String {
lessonToRetrun.lesImage = lesimage
}

if let lestitle = self["lestitle"] as? String {
lessonToRetrun.lesTitle = lestitle
}

if let lescontent = self["lescontent"] as? String {
lessonToRetrun.lesContent = lescontent
}

return lessonToRetrun
}
}

你将能够做到:

let lessonDict = ["title":"Posture",
"subtitle":"Set up your body",
"bgimage":"Ln1", "lesimage":"Posture_pic",
"lestitle":"So starting from the head and moving down:",
"lescontent":"1) The top back part of your head should be pointing up.."]

let convertedLesson = lessonDict.toLesson()

print(convertedLesson)
// Lesson(title: Optional("Posture"), subtitle: Optional("Set up your body"), bgImage: Optional("Ln1"), lesImage: Optional("Posture_pic"), lesTitle: Optional("So starting from the head and moving down:"), lesContent: Optional("1) The top back part of your head should be pointing up.."))

但您必须非常确定 key 是相同的。

希望这有帮助。

关于ios - 如何将变量分离到其他文件并访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com