2012-10-01 110 views
0

我創建像這樣在Xcode的字符串:變量是不是字符串

@property (nonatomic, retain) NSString *lati; 

我合成它,以及...

現在在我的viewLoad我這個功能讓我的緯度:

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation { 
    int degrees = newLocation.coordinate.latitude; 
    double decimal = fabs(newLocation.coordinate.latitude - degrees); 
    int minutes = decimal * 60; 
    double seconds = decimal * 3600 - minutes * 60; 
    lati = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
       degrees, minutes, seconds]; 
    //latLabel.text = lat; 
    degrees = newLocation.coordinate.longitude; 
    decimal = fabs(newLocation.coordinate.longitude - degrees); 
    minutes = decimal * 60; 
    seconds = decimal * 3600 - minutes * 60; 
    longi = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
        degrees, minutes, seconds]; 
} 

後來我想了幾個字符串保存到我的slite3數據庫,但我得到一個Bad_Access因爲我拉逖字符串沒有一套正確的,但我不知道爲什麼...這是我SaveToDB方法:

if (sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) { 
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO DATEN(download, upload, ping, comment, date, network, latitude, longitude) VALUES (\"%@\", \"%@\", \"%@\",\"%@\", \"%@\",\"%@\", \"%@\",\"%@\")",topDownloadLabel.text, topUploadLabel.text, pingLabel.text, Comment, string, WlanOrUmts, lati, longi]; 

在那裏我得到了Bad_Access。

+1

你使用的是ARC嗎? (可能不是。)當你將'lati'賦值給autorelained字符串而不是'self.lati'時,字符串將不會被保留。 –

+0

不,ARC設置爲「否」 – davidOhara

+4

使用self.lati代替lati。 –

回答

3

您使用的是iVar而不是property,只是儘量

self.lati = … 

,而不是...

編輯:對不起,我錯過了意見,同時書面答覆,同時撥打電話時間;-)

+0

無論如何這個答案是正確的,應該被接受=) – Nekto

0

現在一切都很好,把我的字符串保存到數據庫,但在我的下一步我想讀取我的數據庫字符串...一切都很好...

-(NSMutableArray *) daten{ 
daten = [[NSMutableArray alloc] initWithCapacity:100]; 
@try { 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 
    NSString *dbPath = [self getDBPath]; 
    BOOL success = [fileMgr fileExistsAtPath:dbPath]; 
    if(!success) 
    { 
     NSLog(@"Cannot locate database file '%@'.", dbPath); 
    } 
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)) 
    { 
     NSLog(@"An error has occured: %s", sqlite3_errmsg(db)); 

    } 


    const char *sql = "SELECT * FROM Daten"; 
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK) 
    { 
     NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db)); 
    }else{ 

     while (sqlite3_step(sqlStatement)==SQLITE_ROW) { 
      Daten * infos = [[Daten alloc] init]; 
      infos.download = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)]; 
      infos.upload = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)]; 
      infos.ping = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,3)]; 
      infos.comment = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,4)]; 
      infos.date = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,5)]; 
      infos.type = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,6)]; 
      infos.lati = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,7)]; 
      infos.longi = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,8)]; 


      [daten addObject:infos]; 
     } 
    } 
} 
@catch (NSException *exception) { 
    NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db)); 
} 
@finally { 
    sqlite3_finalize(sqlStatement); 
    sqlite3_close(db); 

    return daten; 
} 
} 

但是,如果我想這些值傳遞給我在我的prepareForSegue方法未來的看法,我得到一個Bad_Access:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if ([segue.identifier isEqualToString:@"showDetails"]) 
{ 
    @try { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 
     Daten *date = [[Daten alloc] init]; 
     date = [daten objectAtIndex: storyIndex]; 
     UINavigationController *navController = (UINavigationController*)[segue destinationViewController]; 
     DetailViewController *eventsController = [navController topViewController]; 
     [eventsController setDownload:date.download]; 
     [eventsController setUpload:date.upload]; 
     [eventsController setPing:date.ping]; 
     [eventsController setComment:date.comment]; 
     [eventsController setNetwork:date.type]; 
     //NSLog(@"The content of arry is%@",daten); 
     [eventsController setDate:date.date]; 
     UINavigationController *navigationController = segue.destinationViewController; 
     DetailViewController *DetailsViewController = [[navigationController viewControllers] objectAtIndex:0]; 
     DetailsViewController.delegate = self; 

    } 
    @catch (NSException *exception) { 
     NSLog(@"%@", exception); 
    } 
    @finally { 

    } 
     } 
} 

但只有在date.date或date.type ....