簡體   English   中英

解析:致命錯誤:NSArray元素與Swift Array元素類型不匹配

[英]Parse: fatal error: NSArray element failed to match the Swift Array Element type

每當我從集合視圖中刪除項目,關閉應用程序,重新啟動應用程序然后返回項目時,都會出現此錯誤(嚴重錯誤: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數組中。 這樣,我可以直接將所有調用都發送到該數組,而不會嘗試降低到嵌套級別而不會丟失任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM