每当我从我的集合视图中删除项目,关闭应用程序,重新启动应用程序,然后返回项目时,我收到此错误(致命错误:NSArray元素无法匹配Swift数组元素类型)。我找到了重新订购初始化程序的类似解决方案,但这似乎并不适合我。
**AppDelegate.swift**
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// register PFObject subclasses
Task.registerSubclass()
Task.initialize()
Project.registerSubclass()
Project.initialize()
User.registerSubclass()
User.initialize()
**Project.swift**
import UIKit
import Parse
public class Project: PFObject, PFSubclassing
{
// MARK: - Public API
@NSManaged public var projectTitle: String!
@NSManaged public var owner: User!
@NSManaged public var tasks: [Task]
// MARK - Required PFSubclassing Parse setup
override public class func initialize()
{
struct Static {
static var onceToken: dispatch_once_t = 0
}
dispatch_once(&Static.onceToken) {
Task.initialize()
self.registerSubclass()
}
}
public static func parseClassName() -> String {
return "Project"
}
// MARK: Conveinience Init
init(projectTitle: String, owner: User, tasks: [Task])
{
super.init()
self.projectTitle = projectTitle
self.owner = owner
self.tasks = tasks
}
override init() { super.init() }
}
**Task.swift**
import UIKit
import Parse
public class Task: PFObject, PFSubclassing
{
// MARK: - Public API
@NSManaged public var title: String
@NSManaged public var isChecked: Bool
@NSManaged public var projectOwner: Project
@NSManaged public var category: String
@NSManaged public var isImportant: Bool
// MARK - Required PFSubclassing Parse setup
override public class func initialize()
{
struct Static {
static var onceToken: dispatch_once_t = 0
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
public static func parseClassName() -> String {
return "Task"
}
// MARK: Conveinience Init
// Create new project
init(title: String, projectOwner: Project, isChecked: Bool, category: String, isImportant: Bool)
{
super.init()
self.title = title
self.projectOwner = projectOwner
self.isChecked = isChecked
self.category = category
self.isImportant = isImportant
}
override init() { super.init() }
}
当我尝试使用tasks
数组时,会发生错误。我将它加载到UICollectionView中。
这里可能是我从Parse获取任务的相关代码:
// Fetch data from Parse
func fetchProject() {
let isCurrentTab = navigationController?.tabBarItem.title
let tasksQuery = PFQuery(className: Task.parseClassName())
tasksQuery.whereKey("category", equalTo: isCurrentTab!)
tasksQuery.whereKey("projectOwner", equalTo: project)
tasksQuery.findObjectsInBackgroundWithBlock { (tasks, error) -> Void in
if error == nil {
self.project.tasks.removeAll()
print(tasks)
for task in tasks! {
print(task)
let task = task as! Task
self.project.tasks.append(task)
}
self.collectionView?.reloadData()
} else {
print("\(error?.localizedDescription)")
}
}
}
我正在使用Xcode 7.1和Parse v1.8.5
非常感谢任何帮助!
答案 0 :(得分:0)
我最终使用指针来访问我的tasks数组,而不是将它嵌套在projects数组中。这样,我可以直接对该数组进行所有调用,而不是通过尝试进入嵌套级别而丢失任何内容。