簡體   English   中英

Swift中Google Analytics(分析)異常跟蹤的正確語法是什么?

[英]What is the proper syntax for Google Analytics Exception tracking in Swift?

我正在嘗試對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中,只是不確定在Swift中調用createExceptionWithDescription()的語法。

使用Swift進行Google Analytics(分析)的示例/文檔的方式似乎並沒有太多... = /如果您有任何了解,請告訴我!

謝謝。

謝謝您,David Wong,您的帖子對我的語法幫助很大。

這篇文章對我也有很大幫助:在Google iOS Analytics的強制轉換中,無法將類型“ NSMutableDictionary”的值轉換為類型“ [NSObject:AnyObject]”

這就是對我有用的東西:

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

再次感謝!

我可以想象它的某些方面:

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

您可以迅速執行類似的操作:

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

我建議您學習一下ObjC消息傳遞語法,因為很多iOS代碼仍在ObjC中,但不要為此擔心很多。 Swift是一種更好的語言。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM