Core Plot: only works ok with three plots - xcode4

I am adding a scatter plot to my app (iGear) so when the user selects one, two or three chainrings combined with a cogset on a bike, lines will show the gears meters.
The problem is that Core Plot only shows the plots when three chainrings are selected. I need your help, this is my first try at Core Plot and I'm lost.
My code is the following:
iGearMainViewController.m
- (IBAction)showScatterIpad:(id)sender {
cogsetToPass = [NSMutableArray new];
arrayForChainringOne = [NSMutableArray new];
arrayForChainringTwo = [NSMutableArray new];
arrayForChainringThree = [NSMutableArray new];
//behavior according to number of chainrings
switch (self.segmentedControl.selectedSegmentIndex) {
case 0: // one chainring selected
for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
if (i <10) {
corona = [NSString stringWithFormat:#"cog0%d",i];
}else {
corona = [NSString stringWithFormat:#"cog%d",i];
}
float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
float teeth = [[cassette valueForKey:corona] floatValue];
[cogsetToPass addObject:[NSNumber numberWithFloat:teeth]];
[arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
}
break;
case 1: // two chainrings selected
for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
if (i <10) {
corona = [NSString stringWithFormat:#"cog0%d",i];
}else {
corona = [NSString stringWithFormat:#"cog%d",i];
}
float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
//NSLog(#" gearsForOneChainring = %#",[NSNumber numberWithFloat:one]);
float two = (wheelSize*[_twoChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
[cogsetToPass addObject:[NSNumber numberWithFloat:[[cassette valueForKey:corona]floatValue]]];
[arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
[arrayForChainringTwo addObject:[NSNumber numberWithFloat:two]];
}
break;
case 2: // three chainrings selected
for (int i = 1; i<= [cassette.numCogs intValue]; i++) {
if (i <10) {
corona = [NSString stringWithFormat:#"cog0%d",i];
}else {
corona = [NSString stringWithFormat:#"cog%d",i];
}
float one = (wheelSize*[_oneChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
float two = (wheelSize*[_twoChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
float three = (wheelSize*[_threeChainring.text floatValue]/[[cassette valueForKey:corona]floatValue])/1000;
[cogsetToPass addObject:[cassette valueForKey:corona]];
[arrayForChainringOne addObject:[NSNumber numberWithFloat:one]];
[arrayForChainringTwo addObject:[NSNumber numberWithFloat:two]];
[arrayForChainringThree addObject:[NSNumber numberWithFloat:three]];
}
default:
break;
}
ScatterIpadViewController *sivc = [[ScatterIpadViewController alloc]initWithNibName: #"ScatterIpadViewController" bundle:nil];
[sivc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
sivc.records = [cassetteNumCogs integerValue];
sivc.cogsetSelected = self.cogsetToPass;
sivc.chainringOne = self.arrayForChainringOne;
sivc.chainringThree = self.arrayForChainringThree;
sivc.chainringTwo = self.arrayForChainringTwo;
[self presentViewController:sivc animated:YES completion:nil];
}
And the child view with the code to draw the plots:
ScatterIpadViewController.m
#pragma mark - CPTPlotDataSource methods
- (NSUInteger)numberOfRecordsForPlot: (CPTPlot *)plot {
return records;
}
- (NSNumber *)numberForPlot: (CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
switch (fieldEnum)
{
case CPTScatterPlotFieldX:
return [NSNumber numberWithInt:index];
break;
case CPTScatterPlotFieldY:{
if ([plot.identifier isEqual:#"one"]==YES) {
//NSLog(#"chainringOne objectAtIndex:index = %#", [chainringOne objectAtIndex:index]);
return [chainringOne objectAtIndex:index];
}else if ([plot.identifier isEqual:#"two"] == YES ){
//NSLog(#"chainringTwo objectAtIndex:index = %#", [chainringTwo objectAtIndex:index]);
return [chainringTwo objectAtIndex:index];
}else if ([plot.identifier isEqual:#"three"] == YES){
//NSLog(#"chainringThree objectAtIndex:index = %#", [chainringThree objectAtIndex:index]);
return [chainringThree objectAtIndex:index];
}
default:
break;
}
}
return nil;
}
The error returned is an exception on trying to access an empty array.
2012-11-15 11:02:42.962 iGearScatter[3283:11603] Terminating app due to uncaught exception 'NSRangeException', reason: ' -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
First throw call stack:
(0x1989012 0x1696e7e 0x192b0b4 0x166cd 0x183f4 0x1bd39 0x179c0 0x194fb 0x199e1 0x43250 0x14b66 0x13ef0 0x13e89 0x3b5753 0x3b5b2f 0x3b5d54 0x3c35c9 0x5c0814 0x392594 0x39221c 0x394563 0x3103b6 0x310554 0x1e87d8 0x27b3014 0x27a37d5 0x192faf5 0x192ef44 0x192ee1b 0x29ea7e3 0x29ea668 0x2d265c 0x22dd 0x2205 0x1)*
libc++abi.dylib: terminate called throwing an exception
Thank you!

Solved! I saw the light while riding on bicycle.
- (NSNumber *)numberForPlot: (CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
if (([chainringOne count]!=0) && ([chainringTwo count]==0) && ([chainringThree count]==0)) {
switch (fieldEnum) {
case CPTScatterPlotFieldX:
return [NSNumber numberWithInt:(index+1)];
break;
case CPTScatterPlotFieldY:{
if ([plot.identifier isEqual:#"one"]) {
return [chainringOne objectAtIndex:index];
}
}
default:
break;
}
}else if (([chainringOne count]!=0) && ([chainringTwo count]!=0) && ([chainringThree count]==0)){
switch (fieldEnum) {
case CPTScatterPlotFieldX:
return [NSNumber numberWithInt:(index+1)];
break;
case CPTScatterPlotFieldY:{
if ([plot.identifier isEqual:#"one"]) {
return [chainringOne objectAtIndex:index];
}else if ([plot.identifier isEqual:#"two"]){
return [chainringTwo objectAtIndex:index];
}
}
default:
break;
}
}else if (([chainringOne count]!=0) && ([chainringTwo count]!=0) && ([chainringThree count]!= 0)){
switch (fieldEnum) {
case CPTScatterPlotFieldX:
return [NSNumber numberWithInt:(index+1)];
break;
case CPTScatterPlotFieldY:{
if (([chainringOne count] !=0) && ([plot.identifier isEqual:#"one"])) {
return [chainringOne objectAtIndex:index];
}else if ((chainringTwo || chainringTwo.count) &&([plot.identifier isEqual:#"two"] == YES )){
return [chainringTwo objectAtIndex:index];
}else if ((chainringThree || chainringTwo.count)&& ([plot.identifier isEqual:#"three"] == YES)){
return [chainringThree objectAtIndex:index];
}
default:
break;
}
}
}
return nil;
}

Related

Arduino void loop only running once

The void loop is only running once. Please help. I am inexperienced with Arduino, so any help is much-appreciated. When I open the serial monitor I only see zero once. This code was working earlier today. Sorry if the solution is very simple. Some code has been removed to fit within post restrictions such as ints and ultrasonic declarations and includes.
void setup() {
Serial.begin(9600); //sets up Serial monitor
InfraredSeeker::Initialize();
}
void loop() {
if(u = 1) {
ang = uang;
} else{
ang = irang;
}
Serial.println(uang);
m1 = 200*cos((30-ang)*0.0174533);
m2 = 200*cos((270-ang)*0.0174533);
m3 = 200*cos((150-ang)*0.0174533);
InfraredResult InfraredBall = InfraredSeeker::ReadAC();
switch (InfraredBall.Direction) {
case 1:
irang = 160;
break;
case 2:
irang = 120;
break;
case 3:
irang = 90;
break;
case 4:
irang = 50;
break;
case 5:
irang = 0;
break;
case 6:
irang = 310;
break;
case 7:
irang = 270;
break;
case 8:
irang = 240;
break;
case 9:
irang = 200;
break;
case 0:
irang = 160;
break;
default:
break;
}
lultra = uleft.Ranging(CM);
rultra = urigth.Ranging(CM);
fultra = ufront.Ranging(CM);
if (lultra < 15){
u = 1;
uang = 270;
}
if (rultra < 15) {
u = 1;
uang = 90;
}
if (fultra < 15) {
u = 1;
uang = 180;
}
}

Is this usage of arg ok?

msg.append(QString("\"settime\": %1000,") .arg(eventList[i].failureBegin)); // set time
I would like to know if it`s ok to have %1 right next to 000. Since there is only 1 argument then there is obviously no confusion possible for QString but what if I had 10 .arg() Then it would confuse it with %10 right? Is there an escape sequence for this or do I have to break it down into concatenations?
It is not ok. You can explore the source code of QString::arg or let me show you.
QString QString::arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
{
ArgEscapeData d = findArgEscapes(*this);
if (d.occurrences == 0) {
qWarning() << ""QString::arg: Argument missing:"" << *this << ',' << a;
return *this;
}
//.... something not important.
}
This is how findArgEscapes is implemented. I think source code of Qt is much more readable than most STL implementations.
static ArgEscapeData findArgEscapes(const QString &s)
{
const QChar *uc_begin = s.unicode();
const QChar *uc_end = uc_begin + s.length();
ArgEscapeData d;
d.min_escape = INT_MAX;
d.occurrences = 0;
d.escape_len = 0;
d.locale_occurrences = 0;
const QChar *c = uc_begin;
while (c != uc_end) {
while (c != uc_end && c->unicode() != '%')
++c;
if (c == uc_end)
break;
const QChar *escape_start = c;
if (++c == uc_end)
break;
bool locale_arg = false;
if (c->unicode() == 'L') {
locale_arg = true;
if (++c == uc_end)
break;
}
int escape = c->digitValue();
if (escape == -1)
continue;
++c;
if (c != uc_end) {
int next_escape = c->digitValue();
if (next_escape != -1) {
escape = (10 * escape) + next_escape; //*************
++c;
}
}
if (escape > d.min_escape)
continue;
if (escape < d.min_escape) {
d.min_escape = escape;
d.occurrences = 0;
d.escape_len = 0;
d.locale_occurrences = 0;
}
++d.occurrences;
if (locale_arg)
++d.locale_occurrences;
d.escape_len += c - escape_start;
}
return d;
}

Computing multiple numbers with multiple operations

So I am creating a calculator that will compute numbers.I have this code(I will not include very basic codes)
long num1, num2, answer;
boolean mySwitch = false;
boolean do_subtraction_flag = false; // when true we will apply subtraction
boolean multiply = false;
boolean divide = false;
void loop()
{
char keypressed = myKeypad.getKey();
if(keypressed != NO_KEY)
{
Serial.print(keypressed);
if(keypressed > 47 && keypressed < 58) // is between '0' and '9'
{
if(!mySwitch)
{
num1 = (num1 * 10) + (keypressed - 48);
}
else
{
num2 = (num2 * 10) + (keypressed - 48);
}
}
if(keypressed == '=')
{
if(do_subtraction_flag) // we want to subtract the numbers
{
answer = num1 - num2;
}else if(multiply){
answer = num1 * num2;
}else if(divide){
answer = num1 / num2;
}
else // we want to add the numbers
{
answer = num1 + num2;
}
Serial.println(answer);
num1 = 0;
num2 = 0;
mySwitch = false;
do_subtraction_flag = false;
multiply = false;
divide = false;
}
else if(keypressed == '+')
{
mySwitch = true;
}
else if(keypressed == '*'){
mySwitch = true;
multiply = true;
}else if(keypressed == '/'){
mySwitch = true;
divide = true;
}
else if(keypressed == '-')
{
mySwitch = true;
do_subtraction_flag = true;
}else if (keypressed == 'C'){
for (int i=0; i < 80; i++)
{
Serial.write(8); // print 80 times backspace (BS)
}
}
}
}
Im confused here because I want to compute multiple number with multiple operands (eg., 2+1+3 or 2+1-2), but when i add another variable 'num3' what should I suppose to do with it? if i put it into the do_subtraction flag what if the user inputs 2-1+3?Is it possible to compute 3 numbers with this code?I'm getting confused here but let me know if you are also confused what I want to do
Make an array to hold the numbers and operators that you input, then loop through them when the input == '='. Have your num1 always hold the result and num2 hold the next array value.

sqlite iphone sdk help?

I am reading the sqlite but unable to get data instead it showing nil values. please refer the given code....
-(void)getDatabasePath:(NSString *)dBPath sqlCatagoryId:(NSString *)catId {
if(sqlite3_open([dBPath UTF8String], &_database) == SQLITE_OK) {
NSLog(#"database path %#", dBPath);
NSString *sqlStatement = [NSString stringWithFormat:#"SELECT * FROM SubCategories where CAT_ID = '%#'",catId];
NSString *aCategoryId, *aSubCategoryId, *aSubCategoryName, *aSubCategoryDescription;
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [sqlStatement UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char * str1 = (char *)sqlite3_column_text(compiledStatement, 1);
if (str1) {
aCategoryId = [NSString stringWithUTF8String:str1];
}
else {
aCategoryId = #"";
}
char * str2 = (char *)sqlite3_column_text(compiledStatement, 2);
if (str1) {
aSubCategoryId = [NSString stringWithUTF8String:str2];
}
else {
aSubCategoryId = #"";
}
char * str3 = (char *)sqlite3_column_text(compiledStatement, 3);
if (str1) {
aSubCategoryName = [NSString stringWithUTF8String:str3];
}
else {
aSubCategoryName = #"";
}
char * str4 = (char *)sqlite3_column_text(compiledStatement, 4);
if (str1) {
aSubCategoryDescription = [NSString stringWithUTF8String:str4];
}
else {
aSubCategoryDescription = #"";
}
NSData *aSubCategoryImage;
NSUInteger blobLength = sqlite3_column_bytes(compiledStatement, 5);
if(blobLength > 0){
aSubCategoryImage = [NSData dataWithBytes:sqlite3_column_blob(compiledStatement, 5) length:blobLength];
}
else {
aSubCategoryImage = nil;
}
SubCategoryModel *subCategoryModel = [[SubCategoryModel alloc] initWithCategoryId:aCategoryId subCategoryId:aSubCategoryId subCategoryName:aSubCategoryName subCategoryDescription:aSubCategoryDescription subCategoryImage:aSubCategoryImage];// database:database
//Add the details object to the Array
[subCategorryArray addObject:subCategoryModel];
[subCategoryModel release];
}
sqlite3_finalize(statement);
}
}
[self.aTableView reloadData];
}
please point out where I am going wrong or why I am not able to get data from sqlite. thanks
I was doing wrong in sqlite3_stmt *statement; and sqlite3_stmt * compiledStatement;
that I mixed the both sqlite3_stmt

maxRequestLength - behind the scene details needed!

What really happens to the currently executing request in IIS during a file upload, when the upload length exceed configured maxRequestLength.
Tried hard to find a decent article that talks about that, but there are none!!
Thanks
This is what exactly happens:
In class HttpRequest and in method GetEntireRawContent this condition is checked and will throw an exception:
if (length > maxRequestLengthBytes)
{
throw new HttpException(System.Web.SR.GetString("Max_request_length_exceeded"), null, 0xbbc);
}
Here is the whole of the method if you find useful:
private HttpRawUploadedContent GetEntireRawContent()
{
if (this._wr == null)
{
return null;
}
if (this._rawContent == null)
{
HttpRuntimeSection httpRuntime = RuntimeConfig.GetConfig(this._context).HttpRuntime;
int maxRequestLengthBytes = httpRuntime.MaxRequestLengthBytes;
if (this.ContentLength > maxRequestLengthBytes)
{
if (!(this._wr is IIS7WorkerRequest))
{
this.Response.CloseConnectionAfterError();
}
throw new HttpException(System.Web.SR.GetString("Max_request_length_exceeded"), null, 0xbbc);
}
int requestLengthDiskThresholdBytes = httpRuntime.RequestLengthDiskThresholdBytes;
HttpRawUploadedContent data = new HttpRawUploadedContent(requestLengthDiskThresholdBytes, this.ContentLength);
byte[] preloadedEntityBody = this._wr.GetPreloadedEntityBody();
if (preloadedEntityBody != null)
{
this._wr.UpdateRequestCounters(preloadedEntityBody.Length);
data.AddBytes(preloadedEntityBody, 0, preloadedEntityBody.Length);
}
if (!this._wr.IsEntireEntityBodyIsPreloaded())
{
int num3 = (this.ContentLength > 0) ? (this.ContentLength - data.Length) : 0x7fffffff;
HttpApplication applicationInstance = this._context.ApplicationInstance;
byte[] buffer = (applicationInstance != null) ? applicationInstance.EntityBuffer : new byte[0x2000];
int length = data.Length;
while (num3 > 0)
{
int size = buffer.Length;
if (size > num3)
{
size = num3;
}
int bytesIn = this._wr.ReadEntityBody(buffer, size);
if (bytesIn <= 0)
{
break;
}
this._wr.UpdateRequestCounters(bytesIn);
this.NeedToInsertEntityBody = true;
data.AddBytes(buffer, 0, bytesIn);
num3 -= bytesIn;
length += bytesIn;
if (length > maxRequestLengthBytes)
{
throw new HttpException(System.Web.SR.GetString("Max_request_length_exceeded"), null, 0xbbc);
}
}
}
data.DoneAddingBytes();
if ((this._installedFilter != null) && (data.Length > 0))
{
try
{
try
{
this._filterSource.SetContent(data);
HttpRawUploadedContent content2 = new HttpRawUploadedContent(requestLengthDiskThresholdBytes, data.Length);
HttpApplication application2 = this._context.ApplicationInstance;
byte[] buffer3 = (application2 != null) ? application2.EntityBuffer : new byte[0x2000];
while (true)
{
int num7 = this._installedFilter.Read(buffer3, 0, buffer3.Length);
if (num7 == 0)
{
break;
}
content2.AddBytes(buffer3, 0, num7);
}
content2.DoneAddingBytes();
data = content2;
}
finally
{
this._filterSource.SetContent(null);
}
}
catch
{
throw;
}
}
this._rawContent = data;
}
return this._rawContent;
}

Resources