using System; using System.Linq; using System.Text; using System.Collections.Generic; namespace CoderEngine { public class SwiftCodeWrite { private string CodeStr = ""; /// /// 一个代码缩进 /// private string tabstr = " "; /// /// 逐行添加代码 /// /// /// public void CoderAddLine(string CodeLine) { CodeStr += CodeLine + "\n"; } public string ModelToSwiftCode(string _AssemblyName, string _TypeName) { TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName); ModelToJavaCodeWriter(tm); return CodeStr; } public void ModelToJavaCodeWriter(TypeMode TopMode) { //如果有代码嵌套的下级类 在本类外 写入新的类 if (TopMode.TypeList != null && TopMode.TypeList.Count > 0) { CoderAddLine(tabstr); CoderAddLine("import Foundation"); CoderAddLine("import ObjectMapper"); SwitchCodeDownRecursion(TopMode); } } public void SwitchCodeDownRecursion(TypeMode TopMode) { string ClassName = ""; //如果自己就是集合类型 则取类型下名字 if (TopMode.MemberType.Name == "List`1") { //FullName的值为 "System.Collections.Generic.List`1[[ --集合内嵌类型名称--, CoderEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" //其中包含内嵌List的名称 我们要将其取出 得到的值例如 “程序集.*.*类型” var SonParamName = TopMode.MemberType.FullName.Substring(TopMode.MemberType.FullName.IndexOf("[") + 2); SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(",")); ClassName = SonParamName.Substring(SonParamName.LastIndexOf(".") + 1); } else ClassName = TopMode.MemberType.Name; CoderAddLine(""); CoderAddLine("class "+ClassName+": Mappable {"); foreach (var tl in TopMode.TypeList) { CoderAddLine(""); if (tl.MemberType.Name == "List`1") { //如果集合类型的特殊对待? 貌似Swift没有 } else { } CoderAddLine(tabstr + "var "+ tl.Name + ": "+ CSharpToSwiftTypeName(tl.MemberType) + " = "+ GetDefaultValueForType(tl.MemberType)); } CoderAddLine(""); CoderAddLine(tabstr + "required init?(map: Map) {"); CoderAddLine(""); CoderAddLine(tabstr + "}"); CoderAddLine(""); CoderAddLine(tabstr + "//为" + ClassName + "生成mapping方法"); CoderAddLine(tabstr + "func mapping(map: Map) {"); CoderAddLine(""); foreach (var tl in TopMode.TypeList) { CoderAddLine(tabstr + tabstr + tl.Name + " <- map[\""+ tl.Name + "\"]"); } CoderAddLine(tabstr + "}"); CoderAddLine("}"); foreach (var tl in TopMode.TypeList) { if (tl.MemberType.Name == "List`1") { if (tl.TypeList != null && tl.TypeList.Count > 0) { CoderAddLine(""); SwitchCodeDownRecursion(tl); } } } } public string GetDefaultValueForType(Type myType) { switch (CSharpToSwiftTypeName(myType)) { case "Int": return "0"; case "Double": return "0"; case "Bool": return "Bool"; case "String": return "\"\""; default: { if (CSharpToSwiftTypeName(myType).Contains("[")) { return "[]"; } else return "\"\""; } } } public string CSharpToSwiftTypeName(Type myType) { var oneinfo = CSharpToSwiftTypeItem.Where(w => w.Key == myType.Name).FirstOrDefault(); if (oneinfo.Value != null) { return oneinfo.Value; } else { if (myType.Name == "List`1") { var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2); SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(",")); return "[" + SonParamName.Substring(SonParamName.LastIndexOf('.') + 1) + "]"; } else if (myType.Name == "Nullable`1") { var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2); SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(",")); return SonParamName.Substring(SonParamName.LastIndexOf('.') + 1); } else return "String"; } } /// /// C#和Swift类型对照表 /// public System.Collections.Generic.Dictionary CSharpToSwiftTypeItem = new Dictionary() { { "int","Int"}, { "Int16","Int"}, { "Int32","Int"}, { "Int64","Int"}, { "int?","Int"}, { "long","Int"}, { "long?","Int"}, { "float","Double"}, { "float?","Double"}, { "double","Double"}, { "double?","Double"}, { "boolean","Bool"}, { "string","String"}, { "DateTime","String"}, { "DateTime?","String"}, }; } }