Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Hamza Ansari
30 PointsHow do I re-write this code in Swift?
I need to be able to set different values for different slices for a Pie Chart, but cannot seem to do so in Swift. It would greatly help if someone could re-write this code in Swift, and help me figure out how to set two slice values for two variables, votes and votes2.
- (CGFloat)pieChart:(XYPieChart *)pieChart valueForSliceAtIndex:(NSUInteger)index {
NSNumber* value = self.values[index];
return value.doubleValue;
}
2 Answers
Jason Wayne
11,688 PointsCan't test out the code for you without having XYPieChart, and self.values array, but it should look like this:
func pieChart(pieChart: XYPieChart, valueForSliceAtIndex index: NSUInteger) -> CGFloat{
var value: NSNumber = self.values[index]
return value.doubleValue
}
Hope it works. =]
Jason Wayne
11,688 PointsNot too sure, it's kinda hard without seeing the actual code. But perhaps somewhere along these lines may help
var votes = self.values[0] var votes2 = self.values[1]
Hope it points you to the right direction.
Hamza Ansari
30 PointsHamza Ansari
30 PointsThank you very much! How do I set two different slice values if both values are variables? One is called votes and the other is votes2. I'm not sure how to add different index numbers, which is the problem.