Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

iphone - Cannot convert value of type 'NSMutableDictionary' to type '[NSObject: AnyObject]' in coercion for google ios Analytics

I want to add google Analytics ios into my project but i got some problems when i tried to paste their code into my app like the code below.

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

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

Then i got an error like this

enter image description here

how can i fix this?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

type conversion

type conversion table

Swift 3

Screen Track

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]]; 

Automatic Screen Tracking

Swift 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";
}

event Track

Swift 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]];

or use

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)

for more information see this

example

try this

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

for screen track

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

for event Track

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])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...