1

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

我只是想弄清楚 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.
}

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

使用 Swift 进行 Google Analytics(分析)的示例/文档似乎并没有太多... =/ 如果您知道,请告诉我!

谢谢。

4

2 回答 2

1

谢谢,David Wong,你的帖子让我在语法上走上了正轨。

这篇文章对我也有很大帮助: Cannot convert value of type 'NSMutableDictionary' to type '[NSObject: AnyObject]' in coercion for google ios Analytics

这最终为我工作:

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

再次感谢!

于 2016-03-02T09:59:56.893 回答
0

我想它是这样的:

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

如果它是 Obj-C 中的类函数,写成

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

它写得像

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

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

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

您可以在 swift 中执行类似的操作:

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

我建议你学习一点 ObjC 消息语法,因为很多 iOS 代码仍然在 ObjC 中,但不要担心太多。Swift 是一种更好的语言。

于 2016-03-01T23:45:27.433 回答