在Objective-C中调用sqlite3数据库时,如何调用和转换int/NSNumber
How to call and convert an int/NSNumber when calling on a sqlite3 database in Objective-C?
提前感谢您抽出时间阅读并希望为我提供帮助。我对编码相对陌生,目前面临的问题是;
我构建了一个sqlite3数据库,有三列,分别存储为;字符串、float和int。当试图读取和表示列时,我很难找到一个表示float和整型值的方法。
存储在数据库中的三个属性设置为;
@property (nonatomic, strong) NSString *name;
@property (nonaomtic, strong) NSNumber *price;
@property (nonaomtic, strong) NSNumber *quantity;
我不确定将后两个属性更改为float和int是否会有所不同?
然而,对我来说,主要的困惑在于我为了调用数据库而编写的以下代码;
-(void)readDataFromDatabase{
[self.stock removeAllObjects];
sqlite3 *database;
if (sqlite_open([self.databasePath UTF8String], &database) == SQLITE_OK) {
char *sqlStatement = "select * from entries";
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK){
while(sqlite3_step(compiledStatement) == SQLITE_ROW{
char *n = sqlite3_column_text(compiledStatement, 1);
char *p = sqlite3_column_int(compiledStatement, 2);
chat *q = sqlite3_column_int(compiledStatement, 3);
NSString *name = [NSString stringWithUTF8String:n];
NSNumber *price = [NSString stringWithUTF8String:p];
NSNumber *quantity = [NSString stringWithUTF8String:q];
Data *data = [[Data alloc] initWithData:name thePrice: price theQuantity:quantity];
[self.stock addObject:data];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
我的常识告诉我问题在于这一部分;
NSString *name = [NSString stringWithUTF8String:n];
NSNumber *price = [NSString stringWithUTF8String:p];
NSNumber *quantity = [NSString stringWithUTF8String:q];
我不确定该如何处理NSNumber?我知道NSString字符串WithUTF8String是不正确的,但我不知道NSNumber的等价项。
我真的很感激在这件事上有任何见解和帮助。
谢谢!
您对float
和int
所做的介绍是正确的。Float
不需要像NSString
那样被引用,因为它的大小在32位和64位系统之间没有变化。如果你的整数更大,那么我建议使用NSInteger
作为该值:
char *n = sqlite3_column_text(compiledStatement, 1);
float p = sqlite3_column_text(compiledStatement, 2);
int q = sqlite3_column_text(compiledStatement, 3);
NSString *name = [NSString stringWithUTF8String:n];
float price = p;
NSInteger quantity = q;
注意:您不需要为float或int值指定*
,因为它们不是指针。
你甚至可以只做(如果你已经在你的头中声明了属性):
name = [NSString stringWithUTF8String:sqlite3_column_text(compiledStatement, 1)];
price = sqlite3_column_text(compiledStatement, 2);
quantity = sqlite3_column_text(compiledStatement, 3);
要检查值,可以使用NSLog
:
NSLog(@"%@ %.02f %li", name, price, quantity);
为了完整起见,如果您想将float
和int
制作成NSNumber
:
NSString *name = [NSString stringWithUTF8String:n];
NSNumber *price = [NSNumber numberWithFloat:p];
NSNumber *quantity = [NSNumber numberWithInt:q];
标题中的属性声明为:
name = [NSString stringWithUTF8String:n];
value = [NSNumber numberWithFloat:p];
quantity = [NSNumber numberWithInt:q];
NSLog(@"%@ %@.02f %@", name, price, quantity);
有多种方法可以获得结果,这就是Objective-C非常灵活的原因。
相关文章:
- Rcpp并行无匹配函数,用于调用"转换"
- 为什么在std::for_each()返回时调用转换运算符
- 将多个非原始递归调用转换为迭代解决方案
- 在重载解析期间调用转换运算符,而不是在 c++17 中转换构造函数
- 非调用转换函数上的模板中的编译错误
- 为什么在没有赋值运算符的情况下调用转换构造函数
- 调用转换操作员
- 将C++中涉及数组的函数调用转换为C#时出现问题
- 操作员重载内部调用转换
- 为什么在使用初始化语法时不调用转换运算符,为什么 clang 错误消息看起来是错误的
- 使用消息处理程序将 c++ dll 调用转换为 delphi
- Delphi - 从 dll 调用转换(读取)C++ NULL 终止数组
- 如何直接调用转换运算符
- c++.将定义了2个点的绘图贝塞尔曲线调用转换为定义了3个点的调用
- 调用转换操作符不能用于静态const变量
- 在osx/win7/linux上不同的c++调用转换
- 为什么在这里调用转换构造函数
- 将C API调用转换为delphi
- 为move调用转换构造函数,但未为copy调用转换构造函数
- 没有可行的std::weak_ptr到std::shared_ptr的方法调用转换