如何在swift 4中使用像对象数组这样的结构形成静态数据? [英] How to form static data with the structure like array of objects in swift 4?
本文介绍了如何在swift 4中使用像对象数组这样的结构形成静态数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我正在使用 Swift 平台构建应用程序.我需要像对象结构数组一样显示静态数据,因为我需要在 UITableView 部分和行上加载数据.下面是目标 C 中对象结构的示例数组.我需要在 Swift 中复制相同的内容.
Currently, I'm building an app using the Swift platform. I need to show static data like an array of objects structure because I need to load data on UITableView section and rows. Below is the example array of objects structure in objective C. I need to replicate the same thing in 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";
}
)
推荐答案
此 sudo 代码可能会帮助您快速实现数据源.
This sudo code might help you implement datasource in swift.
func prepareMatchInfo() -> Array>{
func prepareMatchInfo () -> Array>{
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;
}
这篇关于如何在swift 4中使用像对象数组这样的结构形成静态数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文