【问题标题】:addUniqueObject property causes compiler error in xcodeaddUniqueObject 属性导致 xcode 中的编译器错误
【发布时间】:2015-02-21 22:10:02
【问题描述】:

我的代码:

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

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    不,你做的一切都是正确的,你只是有一个错字。正如错误所示,它已经表明它需要一个分隔符。此错误通常具有误导性,但在您的情况下,它会直接导致您的问题:只需在参数之间添加 ,

    发件人:

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

    收件人:

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

    【讨论】:

    • 那是我第一次看到实际上结果是这样的错误。一直都是别的东西。 Parse 的教程 (parse.com/docs/push_guide#top/iOS) 不包含逗号,所以我认为还有更多内容。谢谢!
    【解决方案2】:

    添加频道:

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

    【讨论】:

      猜你喜欢
      • 2017-09-21
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      相关资源
      最近更新 更多