2011-12-12 131 views
0

我是iPhone開發新手。如何將NSString的值轉換爲NSInteger

我想使用下面提到的過程從數據庫中提取記錄。

但是,當我將搜索到的searchDbAuthorId1 or searchDbQuoteId的值傳遞給某個方法時,它顯示了一條BAD ACCESS消息。

if(sqlite3_open([self.databasePath UTF8String], &database) == SQLITE_OK) 
    { 
     NSString *querySQL = [NSString stringWithFormat:@"select distinct(qot.id), key.Authorid, key.Keyword, qot.Quotes from Keywords key inner JOIN Quotes qot on key.Authorid=qot.AuthorID where key.Keyword like \'%@\'",dataSearch]; 
     sqlite3_stmt *compiledStatement; 
     const char *query_stmt = [querySQL UTF8String]; 
     if(sqlite3_prepare_v2(database, query_stmt, -1, &compiledStatement, NULL) == SQLITE_OK) 
     { 
      while (sqlite3_step(compiledStatement)==SQLITE_ROW) 
      { 
       NSString *searchDbAuthorId1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(compiledStatement, 1)]; 
       NSString *searchDbQuoteId1 = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(compiledStatement, 0)]; 
       NSString *searchDbKeyword1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(compiledStatement, 2)]; 
       NSString *searchDbQuote1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(compiledStatement, 3)]; 

       terms *Objterm = [[terms alloc] init]; 
       Objterm->searchDbQuoteId = searchDbQuoteId1; 
       Objterm->searchDbAuthorId = searchDbAuthorId1; 
       Objterm->searchDbKeyword = searchDbKeyword1; 
       Objterm->searchDbQuote = searchDbQuote1; 

       NSLog(@"searchDbQuoteId = %@",Objterm->searchDbQuoteId); 
       NSLog(@"searchDbAuthorId = %@",Objterm->searchDbAuthorId); 
       NSLog(@"searchDbKeyword= %@",Objterm->searchDbKeyword); 
       NSLog(@"searchDbQuote= %@",Objterm->searchDbQuote); 

       NSLog(@"searchDbQuoteId = %d",Objterm->searchDbQuoteId); 
       NSLog(@"searchDbAuthorId = %d",Objterm->searchDbAuthorId); 

我用NSLOG知道實際值。

以下是與%@

值這些都是我需要

2011-12-12 23:03:50.612 Quotes[1039:207] searchDbQuoteId = 15 
2011-12-12 23:03:50.988 Quotes[1039:207] searchDbAuthorId = 6 

正確的價值觀和價值觀%d

和我得到這些值

2011-12-12 23:03:52.332 Quotes[1039:207] searchDbQuoteId = 109590272 
2011-12-12 23:03:53.580 Quotes[1039:207] searchDbAuthorId = 109607456 

謝謝提前。

+0

[轉換第一數目在一個NSString成整數?](可能重複http://stackoverflow.com/questions/1138759/convert-first-number-in-an-nsstring-into-an-整數) – Caleb

回答

2

NSInteger myInt = [myString intValue];是問題標題的答案。然而,問題內容並不十分清楚,我不確定你在問什麼。

4

你的問題對我來說沒有意義,我們需要更多的信息,但我看到你已經找到了問題。所以,回答你的標題:

如何從數值轉換NSString的到NSInteger的

你可以使用NSString的-integerValue方法。

NSInteger integer = [string integerValue]; 
相關問題