addUniqueObject 属性 导致 xcode 中的编译器错误

addUniqueObject property causes compiler error in xcode

我的代码:

在 didFinishLaunchingWithOptions 中:

//Parse Remote Push Notification setup
let userNotificationTypes = (UIUserNotificationType.Alert |
    UIUserNotificationType.Badge |
    UIUserNotificationType.Sound);

let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

初始通道设置函数:

//Parse push remote necessary functions 
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.addUniqueObject("riders" forKey: "channels")
installation.save()
}

我在这一行收到一条错误消息:

installation.addUniqueObject("riders" forKey: "channels")

错误:需要分隔符

我看到另一个堆栈问题说我应该先检查 nil :

Unable to save channels to PFInstallation (iOS)

但是:

(1) 答案在 objective-C 中,我不知道如何将其翻译成 Swift:

if (currentInstallation.channels == nil)
{
    currentInstallation.channels = [[NSArray alloc] init];
}

(2) 我想知道这是否是我唯一需要做的事情,或者这是否是解决此问题的最佳方法?显然这是一个已知的 Parse SDK 错误。

不,你做的一切都是对的,你只是打错了字。正如错误显示的那样,它已经表明它需要一个分隔符。通常这个错误是误导性的,但在你的情况下,它会直接导致你的问题:只需在你的参数之间添加一个 ,:

发件人:

installation.addUniqueObject("riders" forKey: "channels")

收件人:

installation.addUniqueObject("riders", forKey: "channels")

添加频道:

currentInstallation.addUniqueObject("channel-name", forKey:"channels")
currentInstallation.saveInBackground()

删除频道:

currentInstallation.removeObject("channel-name", forKey:"channels")
currentInstallation.saveInBackground()

获取当前频道:

if let channels: [String] = currentInstallation.channels as? [String] {
  // Process list of channels. 
}

当然,要获取当前安装:

let currentInstallation: PFInstallation = PFInstallation.currentInstallation()

请参阅下面的使用示例:

import Foundation
import Parse

class DWWatchlistController {

  private let currentInstallation: PFInstallation
  private let stocks:[SNStock]
  private(set) var watchlist:[SNStock]

  init(stocks:[SNStock]) {
    self.stocks = stocks
    self.currentInstallation = PFInstallation.currentInstallation()
    self.watchlist = []
    updateWatchlist()
  }

  func addStock(stock: SNStock) {
    currentInstallation.addUniqueObject(stock.ticker, forKey:"channels")
    currentInstallation.saveInBackground()
    updateWatchlist()
  }

  func removeStock(stock: SNStock) {
    currentInstallation.removeObject(stock.ticker, forKey:"channels")
    currentInstallation.saveInBackground()
    updateWatchlist()
  }

  private func updateWatchlist() {
    watchlist = fetchSubscribedStocks()
  }

  private func fetchSubscribedStocks() -> [SNStock] {
    if let channels: [String] = currentInstallation.channels as? [String] {
      return stocks.filter({ (stock: SNStock) -> Bool in
        return contains(channels, stock.ticker as String)
      })
    }
    return []
  }
}