1

我試圖在Google Analytics中爲我的應用使用例外跟蹤功能。 https://developers.google.com/analytics/devguides/collection/ios/v3/exceptionsSwift中Google Analytics(分析)異常跟蹤的正確語法是什麼?

我只是想弄清楚在斯威夫特這句法(不是超級熟悉的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()的語法,我不確定。

有肯定不會出現在示例/文檔的方式多使用斯威夫特谷歌Analytics(分析)... = /如果你知道的任何,請讓我知道!

謝謝。

回答

0

我想像它沿着線的東西:如果它的對象 - 一類函數這樣寫

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

它寫的

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

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

每個O f 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是一種更好的語言。

1

謝謝,黃凱,您的帖子幫了不少忙,讓我在正確的軌道與語法上。

這個帖子也幫了我很多: 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]) 

再次感謝!

相關問題