How can I convert my device token (NSData) into an NSString?

后端 未结 30 1975
挽巷
挽巷 2020-11-28 18:31

I am implementing push notifications. I\'d like to save my APNS Token as a String.

- (void)application:(UIApplication *)application
didRegisterForRemoteNotif         


        
30条回答
  •  孤独总比滥情好
    2020-11-28 19:30

    I've tried to test two different methods with format "%02.2hhx" and "%02x"

        var i :Int = 0
        var j: Int = 0
        let e: Int = Int(1e4)
        let time = NSDate.timeIntervalSinceReferenceDate
        while i < e {
            _ =  deviceToken.map { String(format: "%02x", $0) }.joined()
            i += 1
        }
        let time2 = NSDate.timeIntervalSinceReferenceDate
        let delta = time2-time
        print(delta)
    
        let time3 = NSDate.timeIntervalSinceReferenceDate
        while j < e {
            _ =  deviceToken.reduce("", {$0 + String(format: "%02x", $1)})
            j += 1
        }
        let time4 = NSDate.timeIntervalSinceReferenceDate
        let delta2 = time4-time3
        print(delta2)
    

    and the result is that the fastest is "%02x" at average 2.0 vs 2.6 for the reduced version:

    deviceToken.reduce("", {$0 + String(format: "%02x", $1)})
    

提交回复
热议问题