How to set timezone(UTC) UIDatePicker? - ios6

I want to set the timezone from (-12:00) - (+12:00) to (-23:45) -(+23:45)
+ (NSArray *)timeZoneList {
NSMutable *list = [[NSMustableArry alloc]init];
for(int n = -(23 * 60+45); n<= 23*60+45; n+=15; {
NSString * timeZoneName = #"UTC ";
NSString *sign;
if (n==0) sign = #"+-";
else if(n<0) sign = #"-";
else sign = #"+";
timeZoneName = [timeZoneName stringByAppendinfFormat:#"%# %.2d:%.2d",sign,abs(n/60),abs(n%60)];
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:n*60];
[list addObject:[NSArray arryWithObjects:timeZoneName,timeZone,nil]];
}
return [list copy];
}
I found a problem above (-18:15) - (+18:15).
What can I do? Please advise.

Related

How to shuffle a deck of struct cards without using an array in c

I have this code. I create 10 cards in function createDeck and i want to shuffle the cards in function MyDeckOutput without using an array. Can someone help??
I have no idea and my teacher wants like that. I am not allow to use c++ or something extern. :/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctime>
typedef struct Card
{
char name[50];
int maxAlter;
float maxGewicht;
double maxLaenge;
struct Card* pNext;
} cards;
cards* createDeck()
{
cards* pStart = NULL;
cards* pLast = NULL;
cards* pNew = (cards*)malloc(sizeof(cards));
for (int iElm = 0; iElm < 10; iElm++) {
cards* pNew = (cards*)malloc(sizeof(cards));
if (iElm == 0) { strcpy_s(pNew->name, "Ameisenbaer"); pNew->maxAlter = 14; pNew->maxGewicht = 39; pNew->maxLaenge = 0.90; pNew->pNext = NULL; }
if (iElm == 1) { strcpy_s(pNew->name, "Biber"); pNew->maxAlter = 21; pNew->maxGewicht = 30; pNew->maxLaenge = 1.02; pNew->pNext = NULL; }
if (iElm == 2) { strcpy_s(pNew->name, "Brauenbaer"); pNew->maxAlter = 30; pNew->maxGewicht = 600; pNew->maxLaenge = 1.50; pNew->pNext = NULL; }
if (iElm == 3) { strcpy_s(pNew->name, "Delfin"); pNew->maxAlter = 45; pNew->maxGewicht = 150; pNew->maxLaenge = 7.00; pNew->pNext = NULL; }
if (iElm == 4) { strcpy_s(pNew->name, "Elefant"); pNew->maxAlter = 70; pNew->maxGewicht = 6000; pNew->maxLaenge = 3.00; pNew->pNext = NULL; }
if (iElm == 5) { strcpy_s(pNew->name, "Esel"); pNew->maxAlter = 14; pNew->maxGewicht = 39; pNew->maxLaenge = 0.90; pNew->pNext = NULL; }
if (iElm == 6) { strcpy_s(pNew->name, "Federmaus"); pNew->maxAlter = 21; pNew->maxGewicht = 30; pNew->maxLaenge = 1.02; pNew->pNext = NULL; }
if (iElm == 7) { strcpy_s(pNew->name, "Fuchs"); pNew->maxAlter = 30; pNew->maxGewicht = 600; pNew->maxLaenge = 1.50; pNew->pNext = NULL; }
if (iElm == 8) { strcpy_s(pNew->name, "Gorilla"); pNew->maxAlter = 45; pNew->maxGewicht = 150; pNew->maxLaenge = 7.00; pNew->pNext = NULL; }
if (iElm == 9) { strcpy_s(pNew->name, "Giraffe"); pNew->maxAlter = 70; pNew->maxGewicht = 6000; pNew->maxLaenge = 3.20; pNew->pNext = NULL; }
pNew->pNext = NULL;
if (pStart == NULL) pStart = pNew;
if (pLast != NULL) pLast->pNext = pNew;
pLast = pNew;
}
return pStart;
}
/*void MyDeckOutput(cards* pStart)
{
int iEle = 0;
for (cards* pOut = pStart; pOut != NULL; pOut = pOut->pNext)
{
iEle++;
if (iEle < 6) printf("name = %s\n", pOut->name);
}
}*/
void MyDeckOutput(cards* pStart)
{
for (cards* pOut = pStart; pOut != NULL; pOut = pOut->pNext) printf("name = %s\n", pOut->name);
}
void shuffleDeck(cards* pStart)
{
cards* shuffled = NULL;
cards* end = NULL;
int numberOfCards = 10; // cards number
srand(time(NULL)); // seeds the random nr generator with the current
while (numberOfCards > 0)
{
int index = rand() % numberOfCards;
cards* previousCard = NULL;
cards* selectedCard = pStart->pNext;
// iterate over linked list
if (!shuffled)
end = shuffled = selectedCard;
else
end->pNext = selectedCard;
if (previousCard)
previousCard->pNext = selectedCard->pNext;
end->pNext = NULL;
--numberOfCards;
printf("name = %s%i\n", selectedCard->name, index);
}
}
int main()
{
cards* pStart = createDeck();
MyDeckOutput(pStart);
printf("\n\nShuffel:\n");
shuffleDeck(pStart);
system("pause");
return 0;
};
The commentor #user3386109 is spot on with his suggestion.
To help you get started, here's a little code snippet to do what you need to do.
void shuffleDeck(cards *deck)
{
cards *shuffled = NULL;
cards *end = NULL;
int numberOfCards = countDeck(deck); //Count number of cards in your deck
srand(time(NULL)); //Seeds the random number generator with the current
while (numberOfCards > 0)
{
int index = rand()%numberOfCards;
cards *previousCard = NULL;
cards *selectedCard;
//Iterate over linked list until you arrive at index 'x'.
if (!shuffled)
end = shuffled = selectedCard;
else
end->next = selectedCard;
if (previousCard)
previousCard->next = selectedCard->next;
end->next = NULL;
--numberOfCards;
}
}
Please note that this code is incomplete on purpose and merely meant to give you an idea on how to do it.
Edit: I'm not really supposed to do this, but this shuffle code works for me.
cards *shuffleDeck(cards *deck)
{
cards *shuffled = NULL;
cards *end = NULL;
int numberOfCards = countDeck(deck); //Count number of cards in your deck
srand(time(NULL)); //Seeds the random number generator with the current
while (numberOfCards > 0)
{
int index = rand()%numberOfCards;
cards *previousCard = NULL;
cards *selectedCard = deck;
//Iterate over linked list until you arrive at index 'x'.
for (int i = 0; i < index && selectedCard; ++i)
{
previousCard = selectedCard;
selectedCard = selectedCard->pNext;
}
if (!shuffled)
shuffled = selectedCard;
else if (end)
end->pNext = selectedCard;
end = selectedCard;
if (selectedCard)
{
if (previousCard)
previousCard->pNext = selectedCard->pNext; //Closes the gap between the previous element and the next element.
else
deck = selectedCard->pNext; //Resets the anchor element.
selectedCard->pNext = NULL;
}
--numberOfCards;
}
return shuffled;
}
int main()
{
MyDeckOutput(shuffleDeck(createDeck()));
return 0;
};
I will leave filling the gaps to you (how to count the number of the cards).
Please also note, that your 'creating a card' is faulty.
Either use curly braces ({}) around your assignments or, better yet, use a dedicated function.
Alternatively I recommend reading about 'switch/case' statements.

Get values from a Array in a Dictionary in Swift

My ambition is to have a Dictionary that contains (among other things) an array and being able to get out the values of that array.
var total: Int = 0;
let dice:[NSTextField:NSArray] = [
d4Amount:[d4OE, 4],
d6Amount:[d6OE, 6],
];
for (die, dieArray) in dice
{
let button:NSButton = dieArray[0] as! NSButton;
let num:Int = dieArray[1] as! Int;
total += DoRoll(die, oe: button, max: num);
}
In the above the line "let button:NSButton = dieArray[0]..." get's the error Thread 1: signal SIGABRT and the program fails.
First I only had the line:
total += DoRoll(die, oe: dieArray[0] as! NS Button, max: dieArray[1] as! Int);
Which didn't either work (quite obviously), but however when I do this, it works...
total += DoRoll(d4Amount, oe: d4OE, max: 4);
It works perfectly.
Any ideas??
The function DoRoll looks like this (which should not be relevant):
private func DoRoll(amount: NSTextField, oe: NSButton, max: Int) -> Int
{
let nrRolls: Int! = Int(amount.stringValue);
var total: Int = 0;
if(nrRolls != nil && nrRolls != 0)
{
OutputText.string = OutputText.string! + "Now rolling d" + String(max) + ": ";
for(var i: Int = 0; i < nrRolls; i++)
{
var randomNr: Int, specialMax: Int;
var textStr: String = "";
specialMax = max;
if(max >= 100)
{
if(max > 100)
{
specialMax = 995;
}
else if(max > 99)
{
specialMax = 95;
}
else
{
specialMax = max - 1;
}
}
repeat
{
randomNr = Int(arc4random_uniform(UInt32(max))) + 1;
total += randomNr;
if(textStr != "") { textStr = textStr + "+"; }
textStr = textStr + String(randomNr);
} while(oe.state == NSOnState && randomNr >= specialMax)
OutputText.string = OutputText.string! + textStr + " ";
}
OutputText.string = OutputText.string! + "\n";
}
return total;
}
I now know what I did wrong.
This is not at all a case of error in the code above but in the naming of items in the project. I had several different D4Amount in the same project in the View and thus that was why it crashed.
Sorry to bother you.

CorePlot refresh plot connected to table

I'm working with core plot on xCode and I want to plot some real-time incoming data via bluetooth.
I used the CPTTestAPP for Mac Os X as example and tried to adapt it to my specific case.
The first 60 points are added like it's supposed to and due the following unaltered code:
-(void)setupScatterPlots
{
static BOOL hasData = NO;
// Create one plot that uses bindings
CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init];
boundLinePlot.identifier = bindingsPlot;
CPTMutableLineStyle *lineStyle = [boundLinePlot.dataLineStyle mutableCopy];
lineStyle.miterLimit = 1.0;
lineStyle.lineWidth = 2.0;
lineStyle.lineColor = [CPTColor redColor];
boundLinePlot.dataLineStyle = lineStyle;
[self.graph addPlot:boundLinePlot];
[boundLinePlot bind:CPTScatterPlotBindingXValues toObject:self withKeyPath:#"arrangedObjects.x" options:nil];
[boundLinePlot bind:CPTScatterPlotBindingYValues toObject:self withKeyPath:#"arrangedObjects.y" options:nil];
// Set plot delegate, to know when symbols have been touched
// We will display an annotation when a symbol is touched
boundLinePlot.delegate = self;
boundLinePlot.plotSymbolMarginForHitDetection = 5.0;
if ( !hasData ) {
// Add some initial data
contentArray = [NSMutableArray arrayWithCapacity:100];
for ( NSUInteger i = 0; i < 60; i++ ) {
NSNumber *x = #(1.0 + i * 0.05);
NSNumber *y = #(1.2 * arc4random() / (double)UINT32_MAX + 1.2);
[contentArray addObject:#{ #"x": x,
#"y": y }
];
}
self.content = contentArray;
hasData = YES;
}
// Auto scale the plot space to fit the plot data
// Extend the y range by 10% for neatness
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsMomentum = YES;
[plotSpace scaleToFitPlots:#[boundLinePlot]];
CPTPlotRange *xRange = plotSpace.xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromDouble(1.1)];
plotSpace.yRange = yRange;
// Restrict y range to a global range
plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1.0) length:CPTDecimalFromDouble(10.0)];
plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1.0) length:CPTDecimalFromDouble(5.0)];
// set the x and y shift to match the new ranges
CGFloat length = xRange.lengthDouble;
self.xShift = length - 3.0;
length = yRange.lengthDouble;
self.yShift = length - 2.0;
}
I'm using a method called insertData who looks like this:
-(void)insertData:(uint32 *) buf :(uint32 *) i
{
[contentArray addObject:#{ #"x": i,
#"y": buf }
];
NSLog(#"%#",contentArray);
self.content = contentArray;
}
where buf is my incoming data.
But when I call the insertData method, the data are appended to the array but the tab on my interface doesn't change, neither does the plot.
can anyone help me please?
thanks !
I already tried to convert the int to NSNumber by using the following :
NSNumber *ni = #(*i);
NSNumber *nbuf = #(*buf);
and:
NSNumber *ni = [NSNumber numberWithUnsignedInt:(*i);
NSNumber *nbuf = [NSNumber numberWithUnsignedInt:(*buf);
I also tried all the reload stuff I found over the internet
but none of that worked either.
#import "Controller.h"
#import "HeartRateMonitorAppDelegate.h"
static const CGFloat kZDistanceBetweenLayers = 20.0;
static NSString *const bindingsPlot = #"Bindings Plot";
static NSMutableArray *contentArray;
#interface Controller()
#property (nonatomic, readwrite, strong) IBOutlet CPTGraphHostingView *hostView;
#property (nonatomic, readwrite, strong) CPTXYGraph *graph;
#property (nonatomic, readwrite, strong) CPTPlotSpaceAnnotation *symbolTextAnnotation;
-(void)setupGraph;
-(void)setupAxes;
-(void)setupScatterPlots;
#end
#pragma mark -
#implementation Controller
#synthesize hostView;
#synthesize graph;
#synthesize symbolTextAnnotation;
+(void)initialize
{
[NSValueTransformer setValueTransformer:[CPTDecimalNumberValueTransformer new] forName:#"CPTDecimalNumberValueTransformer"];
}
-(void)awakeFromNib
{
[super awakeFromNib];
self.xShift = 0.0;
self.yShift = 0.0;
[self setupGraph];
[self setupAxes];
[self setupScatterPlots];
}
-(id)newObject:(uint32 *) buf :(uint32 *) i
{
NSLog(#"buf = %d", *buf);
NSNumber *x1 = #(*i);
NSNumber *y1 = #(*i);
return #{
#"x": x1,
#"y": y1
};
}
#pragma mark -
#pragma mark Graph Setup Methods
-(void)setupGraph
{
// Create graph and apply a dark theme
CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:NSRectToCGRect(self.hostView.bounds)];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[newGraph applyTheme:theme];
self.hostView.hostedGraph = newGraph;
self.graph = newGraph;
// Graph padding
newGraph.paddingLeft = 0.0;
newGraph.paddingTop = 0.0;
newGraph.paddingRight = 0.0;
newGraph.paddingBottom = 0.0;
// Plot area delegate
newGraph.plotAreaFrame.plotArea.delegate = self;
}
-(void)setupAxes
{
// Setup scatter plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
CPTMutableLineStyle *redLineStyle = [CPTMutableLineStyle lineStyle];
redLineStyle.lineWidth = 10.0;
redLineStyle.lineColor = [[CPTColor redColor] colorWithAlphaComponent:0.5];
// Axes
// Label x axis with a fixed interval policy
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromDouble(0.5);
x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);
x.minorTicksPerInterval = 2;
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
NSArray *exclusionRanges = #[[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(1.99) length:CPTDecimalFromDouble(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.99) length:CPTDecimalFromDouble(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(2.99) length:CPTDecimalFromDouble(0.02)]];
x.labelExclusionRanges = exclusionRanges;
// Label y with an automatic label policy.
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);
y.minorTicksPerInterval = 2;
y.preferredNumberOfMajorTicks = 8;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.labelOffset = 10.0;
exclusionRanges = #[[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(1.99) length:CPTDecimalFromDouble(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.99) length:CPTDecimalFromDouble(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(3.99) length:CPTDecimalFromDouble(0.02)]];
y.labelExclusionRanges = exclusionRanges;
}
-(void)setupScatterPlots
{
static BOOL hasData = NO;
// Create one plot that uses bindings
CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init];
boundLinePlot.identifier = bindingsPlot;
CPTMutableLineStyle *lineStyle = [boundLinePlot.dataLineStyle mutableCopy];
lineStyle.miterLimit = 1.0;
lineStyle.lineWidth = 2.0;
lineStyle.lineColor = [CPTColor redColor];
boundLinePlot.dataLineStyle = lineStyle;
[self.graph addPlot:boundLinePlot];
[boundLinePlot bind:CPTScatterPlotBindingXValues toObject:self withKeyPath:#"arrangedObjects.x" options:nil];
[boundLinePlot bind:CPTScatterPlotBindingYValues toObject:self withKeyPath:#"arrangedObjects.y" options:nil];
// Set plot delegate, to know when symbols have been touched
// We will display an annotation when a symbol is touched
boundLinePlot.delegate = self;
boundLinePlot.plotSymbolMarginForHitDetection = 5.0;
if ( !hasData ) {
// Add some initial data
contentArray = [NSMutableArray arrayWithCapacity:100];
for ( NSUInteger i = 0; i < 5; i++ ) {
NSNumber *x = #(1.0 + i * 0.05);
NSNumber *y = #(1.2 * arc4random() / (double)UINT32_MAX + 1.2);
[contentArray addObject:#{ #"x": x,
#"y": y }
];
}
self.content = contentArray;
hasData = YES;
}
// Auto scale the plot space to fit the plot data
// Extend the y range by 10% for neatness
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsMomentum = YES;
[plotSpace scaleToFitPlots:#[boundLinePlot]];
CPTPlotRange *xRange = plotSpace.xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromDouble(1.1)];
plotSpace.yRange = yRange;
// Restrict y range to a global range
plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1.0) length:CPTDecimalFromDouble(10.0)];
plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1.0) length:CPTDecimalFromDouble(5.0)];
// set the x and y shift to match the new ranges
CGFloat length = xRange.lengthDouble;
self.xShift = length - 3.0;
length = yRange.lengthDouble;
self.yShift = length - 2.0;
}
-(void)insertData:(uint32 *) buf :(uint32 *) i
{
NSNumber *ni = #(*i);
//NSNumber *nbuf = #(*buf);
[contentArray addObject:#{ #"x": ni,
#"y": ni }
];
self.content = contentArray;
[graph reloadData];
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
if ( [plot isKindOfClass:[CPTBarPlot class]] ) {
return 8;
}
else {
return [self.arrangedObjects count];
}
}
-(id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSNumber *num;
if ( [plot isKindOfClass:[CPTBarPlot class]] ) {
num = #( (index + 1) * (index + 1) );
if ( [plot.identifier isEqual:bindingsPlot] ) {
num = #(num.integerValue - 10);
}
}
else {
NSString *key = (fieldEnum == CPTScatterPlotFieldX ? #"x" : #"y");
num = (self.arrangedObjects)[index][key];
if ( fieldEnum == CPTScatterPlotFieldY ) {
num = #([num doubleValue] + 1.0);
}
}
return num;
}
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
if ( [(NSString *)plot.identifier isEqualToString : bindingsPlot] ) {
return (id)[NSNull null]; // Don't show any label
}
else if ( [(NSString *)plot.identifier isEqualToString : bindingsPlot] && (index < 4) ) {
return (id)[NSNull null];
}
else if ( index % 4 ) {
return (id)[NSNull null];
}
else {
return nil; // Use default label style
}
}
#pragma mark -
#pragma mark CPTScatterPlot delegate method
-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
CPTPlotSpaceAnnotation *annotation = self.symbolTextAnnotation;
if ( annotation ) {
[self.graph.plotAreaFrame.plotArea removeAnnotation:annotation];
self.symbolTextAnnotation = nil;
}
// Setup a style for the annotation
CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
hitAnnotationTextStyle.color = [CPTColor whiteColor];
hitAnnotationTextStyle.fontSize = 16.0f;
hitAnnotationTextStyle.fontName = #"Helvetica-Bold";
// Determine point of symbol in plot coordinates
NSDictionary *dataPoint = (self.arrangedObjects)[index];
NSNumber *x = dataPoint[#"x"];
NSNumber *y = dataPoint[#"y"];
NSArray *anchorPoint = #[x, y];
// Add annotation
// First make a string for the y value
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
NSString *yString = [formatter stringFromNumber:y];
// Now add the annotation to the plot area
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:yString style:hitAnnotationTextStyle];
annotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:self.graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
annotation.contentLayer = textLayer;
annotation.displacement = CGPointMake(0.0, 20.0);
[self.graph.plotAreaFrame.plotArea addAnnotation:annotation];
self.symbolTextAnnotation = annotation;
}
#pragma mark -
#pragma mark Plot area delegate method
-(void)plotAreaWasSelected:(CPTPlotArea *)plotArea
{
// Remove the annotation
CPTPlotSpaceAnnotation *annotation = self.symbolTextAnnotation;
if ( annotation ) {
[self.graph.plotAreaFrame.plotArea removeAnnotation:annotation];
self.symbolTextAnnotation = nil;
}
}
#pragma mark -
#pragma mark PDF / image export
-(IBAction)exportToPDF:(id)sender
{
NSSavePanel *pdfSavingDialog = [NSSavePanel savePanel];
[pdfSavingDialog setAllowedFileTypes:#[#"pdf"]];
if ( [pdfSavingDialog runModal] == NSOKButton ) {
NSData *dataForPDF = [self.graph dataForPDFRepresentationOfLayer];
[dataForPDF writeToURL:[pdfSavingDialog URL] atomically:NO];
}
}
-(IBAction)exportToPNG:(id)sender
{
NSSavePanel *pngSavingDialog = [NSSavePanel savePanel];
[pngSavingDialog setAllowedFileTypes:#[#"png"]];
if ( [pngSavingDialog runModal] == NSOKButton ) {
NSImage *image = [self.graph imageOfLayer];
NSData *tiffData = [image TIFFRepresentation];
NSBitmapImageRep *tiffRep = [NSBitmapImageRep imageRepWithData:tiffData];
NSData *pngData = [tiffRep representationUsingType:NSPNGFileType properties:nil];
[pngData writeToURL:[pngSavingDialog URL] atomically:NO];
}
}
#pragma mark -
#pragma mark Printing
-(IBAction)printDocument:(id)sender
{
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
NSRect printRect = NSZeroRect;
printRect.size.width = (printInfo.paperSize.width - printInfo.leftMargin - printInfo.rightMargin) * printInfo.scalingFactor;
printRect.size.height = (printInfo.paperSize.height - printInfo.topMargin - printInfo.bottomMargin) * printInfo.scalingFactor;
self.hostView.printRect = printRect;
NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:self.hostView printInfo:printInfo];
[printOperation runOperationModalForWindow:self.hostView.window
delegate:self
didRunSelector:#selector(printOperationDidRun:success:contextInfo:)
contextInfo:NULL];
}
-(void)printOperationDidRun:(NSPrintOperation *)printOperation success:(BOOL)success contextInfo:(void *)contextInfo
{
// print delegate
}
Core Plot expects the data values to be NSNumber instances, not plain integers. You can use NSDecimalNumber (a subclass of NSNumber) if you need to preserve full decimal precision.
Since you're using data bound to an NSArrayController, -reloadData and related methods will have no effect. Those are only used when providing data through a datasource. That's the only method available on iOS which is why much of the sample code and demos on the web use it.
Once an NSArrayController is initialized, you should use its methods to add or remove data rather than updating the content array directly. The CPTTestAPP demo uses -insertObject:atArrangedObjectIndex: and -removeObjectAtArrangedObjectIndex: to do this.

code with double free or corruption

I've got a double free problem in my program. I know which pointer is double freed but I cant figure out when was it freed first.
here is the code of my function :
int spectrum_gen(char *shift_r, char *rec_poly, char *redun_poly,int spectrum_length)
{
char *seq = NULL,*l_shift = NULL,loop_shift[SIZE];
int current_weight,*spectrum = NULL,*spect_numb = NULL,length=1,spectrum_size=0;
int index,index2,temp,temp2,*temp3;
/* int *weights = NULL; */
int *encoded_w = NULL;
int min_length,min_weight = 1000;
int looping = 0;
int **spectrum_content = NULL;
int *seq_w;
int *weight_table = symbols_weight(Q);
int *weights = NULL;
spectrum= (int*) malloc(sizeof(int));
seq = (char*) malloc(sizeof(char));
l_shift = (char*) malloc(SIZE*sizeof(char*));
weights = (int*) malloc(sizeof(int));
encoded_w = (int*) malloc(sizeof(int));
spect_numb = (int*) malloc(sizeof(int));
spectrum_content = (int**) malloc(sizeof(int*));
spectrum_content[1] = (int*) malloc(sizeof(int));
seq_w = (int*) malloc(sizeof(int));
strcpy(seq,"1");
convert_to_real(seq,1);
while(length > 0)
{
/* show_word(seq,length);
show_word(shift_r,SIZE);
puts(""); */
if(length == 1)
{
set2zero(shift_r,SIZE);
current_weight = RTZ_weight(0,seq[0],shift_r,rec_poly,redun_poly,encoded_w,seq_w,weight_table);
*seq_w += seq_weight(seq,1,weight_table);
}
else
{
current_weight = RTZ_weight(weights[length-2],seq[length-1],shift_r,rec_poly,redun_poly,encoded_w,seq_w,weight_table);
*seq_w += seq_weight(seq,length,weight_table);
/* show_word(shift_r,SIZE);
show_word(loop_shift,SIZE); */
if(looping==1 && str_cmp(shift_r,loop_shift,SIZE))
{
puts("looping sequence!!");
free(spectrum);
spectrum = NULL;
free(encoded_w);
encoded_w = NULL;
free(spect_numb);
spect_numb = NULL;
free(spectrum_content);
spectrum_content = NULL;
free(weights);
weights = NULL;
return 1;
break;
}
if(*encoded_w==weights[length-2] && looping==0 && length>3)
{
str_cpy(loop_shift,shift_r,SIZE);
looping = 1;
}
if(*encoded_w != weights[length-2])
{
looping = 0;
}
}
weights = realloc(weights,length*sizeof(int));
weights[length-1] = *encoded_w;
l_shift = realloc(l_shift,length*sizeof(char));
l_shift[length-1] = shift_r[SIZE-1];
if((shift_r[0] != 0) && (*encoded_w < spectrum_length))
{
if((temp = index4(current_weight,spectrum,spectrum_size,1,0)) != (-1))
{
spect_numb[temp]++;
if((temp2 = index4(*seq_w,spectrum_content[temp],spectrum_content[temp][0],2,1)) != (-1))
{ spectrum_content[temp][temp2+1]++;
}
else
{
spectrum_content[temp][0] += 2;
spectrum_content[temp] = realloc(spectrum_content[temp],spectrum_content[temp][0]*sizeof(int));
spectrum_content[temp][spectrum_content[temp][0]-2] = *seq_w;
spectrum_content[temp][spectrum_content[temp][0]-1] = 1;
}
}
else
{
spectrum_size++;
spectrum = realloc(spectrum,spectrum_size*sizeof(int));
spect_numb = realloc(spect_numb,spectrum_size*sizeof(int));
/* spectrum content : creation of a new table to store the inputs with output of weight current_weight*/
spectrum_content = realloc(spectrum_content,spectrum_size*sizeof(int*));
spectrum_content[spectrum_size-1] = (int*) malloc(3*sizeof(int));
spectrum_content[spectrum_size-1][0] = 3;
spectrum_content[spectrum_size-1][1] = *seq_w;
spectrum_content[spectrum_size-1][2] = 1;
spectrum[spectrum_size-1] = current_weight;
spect_numb[spectrum_size-1] = 1;
}
}
if(seq_equal_zero(shift_r,SIZE) || (*encoded_w >= spectrum_length))
{
while((length>0) && (seq[length-1] == Q-1))
{
length--;
for(index=0;index<SIZE-1;index++)
shift_r[index] = shift_r[index+1];
shift_r[SIZE-1] = l_shift[length-1];
}
for(index=0;index<SIZE-1;index++)
shift_r[index] = shift_r[index+1];
shift_r[SIZE-1] = l_shift[length-2];
seq[length-1] += 1;
}
else
{
length++;
seq = realloc(seq,length*sizeof(char));
seq[length-1] = 0;
}
/* printf("%d\n%d\n",*encoded_w,current_weight);
getchar(); */
}
/* sort(spectrum,spect_numb,spectrum_content,spectrum_size);*/
puts("Debut du spectre de ce codeur :");
for(index=0;spectrum[index+1]<=spectrum_length;index++)
{
printf("( ");
for(index2=1;index2<spectrum_content[index][0]-2;index2+=2)
{
printf("%d*I^%d + ",spectrum_content[index][index2+1],spectrum_content[index][index2]);
}
printf("%d*I^%d",spectrum_content[index][spectrum_content[index][0]-1],spectrum_content[index][spectrum_content[index][0]-2]);
printf(" )*O^%d + ",spectrum[index]);
}
printf("( ");
for(index2=1;index2<spectrum_content[index][0]-2;index2+=2)
{
printf("%d*I^%d + ",spectrum_content[index][index2+1],spectrum_content[index][index2]);
}
printf("%d*I^%d",spectrum_content[index][spectrum_content[index][0]-1],spectrum_content[index][spectrum_content[index][0]-2]);
printf(")*O^%d",spectrum[index]);
puts("");
free(seq);
seq = NULL;
free(spectrum);
spectrum = NULL;
free(encoded_w);
encoded_w = NULL;
free(spect_numb);
spect_numb = NULL;
free(spectrum_content);
spectrum_content = NULL;
free(weights);
weights = NULL;
return 0;
}
that pointer is called seq.
It would be so cool if someone helps me with this :)
here are the two functions that handle that pointer
void convert_to_real(char *word,int end)
{
int index;
for(index=0;index<end;index++) word[index]-='0';
}
i dont think it may be a problem
the only other function that handles that pointer is :
int seq_weight(char *seq,int end,int *weight_table)
{
int index,weight = 0;
for(index=0;index<end;index++)
if(seq[index]>=0 && seq[index]<Q)
weight += weight_table[(int)seq[index]];
return weight;
}
and i dont think it would cause a problem neither. :(
It's great that you set the pointer value to null after you have free'd it. So make use of that fact, and check for null before you free it - that way you avoid the double delete. Then you don't have to hunt for the double deletion because you will be protected from it.

Scatterplot Not Showing

I'm doing an application that requires core plot for drawing charts, I'm new to this library and I am finding it pretty hard to find good documentation or examples. I'm running into a problem where the line for the graph is not being display despite the fact that the data source method is getting called and returning the right number at the right index. Also the x Axis is being displayed wrong (Check the image below(1.0)). The Y axis is set correctly and the increment is also correct. I've been playing around trying to figure out what it's wrong but I spent too much time already so I was hoping to find some one here that could help or point me at the right direction. This is my implementation file :
-(void)initPlot {
[self generateData];
[self configureHost];
[self configureGraph];
[self configurePlots];
[self configureAxes];
}
- (void)generateData{
//Array containing all the dates that will be displayed on the X axis
dates = [NSArray arrayWithObjects:#"Apr 25", #"Apr 26", #"Apr 29",#"Apr 30", #"May 1", nil];
//Dictionary containing the name of the single set and its associated color
sets = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], #"Plot 1",nil];
_dataY = [[NSMutableArray alloc] init];
[_dataY insertObject:[NSNumber numberWithFloat:618.0] atIndex:0];
[_dataY insertObject:[NSNumber numberWithFloat:613.0] atIndex:0];
[_dataY insertObject:[NSNumber numberWithFloat:613.0] atIndex:0];
[_dataY insertObject:[NSNumber numberWithFloat:614.0] atIndex:0];
[_dataY insertObject:[NSNumber numberWithFloat:604.0] atIndex:0];
_dataForPlot = [[NSMutableArray alloc] init];
for(int i = 0; i < dates.count; i++){
NSString *date = [dates objectAtIndex:i];
NSNumber *price = [_dataY objectAtIndex:i];
NSMutableDictionary *point1 = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:date, #"x", price, #"y", nil] autorelease];
[_dataForPlot addObject:point1];
}
NSLog(#"Data %#",_dataForPlot);
}
-(void)configureHost {
_hostView.allowPinchScaling = NO;
}
-(void)configureGraph {
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
[graph applyTheme:[CPTTheme themeNamed:kCPTPlainBlackTheme]];
_hostView.hostedGraph = graph;
graph.plotAreaFrame.masksToBorder = NO;
// Configure the Graph Padding
graph.paddingLeft = 0.0f;
graph.paddingTop = 0.0f;
graph.paddingRight = 0.0f;
graph.paddingBottom = 0.0f;
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor whiteColor];
borderLineStyle.lineWidth = 2.0f;
graph.plotAreaFrame.borderLineStyle = borderLineStyle;
graph.plotAreaFrame.paddingTop = 10.0;
graph.plotAreaFrame.paddingRight = 10.0;
graph.plotAreaFrame.paddingBottom = 40.0;
graph.plotAreaFrame.paddingLeft = 70.0;
// Set graph title
graph.title = #"Test";
// Create and set text style
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = #"Helvetica-Bold";
titleStyle.fontSize = 16.0f;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
graph.plotAreaFrame.borderLineStyle = nil;
}
- (void)configurePlots{
CPTColor *aColor = [CPTColor redColor];
CPTMutableLineStyle *barLineStyle = [[[CPTMutableLineStyle alloc] init] autorelease];
barLineStyle.lineWidth = 1.0;
barLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
// Enable user interactions for plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(5.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat([self lowerValue]) length:CPTDecimalFromFloat([self higherValue])];
dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = #"Plot 1";
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
CPTGradient *areaGradient = [CPTGradient gradientWithBeginningColor :[CPTColor greenColor]
endingColor :[CPTColor blackColor]];
areaGradient.angle = -90.0f ;
CPTFill *areaGradientFill = [ CPTFill fillWithGradient :areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPTDecimalFromString (#"0.0");
dataSourceLinePlot.interpolation = CPTScatterPlotInterpolationLinear;
// Set up plot space
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.4f)];
plotSpace.yRange = yRange;
// Create styles and symbols
CPTMutableLineStyle *aLineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
aLineStyle.lineWidth = 1.0;
aLineStyle.lineColor = aColor;
dataSourceLinePlot.dataLineStyle = aLineStyle;
//Add legend
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
theLegend.numberOfRows = sets.count;
theLegend.fill = [CPTFill fillWithColor:[CPTColor colorWithGenericGray:0.15]];
theLegend.borderLineStyle = barLineStyle;
theLegend.cornerRadius = 10.0;
theLegend.swatchSize = CGSizeMake(15.0, 15.0);
whiteTextStyle.fontSize = 13.0;
theLegend.textStyle = whiteTextStyle;
theLegend.rowMargin = 5.0;
theLegend.paddingLeft = 10.0;
theLegend.paddingTop = 10.0;
theLegend.paddingRight = 10.0;
theLegend.paddingBottom = 10.0;
graph.legend = theLegend;
graph.legendAnchor = CPTRectAnchorTopLeft;
graph.legendDisplacement = CGPointMake(80.0, -10.0);
}
- (void)configureAxes{
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = #"Helvetica-Bold";
axisTextStyle.fontSize = 11.0f;
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
// Line Style
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 2.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];
//Axises
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
//Y axis
CPTXYAxis *y = axisSet.yAxis;
y.title = #"Price";
y.titleOffset = 50.0f;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
y. majorIntervalLength = CPTDecimalFromString(#"5");
y.minorTicksPerInterval = 4;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
y.minorTickLineStyle = nil;
y.labelOffset = 2.0f;
// Configure x-axis
CPTXYAxis *x = axisSet.xAxis;
x. majorIntervalLength = CPTDecimalFromString (#"5");
x.orthogonalCoordinateDecimal = CPTDecimalFromInt(0);
x.majorIntervalLength = CPTDecimalFromInt(5);
x.minorTicksPerInterval = 0;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorGridLineStyle = majorGridLineStyle;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[_dataForPlot count]];
static CPTMutableTextStyle *labelTextStyle = nil;
labelTextStyle = [[CPTMutableTextStyle alloc] init];
labelTextStyle.color = [CPTColor whiteColor];
labelTextStyle.fontSize = 10.0f;
int index = 0;
for(NSString *date in dates){
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:date textStyle:labelTextStyle];
newLabel.tickLocation = CPTDecimalFromInt(index);
newLabel.offset = x.labelOffset + x.majorTickLength + 5;
newLabel.rotation = M_PI / 4;
[customLabels addObject:newLabel];
[newLabel release];
index++;
}
x.axisLabels = [NSSet setWithArray:customLabels];
}
- (float)higherValue{
NSNumber* max = [_dataY valueForKeyPath:#"#max.self"];
return [max floatValue];
}
- (float)lowerValue{
NSNumber* min = [_dataY valueForKeyPath:#"#min.self"];
return [min floatValue];
}
The Data Source Methods :
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return dates.count;
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSString *key = (fieldEnum == CPTScatterPlotFieldX ? #"x" : #"y");
NSNumber *num = 0;
//if ( [(NSString *)plot.identifier isEqualToString:#"Plot 1"] ) {
num = [[_dataForPlot objectAtIndex:index] valueForKey:key];
if ( fieldEnum == CPTScatterPlotFieldX ) {
num = 0;
}
//}
CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
fadeInAnimation.duration = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:2.0];
[dataSourceLinePlot addAnimation:fadeInAnimation forKey:#"animateOpacity"];
NSLog(#"NUM : %# for key : %# at index : %i",num,key,index);
return num;
}
This is the image :
The x-axis is displayed exactly as you told it. It ranges between -0.25 and 5.25 (0 to 5 expanded by 10%) with five labels at 0, 1, 2, 3, and 4.
The datasource returns nil (0) for the CPTScatterPlotFieldX field for every point. This tells the plot to ignore that point. Based on the plot place range you've set up, you should return an NSNumber containing the index for that field.

Resources