Swift中Google Analytics异常跟踪的正确语法是什么?

时间:2021-11-01 13:51:49

I'm trying to use Exception Tracking for my app in Google Analytics. https://developers.google.com/analytics/devguides/collection/ios/v3/exceptions

我正在尝试在Google Analytics中为我的应用使用异常跟踪。 https://developers.google.com/analytics/devguides/collection/ios/v3/exceptions

I am just trying to figure out the syntax for this in Swift (not super familiar with Obj-C):

我只想在Swift中找出这个语法(不熟悉Obj-C):

@try {
    // Request some scores from the network.
    NSArray *highScores = [self getHighScoresFromCloud];
}
@catch (NSException *exception) {
    // May return nil if a tracker has not already been initialized with a
    // property ID.
    id tracker = [[GAI sharedInstance] defaultTracker];
    [tracker send:[[GAIDictionaryBuilder
        createExceptionWithDescription:@"Connection timout %d: %@", connectionError, errorDescription  // Exception description. May be truncated to 100 chars.
    withFatal:@NO] build]];  // isFatal (required). NO indicates non-fatal exception.
}

I have set up my tracker okay, and it is working fine saving other data to GA, it is just the syntax calling createExceptionWithDescription() in Swift that I'm not certain of.

我已经设置了我的跟踪器了,它可以很好地将其他数据保存到GA,它只是在Swift中调用createExceptionWithDescription()的语法,我不确定。

There sure doesn't appear to be much in the way of examples / docs for using Swift for Google Analytics... =/ If you know of any, please let me know!

对于使用Swift进行Google Analytics的示例/文档,肯定没有太多内容... = /如果您知道,请告诉我们!

Thanks.

2 个解决方案

#1


0  

I'd imagine its something along the lines of:

我想象它的一些东西:

let dictionaryToSend = GAIDictionaryBuilder.createExceptionWithDescription("Connection timeout \(connectionError): \(errorDescription)", withFatal: NSNumber(bool: false)).build()

If its a class function in Obj-C written like

如果它的一个类函数在Obj-C中编写就好了

[GAIDictionaryBuilder createExceptionWithDescription:...]; // objc

[GAIDictionaryBuilder createExceptionWithDescription:...]; // objc

Its written like

它写得像

GAIDictionaryBuilder.createExceptionWithDescription(...); // swift

GAIDictionaryBuilder.createExceptionWithDescription(...); //很快

Each of the colons in obj-c denotes an argument variable.

obj-c中的每个冒号表示参数变量。

// Broken into two lines to make it easier to read
    [GAIDictionaryBuilder createExceptionWithDescription: @"String here"
                          withFatal: @NO]; 

You can do a similar thing in swift:

你可以在swift中做类似的事情:

//Comma separated 
    GAIDictionaryBuilder.createExceptionWithDescription("Description String",
                                                        withFatal: NSNumber(bool:false));

I suggest you learn a little bit of the ObjC messaging syntax since a lot of iOS code is still in ObjC but don't worry a huge amount about it. Swift is a better language.

我建议你学习一点ObjC消息语法,因为很多iOS代码仍然在ObjC中,但不要担心它的大量数据。斯威夫特是一种更好的语言。

#2


1  

Thanks, David Wong, your post helped a lot to get me on the right track with the syntax.

谢谢,黄大卫,你的帖子帮助了我很多,使我的语法正确。

This post also helped me a lot: Cannot convert value of type 'NSMutableDictionary' to type '[NSObject: AnyObject]' in coercion for google ios Analytics

这篇文章也对我有很大的帮助:无法将'NSMutableDictionary'类型的值转换为强制类型为'[NSObject:AnyObject]'的谷歌ios Analytics

This is what ended up working for me:

这最终为我工作:

let tracker = GAI.sharedInstance().defaultTracker
let eventTracker: NSObject = GAIDictionaryBuilder.createExceptionWithDescription("No internet connection.", withFatal: false).build()
tracker.send(eventTracker as! [NSObject : AnyObject])

Thanks again!

#1


0  

I'd imagine its something along the lines of:

我想象它的一些东西:

let dictionaryToSend = GAIDictionaryBuilder.createExceptionWithDescription("Connection timeout \(connectionError): \(errorDescription)", withFatal: NSNumber(bool: false)).build()

If its a class function in Obj-C written like

如果它的一个类函数在Obj-C中编写就好了

[GAIDictionaryBuilder createExceptionWithDescription:...]; // objc

[GAIDictionaryBuilder createExceptionWithDescription:...]; // objc

Its written like

它写得像

GAIDictionaryBuilder.createExceptionWithDescription(...); // swift

GAIDictionaryBuilder.createExceptionWithDescription(...); //很快

Each of the colons in obj-c denotes an argument variable.

obj-c中的每个冒号表示参数变量。

// Broken into two lines to make it easier to read
    [GAIDictionaryBuilder createExceptionWithDescription: @"String here"
                          withFatal: @NO]; 

You can do a similar thing in swift:

你可以在swift中做类似的事情:

//Comma separated 
    GAIDictionaryBuilder.createExceptionWithDescription("Description String",
                                                        withFatal: NSNumber(bool:false));

I suggest you learn a little bit of the ObjC messaging syntax since a lot of iOS code is still in ObjC but don't worry a huge amount about it. Swift is a better language.

我建议你学习一点ObjC消息语法,因为很多iOS代码仍然在ObjC中,但不要担心它的大量数据。斯威夫特是一种更好的语言。

#2


1  

Thanks, David Wong, your post helped a lot to get me on the right track with the syntax.

谢谢,黄大卫,你的帖子帮助了我很多,使我的语法正确。

This post also helped me a lot: Cannot convert value of type 'NSMutableDictionary' to type '[NSObject: AnyObject]' in coercion for google ios Analytics

这篇文章也对我有很大的帮助:无法将'NSMutableDictionary'类型的值转换为强制类型为'[NSObject:AnyObject]'的谷歌ios Analytics

This is what ended up working for me:

这最终为我工作:

let tracker = GAI.sharedInstance().defaultTracker
let eventTracker: NSObject = GAIDictionaryBuilder.createExceptionWithDescription("No internet connection.", withFatal: false).build()
tracker.send(eventTracker as! [NSObject : AnyObject])

Thanks again!