为什么Xcode 8(iOS 10)在控制台中打印[LogMessageLogging]

前端之家收集整理的这篇文章主要介绍了为什么Xcode 8(iOS 10)在控制台中打印[LogMessageLogging]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么 Xcode 8(iOS 10)打印[LogMessageLogging]< private>在控制台中,当我调用地图视图时?

任何人都可以提出一些建议吗?

解决方法

Privacy

The unified logging system considers dynamic strings and complex dynamic objects to be private,and does not collect them automatically. To ensure the privacy of users,it is recommended that log messages consist strictly of static strings and numbers. In situations where it is necessary to capture a dynamic string,you may explicitly declare the string public using the keyword public. For example,%{public}s.

>资料来源:https://developer.apple.com/reference/os/1891852-logging
> WWDC视频:https://developer.apple.com/videos/play/wwdc2016/721/?time=714

示例(ObjC):

os_log_t log = os_log_create("com.example.my-subsystem","test");

const char *staticString = "I am static string!";
const char *dynamicString = [[NSString stringWithFormat:@"I am %@!",@"dynamic string"]
                             cStringUsingEncoding:NSUTF8StringEncoding];

os_log(log,"Message: %s",staticString);
os_log(log,dynamicString);
os_log(log,"Message: %{public}s",dynamicString);

// Output
// [test] Message: I am static string!
// [test] Message: <private>
// [test] Message: I am dynamic string!

示例(Swift):

目前在Swift(https://openradar.appspot.com/radar?id=6068967584038912)中记录字符串似乎已被破坏,因此我们需要手动将C函数桥接到Swift:

更新01.雷达28599032是修复程序,不适用于Xcode 10.

// File: os_log_rdar28599032.h
#import <Foundation/Foundation.h>
#import <os/log.h>

void log_private(os_log_t,os_log_type_t,NSString *);
void log_public(os_log_t,NSString *);


// File: os_log_rdar28599032.m
#import "os_log_rdar28599032.h"

void log_private(os_log_t log,os_log_type_t type,NSString * message) {
   os_log_with_type(log,type,"%s",[message cStringUsingEncoding:NSUTF8StringEncoding]);
}

void log_public(os_log_t log,"%{public}s",[message cStringUsingEncoding:NSUTF8StringEncoding]);
}


// File: ViewController.swift
import Cocoa
import os.log
import os.activity

class ViewController: NSViewController {

   static let log = OSLog(subsystem: "com.example.my-subsystem",category: "test")
   typealias SelfClass = ViewController

   override func viewDidLoad() {
      super.viewDidLoad()
      log_private(SelfClass.log,.fault,"I am dynamic \("string")!")
      log_public(SelfClass.log,"I am dynamic \("string")!")
      log_private(SelfClass.log,#file)
      log_public(SelfClass.log,#file)
   }

}

// Output
// [test] <private>
// [test] I am dynamic string!
// [test] <private>
// [test] /[REDACTED]/ViewController.swift

猜你在找的iOS相关文章