无法将类型“NSMutableDictionary”的值转换为强制类型“[NSObject: AnyObject]”以用于 google ios Analytics

Posted

技术标签:

【中文标题】无法将类型“NSMutableDictionary”的值转换为强制类型“[NSObject: AnyObject]”以用于 google ios Analytics【英文标题】:Cannot convert value of type 'NSMutableDictionary' to type '[NSObject: AnyObject]' in coercion for google ios Analytics 【发布时间】:2015-11-06 07:26:04 【问题描述】:

我想将 google Analytics ios 添加到我的项目中,但是当我尝试将他们的代码粘贴到我的应用中时遇到了一些问题,如下面的代码。

var tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: "rootPlayView")

var builder = GAIDictionaryBuilder.createScreenView()
tracker.send(builder.build() as [NSObject : AnyObject])

然后我遇到了这样的错误

我该如何解决这个问题?

谢谢!

【问题讨论】:

【参考方案1】:

类型转换

斯威夫特 3

屏幕轨迹

let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: "Home")
tracker.send(GAIDictionaryBuilder.createScreenView().build() as [AnyHashable : Any])

Objective-C

id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
// Set the screen name on the tracker so that it is used in all hits sent from this screen.
[tracker set:kGAIScreenName value:@"Home"];
[tracker send:[[GAIDictionaryBuilder createScreenView]  build]]; 

自动屏幕跟踪

斯威夫特 3

override func viewDidAppear(_ animated: Bool) 
    super.viewDidAppear(animated)
    self!.screenName = "About Screen"

Objective-C

- (void)viewDidAppear:(BOOL)animated 
    [super viewDidAppear:animated];
    self.screenName = @"About Screen";

事件追踪

斯威夫特 3

let tracker = GAI.sharedInstance().defaultTracker

tracker!.send(GAIDictionaryBuilder.createEventWithCategory("ui_action", action: "button_press", label: "menuButton", play: nil).build())

Objective-C

id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action"     // Event category (required)
    action:@"button_press"  // Event action (required)
    label:@"play"           
    value:nil] build]];

或使用

weak var tracker = GAI.sharedInstance().defaultTracker
// Set screen name on the tracker to be sent with all hits.
 tracker!.set(kGAIScreenName, value: "Home Screen")
// Send a screen view for "Home Screen".
// [tracker send:[[GAIDictionaryBuilder createAppView] build]];   

// Previous V3 SDK versions.
tracker!.send(GAIDictionaryBuilder.createScreenView().build())

// SDK Version 3.08 and up.
// This event will also be sent with &cd=Home%20Screen.
tracker!.send(GAIDictionaryBuilder.createEventWithCategory("UX", action: "touch", label: "menuButton", value: nil).build())
// Clear the screen name field when we're done.
tracker!.set(kGAIScreenName, value: nil)

更多信息请见this

例子

试试这个

var builder = GAIDictionaryBuilder.createScreenView().build() as! [NSObject : AnyObject]
tracker.send(builder)

用于屏幕轨道

let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIDescription, value: "rootPlayView")
let builder: NSObject = GAIDictionaryBuilder.createScreenView().build()
tracker.send(builder as! [NSObject : AnyObject])

用于事件跟踪

let tracker = GAI.sharedInstance().defaultTracker
let builder: NSObject = GAIDictionaryBuilder.createEventWithCategory(
            "xxxx",
            action: "buttonclicked",
            label: "you pressed xxx button",
            value: nil).build()
tracker.send(builder as! [NSObject : AnyObject])

【讨论】:

对 swift 3 有任何想法吗? tracker 在这种情况下是可选的,需要解包。这对我有用:if let tracker = GAI.sharedInstance().defaultTracker tracker.set(kGAIDescription, value: "rootPlayView") let builder: NSObject = GAIDictionaryBuilder.createScreenView().build() tracker.send(builder as! [NSObject : AnyObject]) @FuadKamal- 你能更新答案吗,对其他人有用 这个答案需要更新。这不是 Swift 3 版本。至少不是当前的。【参考方案2】:

此答案已针对 Swift 3 更新

let tracker = GAI.sharedInstance().defaultTracker
let eventTracker: NSObject = GAIDictionaryBuilder.createEvent( 
                withCategory: "SomeCategory",
                action: "SomeAction",
                label: "SomeLabel",
                value: nil).build()
tracker.send(eventTracker as [NSObject : AnyObject]!)

或者...

let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIDescription, value: "ScreenViewName")
let eventTracker: NSObject = GAIDictionaryBuilder.createScreenView().build()
tracker.send(eventTracker as! [NSObject : AnyObject])

【讨论】:

试了很多方法,你的回答对我有很大帮助,感谢分享。【参考方案3】:

对于 GA 3.17.0 使用:

let tracker = GAI.sharedInstance().defaultTracker
let builder = GAIDictionaryBuilder.createEvent(withCategory: category,
                                                     action: action,
                                                      label: nil,
                                                      value: nil).build()
tracker?.send(builder as [NSObject : AnyObject]!)

注意[NSObject: AnyObject]!末尾的!

【讨论】:

【参考方案4】:

小小跟进惊人的答案,在swift 3中,方法是

let builder:NSObject = GAIDictionaryBuilder.createEvent(withCategory: category, action: eventName, label: label, value: nil).build()

【讨论】:

以上是关于无法将类型“NSMutableDictionary”的值转换为强制类型“[NSObject: AnyObject]”以用于 google ios Analytics的主要内容,如果未能解决你的问题,请参考以下文章

无法将“Swift._SwiftDeferredNSDictionary<Swift.String, Swift.String>”类型的值转换为“NSMutableDictionary”

将 C 类型值设置为 NSMutableDictionary

iOS 不兼容的指针类型(来自 NSDictionary 的 NSMutableDictionary)

在 NSMutableDictionary 中设置 NSURLSessionDownloadTask

无法更新 NSMutableDictionary?

单例、NSMutableDictionary 和 plist