Java通用投射(Java generic casting)
我有一个泛型类
A<T>
,一个子类B扩展了A<String>
。 A类有一个返回它的方法(所以A<T>
)。 问题是,如果我在B对象上调用该方法,它将返回A<String>
并且我必须转换为B,但是B与A<String>
相同,不是吗? 所以我不应该投...I have a generic class
A<T>
and a subclass B extendsA<String>
. The class A have a method which returns this (so anA<T>
). The problem is, if I call the method on a B object, it will return aA<String>
and I have to cast to B, but B is the same asA<String>
, isn't it ? So I shouldn't have to cast ...
原文:https://stackoverflow.com/questions/38973046
最满意答案
如果您想要获取数组中的所有名称字段,下面的代码应该可以使用
NSMutableArray* result=[[NSMutableArray alloc] init]; NSString *ask = [NSString stringWithFormat:@"SELECT name FROM users"]; sqlite3_stmt *statement; const char *query_statement = [ask UTF8String]; const char *dbpath = [_databasePath UTF8String]; if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){ { if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)]; //add your namefield here. [result addObject:nameField]; } sqlite3_finalize(statement); sqlite3_close(_DB); } }
最后你可以做到
totalStrings=[[NSMutableArray alloc] initWithArray:result];
The code below should work in case you want to get all namefields in array
NSMutableArray* result=[[NSMutableArray alloc] init]; NSString *ask = [NSString stringWithFormat:@"SELECT name FROM users"]; sqlite3_stmt *statement; const char *query_statement = [ask UTF8String]; const char *dbpath = [_databasePath UTF8String]; if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){ { if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)]; //add your namefield here. [result addObject:nameField]; } sqlite3_finalize(statement); sqlite3_close(_DB); } }
In the end you can do
totalStrings=[[NSMutableArray alloc] initWithArray:result];
相关问答
更多-
在NSOrderedSet类中有一个方法来获取数组[orderedSet array] 。 然后你可以[array mutableCopy]来获得一个可变数组。 或者使用[NSMutableArray arrayWithArray:[set array]]; 如果你更喜欢。 但请注意这一点: 这将为接收的有序集合返回一个代理对象,这就像一个不可变数组一样。 虽然您不能通过此代理对有序集合进行变异,但原始有序集合的变化将反映在代理中,并且由于没有进行有序集合的复制,它会自发地变化。 基本上你必须复制数组,如果 ...
-
有许多方法可以复制数组:您可以使用 - [NSArray copy]获取不可变副本,或者[NSArray mutableCopy]使用可变副本。 不要忘记该副本添加了一个引用,因此您需要在某处发布或自动释放(如果您不使用GC)。 或者,您可以使用 - [NSMutableArray addObjectsFromArray:]。 举个例子,看起来你想在最后做这样的事情: [flashCardQuestionViewController setListNoOfOptionsInQuestion:optionTe ...
-
NSMutableArray的NSInternalInconsistencyException(NSInternalInconsistencyException for NSMutableArray)[2019-11-18]
使用options:NSJSONReadingMutableContainers NSJSONSerialization调用上的NSJSONReadingMutableContainers。 然后它创建的所有字典和数组都是可变的。 Use options:NSJSONReadingMutableContainers on your NSJSONSerialization call. Then all the dictionaries and arrays it creates will be mutable ... -
对于非常少量的数据,如几个字符串,但不是图像,您也可以使用更简单的NSUserDefaults。 这通常用于保存首选项或某些持久数据。 要保存它: [[NSUserDefaults standardUserDefaults] aString forKey:@"aStringKey]; if (![[NSUserDefaults standardUserDefaults] synchronize]) NSLog(@"not successful in writing the default prefs ...
-
如果您想要获取数组中的所有名称字段,下面的代码应该可以使用 NSMutableArray* result=[[NSMutableArray alloc] init]; NSString *ask = [NSString stringWithFormat:@"SELECT name FROM users"]; sqlite3_stmt *statement; const char *query_statement = [ask UTF8String]; ...
-
要将CGPoint存储在NSMutableArray ,请执行此操作 NSMutableArray *yourCGPointsArray = [[NSMutableArray alloc] init]; [yourCGPointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(100, 100)]]; //Now getting the cgpoint back CGPoint point = [[yourCGPointsArray object ...
-
您只需创建一个对象tmpItem 。 这将添加到数组中,并且在while循环的下一次运行中,您不是要创建新的tmpItem而是修改旧的tmpItem并将其添加到数组中。 因此,您将得到一个包含两个指向同一对象tmpItem (具有最新状态)的指针的Array。 解决方案:在while循环中创建tmpItem 。 You just create one object tmpItem. This will be added to the array and in the next run of the whil ...
-
NSMutableArray * muArr = [NSMutableArray new]; for(NSDictionary * dict in yourMuArr) { [muArr addObject:dict[@"resim"]]; } NSArray * allImages = [NSArray arrayWithArray:muArr]; NSMutableArray * muArr = [NSMutableArray new]; for(NSDictionary * dict in yo ...
-
不执行任何查询SQLite(Not executing any queries SQLite)[2023-10-10]
问题是你在布尔变量中保存SQLite调用的返回值。 您应该将这些变量更改为int ,与sqlite3_xxx()函数调用return的类型相同。 线索是您在修订后的问题中提供的错误消息: 常量101与'BOOL'(又名'bool')表达式的比较总是错误的。 当您为64位目标构建时, BOOL被定义为bool类型。 并且bool类型将采用任何非零值并将其更改为1 。 但是,对于非64位目标, BOOL被定义为signed char (它不会将非零值映射到1 ),这就是为什么你的代码在针对某些目标而不是其他目标 ... -
来自sqlite数据库的NSMutableArray addObject无法正常工作(NSMutableArray addObject from sqlite database not working)[2020-08-23]
尝试调试你的代码...在NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];上设置一个断点NSString *field2Str = [[NSString alloc]initWithUTF8String:field2]; 行,并检查field2是否为空... 尝试像这样打开db: NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"speakerdb" ...