我是swift和IOS的新手,我不知道将变量(mylessons)与其他文件(.swift)或(.json)分开来实现MVC。谁能教我?非常感谢
declare @outputtable table (start datetime, stop datetime)
declare @curstart datetime, @curstop datetime, @curdiff int
declare @outputstart datetime
DECLARE cur CURSOR FOR
select start, stop, datediff(ss, stop, LEAD(start) over (order by rn))
from @t
OPEN cur
FETCH NEXT FROM cur
INTO @curstart, @curstop, @curdiff
WHILE @@FETCH_STATUS = 0
BEGIN
if (@outputstart is null)
set @outputstart = @curstart
if (@curdiff > 25)
begin
insert into @outputtable values (@outputstart, @curstop)
set @outputstart = null
end
FETCH NEXT FROM cur
INTO @curstart, @curstop, @curdiff
END
CLOSE cur;
DEALLOCATE cur;
insert into @outputtable values (@outputstart, @curstop)
select * from @outputtable
答案 0 :(得分:0)
参考您给出的代码段,为了实现MVC,您应该为课程创建一个包含所需属性的模型,因此,mylessons
数组应该包含课程的对象。
如果要分隔不同文件中的图层(这是一个好主意),您可能需要创建一个文件 - 例如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?
}
<强>的ViewController:强>
// 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..")]
备注:如果您正在实施tableView:cellForRowAtIndexPath:
,则应该访问每行的课程,如下所示:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//....
let currentLesson = myLessons[indexPath.row]
// ...
}
同时强>
如果您能够将给定的字典转换为课程对象,实际上可以通过添加以下字典扩展来实现它将会很好:
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.."))
但您必须非常确定 keys
是完全相同的。
希望这会有所帮助。