Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot pass in struct to ChartDataEntry #954

Closed
YileiHeOMT opened this issue Apr 19, 2016 · 5 comments
Closed

Cannot pass in struct to ChartDataEntry #954

YileiHeOMT opened this issue Apr 19, 2016 · 5 comments

Comments

@YileiHeOMT
Copy link

YileiHeOMT commented Apr 19, 2016

In the class ChartDataEntry:
The initialiser is

public init(value: Double, xIndex: Int, data: AnyObject?)
    {
        super.init()

        self.value = value
        self.xIndex = xIndex
        self.data = data
    }

For the third parameter, I can only pass in a class type object. That makes it impossible to pass in a struct or a tuple. So I recommend changing the type or data to Any?

@liuxuan30
Copy link
Member

liuxuan30 commented Apr 20, 2016

well why you want to use struct or tuple for data? you can convert it to Object and move on.
BTW, struct and class are different at low level of swift (value type and reference type). Changing to struct needs more considerations and changes and I just can't see what's the benefit

@YileiHeOMT
Copy link
Author

YileiHeOMT commented Apr 20, 2016

@liuxuan30
My case is:
In my line chart, the value for each XIndex is a double type value(certainly it is). When I click on the chart, I want the pop-up overlay(aka. marker) to show more details like "date".

Let's see a example:

struct CarSalesHistoryItem {
 var profit:Double
 var date: NSDate
 var numberOfCarsSold:Int
//...
}

In the chart, I get a array of CarSalesHistoryItem. I use profit as yAxis value and date as xAxis value. When I tap on a point, I want it to display all the information of that history item.
Then in the method public override func refreshContent(entry entry: ChartDataEntry, highlight: ChartHighlight) of ChartMarker, I can get the data from entry.data for display.

You may say, changing CarSalesHistoryItem to class type will fix it. But in many projects, we use struct as data model. Because struct is "value type" while class is "reference type".

@liuxuan30
Copy link
Member

liuxuan30 commented Apr 20, 2016

yeah, then it's your duty to take care of them. It's very easy for you to sub class the entry class that can take a struct and send back to you, or write an extension to convert between struct and class, or at least you can add a new init method for ChartDataEntry

@YileiHeOMT
Copy link
Author

Fair enough

@pmairoldi
Copy link
Collaborator

I'm not sure but I believe it might be anyobject for technical reasons. I don't think any translates to objc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants