目前,我正在使用Swift平台构建应用程序。我需要显示静态数据,如对象结构数组,因为我需要在UITableView部分和行上加载数据。下面是目标C中对象结构的示例数组。我需要在Swift中复制相同的东西。
NSArray *dummyArray = [[NSArray alloc] initWithObjects:
[[NSDictionary alloc] initWithObjectsAndKeys:@"India",@"team",@"280",@"score",@"5",@"wickets", nil],
[[NSDictionary alloc] initWithObjectsAndKeys:@"SouthAfrica",@"team",@"279",@"score",@"9",@"wickets", nil],
nil];
NSMutableArray *demoArrayOfObjects = [[NSMutableArray alloc] initWithObjects:
[[NSDictionary alloc] initWithObjectsAndKeys:@"17-03-2018",@"date",@"Ind win by 5 wickets",@"result",@"SouthAfrica",@"Place",dummyArray,@"data", nil], nil];
NSLog(@"demoArrayOfObjects = %@",demoArrayOfObjects);
输出:
demoArrayOfObjects = (
{
Place = SouthAfrica;
data = (
{
score = 280;
team = India;
wickets = 5;
},
{
score = 279;
team = SouthAfrica;
wickets = 9;
}
);
date = "17-03-2018";
result = "Ind win by 5 wickets";
}
)
答案 0 :(得分:0)
尝试一下,用swift 4编写
let dataDictionary1 = ["team": "India", "score": "280", "wickets": "5"]
let dataDictionary2 = ["team": "SouthAfrica", "score": "279", "wickets": "9"]
let dummyArray = [dataDictionary1, dataDictionary2]
let fullDictionary = ["date": "17-03-2018", "result": "Ind win by 5 wickets", "Place": "SouthAfrica", "data": dummyArray] as [String : Any]
let demoArrayOfObjects = [fullDictionary]
print(demoArrayOfObjects)
转换json:
if let jsonData = try? JSONSerialization.data(withJSONObject: demoArrayOfObjects, options: []) {
if let jsonString = String(data: jsonData, encoding: String.Encoding.utf8) {
print("json - \(jsonString)")
}
}
答案 1 :(得分:0)
这个sudo代码可能会帮助你在swift中实现数据源。
func prepareMatchInfo() - >阵列> {
var arrayOfMatches : Array<Dictionary<String,Any>> = Array<Dictionary<String,String>>();
var arrayOfScores : Array<Dictionary<String,String>> = Array<Dictionary<String,String>>();
arrayOfScores.append(getTeamScore(teamName: "India", score: "280", wickets: "5"));
arrayOfScores.append(getTeamScore(teamName: "South Africa", score: "279", wickets: "6"));
arrayOfMatches.append(getMatchInfo(place: "South Africa", data: arrayOfScores, date: Date(), result: "India Won by 5 Wickets"));
return arrayOfMatches;
}
func getMatchInfo (place:String, data:Array<Dictionary<String,String>>, date:Date, result:String) -> Dictionary<String, Any>{
var dict : Dictionary<String, Any> = Dictionary();
dict["Place"] = place;
dict["Data"] = data;
dict["Date"] = date;
dict["Result"] = result;
return dict;
}
func getTeamScore (teamName:String, score:String, wickets:String) -> Dictionary<String, String>{
var dict : Dictionary<String, String> = Dictionary();
dict["TeamName"] = teamName;
dict["Score"] = score;
dict["Wickets"] = wickets;
return dict;
}
答案 2 :(得分:0)
我会创建一个符合Encodable协议的结构数组,而不是使用字典:
注意:仅限Swift 4
struct CricketData : Encodable {
let country: String
let score: Int
let wickets: Int
}
struct Tournament : Encodable {
let data: [CricketData]
let hostCountry: String
let result: String
let date: Date
}
let array = [Tournament] // here you should create your data array with all the info about the games
let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(array)