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

时间:2015-02-21 22:10:03

标签: ios swift parse-platform

我的代码:

在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错误。

2 个答案:

答案 0 :(得分:1)

不,你正在做的一切正确,你只是有一个错字。由于错误显示已经说它需要一个分隔符。通常这个错误会产生误导,但在您的情况下,它会直接导致您的问题:只需在参数之间添加,

来自:

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

要:

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

答案 1 :(得分:0)

添加频道:

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 []
  }
}