【问题标题】:Cannot convert value of type [AnyObject] to expected argument type [PFObject] -- Parse & Swift II Error无法将 [AnyObject] 类型的值转换为预期的参数类型 [PFObject] -- Parse & Swift II 错误
【发布时间】:2015-12-08 21:25:59
【问题描述】:

这是失败的行,带有错误消息“无法将 [AnyObject] 类型的值转换为预期的参数类型 [PFObject]”

self.customObtainAllHandlerWithObjects(对象,成功:成功,失败:失败)


导入 UIKit 导入解析

类 AbstractService: BaseService {

private let _cache = AbstractCache<T>()
private let _parser = AbstractParser<T>()

/// Store all completionHandlers for convertFromParseObject operations here. Need this to avoid a concurrent conversions of the same object.
var conversions = Dictionary<String, [(entity: T) -> Void]>()

/// Contains ids of objects, which need their videos to be downloaded.
var queue: [(entityId: String, file: PFFile)] = []
var isQueueDownloading = false

var className: String {
    get {
        fatalError("This property must be overridden")
    }
}

// MARK: - Create

func createEntity() -> T {
    let entity = T()
    cache().appendEntity(entity)
    return entity
}

// MARK: - Obtain all

/// Base method, which obtains entities from Parse database.
///
/// - parameter skip: Number of records, which will be skipped.
/// - parameter limit: Max number of entities returned.
/// - parameter constraints: A block, which applies constraints to PFQuery. E.g. query.includeKey("author") or query.whereKey("user", equalTo: PFUser.currentUser()).
/// - parameter success: Success block. Executes when operation successfully finished.
/// - parameter failure: Failure block. Executes when operation fails.

func obtain(skip skip: Int?, limit: Int?, constraints applyConstraints: ((query: PFQuery) -> PFQuery)?, success: (entities: [T]) -> Void, failure: FailureCompletionHandler) {
    var query = PFQuery(className: className)
    if let skip = skip {
        query.skip = skip
    }
    query.limit = limit ?? 1000
    if let applyConstraints = applyConstraints {
        query = applyConstraints(query: query)
    }
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if let error = error {
            self.callFailureCompletionHandler(failure, error: error)
        }
        else {
            if let objects = objects {
                self.customObtainAllHandlerWithObjects(objects, success: success, failure: failure)
            }
            else {
                failure(allowRetry: false, errorMessage: "Cannot load \(self.className)")
            }
        }
    }
}

【问题讨论】:

  • 请不要使用屏幕截图来展示您的代码:如果想要验证它,好人必须重新输入它。此外,它很难阅读。请添加minimal, complete and verifiable example。您可能想阅读How do I ask a good question,它大大提高了获得有用答案的可能性。
  • 操作,抱歉。浏览入门指南应该做得更好。试图解决这个问题非常沮丧,并正在寻找一个快速的解决方案。

标签: swift parse-platform swift2


【解决方案1】:

您收到此错误是因为 object 属于 [AnyObject] 类型,但您正在调用的函数--customObtainHandler--期待 PFObject 所以你必须将你的 object 转换为 PFObject

您可以使用 as 关键字在类型之间进行转换。

所以你做这样的事情:

if let myVar as? PFObject {
    // now myVar is a PFObject
}

我不建议这样做,但您也可以使用以下方式强制转换它:

myVar as! PFObject

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    相关资源
    最近更新 更多