每当我从集合视图中删除项目,关闭应用程序,重新启动应用程序然后返回项目时,都会出现此错误(严重错误:NSArray元素与Swift Array元素类型不匹配)。我发现类似的解决方案可以对初始化程序进行重新排序,但对我来说似乎并不满意。
**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
任何帮助将非常感激!
最佳答案
我最终使用指针访问任务数组,而不是将其嵌套在projects数组中。这样,我可以直接对该数组进行所有调用,而不会尝试降级到嵌套级别而不会丢失任何内容。
关于ios - 解析:致命错误:NSArray元素与Swift Array元素类型不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32871057/