Reputation: 81
I'm getting data from a MySQL-database in JSON-format. In an Objective-C file the data gets modified and put into an NSMutableArray ("_data"). By the function "itemsDownloaded" the delegate gets notified as soon the download from the database is finished and receives the "_data"-array.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Create an array to store the data
NSMutableArray *_data = [[NSMutableArray alloc] init];
// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];
// Create a new data object and set its props to JsonElement properties
Data *newData = [[Data alloc] init];
newData.sozialversicherungsnummer = jsonElement[@"Sozialversicherungsnummer"];
newData.messzeitpunkt = jsonElement[@"Messzeitpunkt"];
newData.puls = jsonElement[@"Puls"];
newData.sauerstoffgehalt = jsonElement[@"Sauerstoffgehalt"];
// Add this question to the locations array
[_data addObject:newData];
}
// Ready to notify delegate that data is ready and pass back items
if (self.delegate)
{
[self.delegate itemsDownloaded:_data];
}
}
My aim is to access the properties "sozialversicherungsnummer", "messzeitpunkt", "puls" and "sauerstoffsättigung" of "newData" (in the above file). The class "Data" defines these four properties.
Now I want to display these properties inside of a chart in a swift file. For example I want to display "messzeitpunkt" on the x-axis and "puls" on the y-axis. I know how to handle the chart but my problem is that I don't know how to get access to the properties inside of the swift file.
If I write these lines into my swift file:
var data: NSArray = [];
func itemsDownloaded(items: [AnyObject]!) {
data = items
print("\(data)")
}
I get this on my output:
(
"<Data: 0x7ca5ff60>",
"<Data: 0x7ca5dab0>",
"<Data: 0x7be497e0>",
"<Data: 0x7ca42c00>"
)
Can somebody please help me?
Upvotes: 0
Views: 326
Reputation: 534950
The problem is that you don't want an NSArray. Swift doesn't know what's inside an NSArray. You want a Swift array, namely a [Data]
. That way, Swift knows that each item is a Data, and you can access its properties.
Your output is:
(
"<Data: 0x7ca5ff60>",
"<Data: 0x7ca5dab0>",
"<Data: 0x7be497e0>",
"<Data: 0x7ca42c00>"
)
And that is exactly what you want and expect! You have an array of four Data objects. The only problem is that you have forgotten to tell Swift about this. You need to type the array as a [Data]
or cast it to a [Data]
.
For example, where you are now saying:
func itemsDownloaded(items: [AnyObject]!) {
data = items
print("\(data)")
}
Try saying this:
func itemsDownloaded(items: [AnyObject]!) {
let datas = items as! [Data]
datas.forEach {print($0.messzeitpunkt)}
}
That is legal, because now you have told Swift what is in the array. And you will see that your data is there, exactly as you intend.
Upvotes: 2