如何将值从 NSString 转换为 NSInteger

发布于 2024-12-20 17:56:11 字数 2580 浏览 3 评论 0原文

我是 iPhone 开发新手。

我想使用下面提到的过程从数据库中获取记录。

但是,当我将 searchDbAuthorId1 或 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

提前致谢。

I am new to iPhone development.

I would like to fetch records from a database using the procedure mentioned below.

But when I passed the searched value of searchDbAuthorId1 or searchDbQuoteId to a method, it showed a BAD ACCESS message.

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);

I used NSLOG to know the actual values.

Following are the values with %@

These are the correct values I needed

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

and values with %d

and I am getting these values

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

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡看悲欢离合 2024-12-27 17:56:11

您的问题对我来说确实没有意义,我们需要更多信息,但我看到您已经找到了问题所在。那么,回答你的标题:

如何将值从 Nsstring 转换为 NSinteger

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

NSInteger integer = [string integerValue];

Your question doesn't really makes sense to me, we'd need more information but I see you already tracked down the problem. So, answering your title:

How to convert values from Nsstring to NSinteger

You can use the -integerValue method of NSString.

NSInteger integer = [string integerValue];
奶气 2024-12-27 17:56:11

NSInteger myInt = [myString intValue]; 是问题标题的答案。但问题内容不是很清楚,我不确定你在问什么。

NSInteger myInt = [myString intValue]; is the answer to the title of your question. The questions contents, however, are not very clear and I'm unsure of what you are asking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文