问题
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);
Output:
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";
}
)
回答1:
This sudo code might help you implement datasource in swift.
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;
}
回答2:
Try it, written in 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)
to convert json:
if let jsonData = try? JSONSerialization.data(withJSONObject: demoArrayOfObjects, options: []) {
if let jsonString = String(data: jsonData, encoding: String.Encoding.utf8) {
print("json - \(jsonString)")
}
}
回答3:
I would make an array of structs that conform to the Encodable protocol, instead of using dictionaries:
Note: Swift 4 only
More info at Apple: https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types
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)
来源:https://stackoverflow.com/questions/49572962/how-to-form-static-data-with-the-structure-like-array-of-objects-in-swift-4