Swift - define a recursive type with a protocol
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I want to define a type that can be serialized to a valid JSON object So for instance if a JSON can contain the following: String Number Array Object Date Boolean I would like to define a protocol with valid types protocol JsonSerializable { } typealias JSONObject = [ String : JsonSerializable ] typealias JSONArray = [ JsonSerializable ] // foundation implements serializing numbers + strings + dates etc. to JSON extension String : JsonSerializable {} extension Int : JsonSerializable {} // problem with defining dictionary and array