【问题标题】:Swift 2.0 issue showing notification with custom sound fileSwift 2.0 问题显示带有自定义声音文件的通知
【发布时间】:2015-12-14 20:25:57
【问题描述】:

当用户收到消息时,我正在尝试让我的应用发送带有自定义声音文件的通知,但 var pushData:NSDictionary = NSDictionary(dictionaryLiteral: "New Message "+myName,"alert","KeepNotification.wav","sound") 中出现错误 Xcode 说:无法使用类型为 '(dictionaryLiteral: String, String, String, String)' 的参数列表调用类型 'NSDictionary' 的初始化程序

我不知道这意味着什么,所以如果有人向我展示了正确的代码行,我将不胜感激。

if success == true {

                //This to push notification when message is sent successful with sound ********

                var myName = PFUser.currentUser().valueForKey("profileName") as! String

                var pushData:NSDictionary = NSDictionary(dictionaryLiteral: "New Message "+myName,"alert","KeepNotification.wav","sound")

                var uQuery:PFQuery = PFUser.query()
                uQuery.whereKey("username", equalTo: otherName)

                var pushQuery:PFQuery = PFInstallation.query()
                pushQuery.whereKey("user", matchesQuery: uQuery)

                var push:PFPush = PFPush()
                push.setQuery(pushQuery)



                push.setData(pushData as [NSObject : AnyObject])
                push.sendPushInBackgroundWithBlock{

                    (success:Bool! , error:NSError!) -> Void in

                }

                print("Push Sent")
                //-----------------------------------------------------------------
                print("Message Sent" + myName)
                self.messageTextView.text = ""
                self.mLabel.hidden = false
                self.refreshResullts()


            }

        }

    }

}

谢谢,如果有任何不清楚的地方,我会编辑我的问题。

【问题讨论】:

  • 注意写“if success { ... }”就足够了
  • 请使用更具描述性的问题标题。

标签: ios swift parse-platform


【解决方案1】:

如果您尝试初始化NSDictionary,那么您可以使用:

var pushData = ["alert" : "New Message "+ myName, "sound" : "KeepNotification.wav"]

请参阅 NSDictionary Class Reference 以了解可用于 NSDictionary 的所有初始化程序类型及其用法。

【讨论】:

  • NSDictionary?这是斯威夫特。只需使用var pushData = [ "alert" : "New message " + myName, "sound" : "KeepNotification.wav"]
  • @MidhunMP 不,我认为他说考虑到唯一使用pushData as [NSObject : AnyObject],不需要使用NSDictionary
  • syntax 是 Swift,但使用 Swift Dictionary 而不是 NSDictionary 更习惯用法(并且在类型安全方面更安全)。
  • @NRitH: 同意:)
  • @Sulthan:是的,同意你的看法。感谢您通知那部分代码。
猜你喜欢
  • 1970-01-01
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多