我的应用程序在运行时卡在启动画面上。它已经有一个storyboard入口点,它指向名为studentsViewController的视图控制器,所以我不知道为什么它不起作用。没有崩溃。 StudentsViewController:
import UIKit
class StudentsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var currentClass = VirtualRewardsClient.sharedInstance.getClass()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(StudentTableViewCell.self, forCellReuseIdentifier: "studentCell")
Class.sharedInstance.addStudent("Dhruv")
Class.sharedInstance.printClass()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Class.sharedInstance.students.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("studentCell") as StudentTableViewCell
return cell
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
VirtualRewardsClient.swift
import UIKit
class VirtualRewardsClient{
class var sharedInstance: VirtualRewardsClient{
struct Static{
static var instance = VirtualRewardsClient()
}
return Static.instance
}
func getClass() -> Class{
if let data = NSUserDefaults.standardUserDefaults().objectForKey(classKey) as? NSData{
let unarc = NSKeyedUnarchiver(forReadingWithData: data)
unarc.setClass(Class.self, forClassName: "Class")
let currentClass = unarc.decodeObjectForKey("root") as Class
Class.sharedInstance.students = currentClass.students
Class.sharedInstance.teacher = currentClass.teacher
return currentClass
}
return Class()
}
}
Class.swift
import Foundation
导入UIKit
让classKey =“CLASS_KEY”
class Class:NSObject { let defaults = NSUserDefaults.standardUserDefaults()
class var sharedInstance: Class{
struct Static{
static var instance: Class = VirtualRewardsClient.sharedInstance.getClass()
}
return Static.instance
}
var students:[Student] = [Student]()
var teacher = Teacher(currentClass: sharedInstance)
func addStudent(name: String, value: Int){
students.append(Student(name: name, startingPoints: value))
defaults.setObject(NSKeyedArchiver.archivedDataWithRootObject(Class.sharedInstance), forKey: classKey)
VirtualRewardsClient.sharedInstance.getClass()
}
func addStudent(name: String){
students.append(Student(name: name))
defaults.setObject(NSKeyedArchiver.archivedDataWithRootObject(Class.sharedInstance), forKey: classKey)
VirtualRewardsClient.sharedInstance.getClass()
}
func printClass(){
for i in students{
println("Student: \(i.name), Points: \(i.points)")
}
}
}