Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions ChartsRealm/Classes/Data/RealmBarDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ open class RealmBarDataSet: RealmBarLineScatterCandleBubbleDataSet, IBarChartDat
super.init(results: results, xValueField: xValueField, yValueField: yValueField, label: label)
}

public convenience init(results: Results<Object>?, xValueField: String?, yValueField: String, stackValueField: String, label: String?)
public convenience init<T: Object>(results: Results<T>?, xValueField: String?, yValueField: String, stackValueField: String, label: String?)
{
var converted: RLMResults<RLMObject>?

Expand All @@ -62,7 +62,7 @@ open class RealmBarDataSet: RealmBarLineScatterCandleBubbleDataSet, IBarChartDat
self.init(results: results, xValueField: xValueField, yValueField: yValueField, stackValueField: stackValueField, label: "DataSet")
}

public convenience init(results: Results<Object>?, xValueField: String?, yValueField: String, stackValueField: String)
public convenience init<T: Object>(results: Results<T>?, xValueField: String?, yValueField: String, stackValueField: String)
{
var converted: RLMResults<RLMObject>?

Expand All @@ -79,7 +79,7 @@ open class RealmBarDataSet: RealmBarLineScatterCandleBubbleDataSet, IBarChartDat
self.init(results: results, xValueField: nil, yValueField: yValueField, stackValueField: stackValueField, label: label)
}

public convenience init(results: Results<Object>?, yValueField: String, stackValueField: String, label: String)
public convenience init<T: Object>(results: Results<T>?, yValueField: String, stackValueField: String, label: String)
{
var converted: RLMResults<RLMObject>?

Expand All @@ -96,7 +96,7 @@ open class RealmBarDataSet: RealmBarLineScatterCandleBubbleDataSet, IBarChartDat
self.init(results: results, xValueField: nil, yValueField: yValueField, stackValueField: stackValueField)
}

public convenience init(results: Results<Object>?, yValueField: String, stackValueField: String)
public convenience init<T: Object>(results: Results<T>?, yValueField: String, stackValueField: String)
{
var converted: RLMResults<RLMObject>?

Expand Down Expand Up @@ -197,9 +197,18 @@ open class RealmBarDataSet: RealmBarLineScatterCandleBubbleDataSet, IBarChartDat
/// is calculated from the Entries that are added to the DataSet
fileprivate var _stackSize = 1

internal override func buildEntryFromResultObject(_ object: RLMObject, x: Double) -> ChartDataEntry
internal override func buildEntryFromResultObject(_ object: RLMObjectBase, x: Double) -> ChartDataEntry
{
let value = object[_yValueField!]
var value: Any?
if let object = object as? RLMObject
Copy link
Copy Markdown
Collaborator Author

@liuxuan30 liuxuan30 Jan 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jjatie Do you have better solution that we could reduce the duplicated code like this pattern.

Basically, we take RLMObjectBase, but later need to cast it to a concrete class type to get the value. Objc is using RLMObject, while Swift is using Object.

I don't come up a better one here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first, you should use let value: Object. It doesn't need to be optional or a variable because you know you're going to initialize it right away.

Second, you can write it like this

let value = (object as? RLMObject)[_yValueField!] ?? (object as! Object)[_yValueField!]

or

let object = (object as? RLMObject) ?? (object as! Object)
let value = object[_yValueField!]

Copy link
Copy Markdown
Collaborator Author

@liuxuan30 liuxuan30 Jan 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems neither of them works.
first statement

let value = (object as? RLMObject)[_yValueField!] ?? (object as! Object)[_yValueField!]

gives me Type 'RLMObject?' has no subscript members, as the left operand is optional, while the right operand would work.

second statement gives me Type 'RLMObjectBase' has no subscript members

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, should have been
let value = (object as? RLMObject)?[_yValueField!] ?? (object as! Object)[_yValueField!]

{
value = object[_yValueField!]
}
else
{
value = (object as! Object)[_yValueField!]
}

let entry: BarChartDataEntry

if let array = value as? RLMArray<RLMObject>
Expand All @@ -209,11 +218,25 @@ open class RealmBarDataSet: RealmBarLineScatterCandleBubbleDataSet, IBarChartDat
{
values.append(val[_stackValueField!] as! Double)
}
entry = BarChartDataEntry(x: _xValueField == nil ? x : object[_xValueField!] as! Double, yValues: values)
if let object = object as? RLMObject
{
entry = BarChartDataEntry(x: _xValueField == nil ? x : object[_xValueField!] as! Double, yValues: values)
}
else
{
entry = BarChartDataEntry(x: _xValueField == nil ? x : (object as! Object)[_xValueField!] as! Double, yValues: values)
}
}
else
{
entry = BarChartDataEntry(x: _xValueField == nil ? x : object[_xValueField!] as! Double, y: value as! Double)
if let object = object as? RLMObject
{
entry = BarChartDataEntry(x: _xValueField == nil ? x : object[_xValueField!] as! Double, y: value as! Double)
}
else
{
entry = BarChartDataEntry(x: _xValueField == nil ? x : (object as! Object)[_xValueField!] as! Double, y: value as! Double)
}
}

return entry
Expand Down
Loading